refactor: 精简三处重复 — as_text×18、catalog 助手×4、view 路由 34 臂

按审计结论清理冗余(不改变行为):
1. as_text:17 个 host 模块各自复制同一函数(两种措辞),合并为
   crate::host::as_text(统一 "{label} needs text",无测试依赖旧措辞)
2. catalog.rs:extract_fm_field / extract_nested_by / extract_fm_list /
   yaml_escape 与 knowledge::util 逐字相同,改为直接引用
3. view/mod.rs:handle 29 臂 + handle_debug 5 臂折叠成两张路由表
   (Notebook/Debug/Action 三种处理器),每臂原本重复同一 api_debug_post 包装

实测:
- cargo build 零警告;cargo test --release 605 passed / 0 failed
- view 服务实测:GET / → 200;POST /api/debug/continue → 200(真实响应
  {"error":"unknown session"});POST /api/nope → 405

行数:view/mod.rs -99(168 删 69 增);src 合计由 53,276 降至约 53,000
(as_text 18 文件各约 6 行、catalog 4 函数约 70 行)

期间发现并修正一处真实风险:路由臂分布在两个 handler(handle / handle_debug),
首次只替换了一个导致 debug 路由 405;已按 handler 分别建表并由实测覆盖。
This commit is contained in:
cflmy committed 2026-10-22 15:39:18 +08:00
1 parent 6ae0d39e5a
commit 8b0ea9ed7a
21 files changed
+101 -377

No files matched your search

+3 -101
View File
@@ -3,6 +3,9 @@
use std::fs;
use std::path::{Path, PathBuf};
// `<TAB>` slimming: these were byte-identical copies of `knowledge::util`.
use crate::knowledge::util::{extract_fm_field, extract_fm_list, extract_nested_by, yaml_escape};
use anyhow::{Context, Result};
use crate::lex::{classify_source, LineKind};
@@ -318,111 +321,10 @@ fn extract_frontmatter_title(source: &str) -> Option<String> {
extract_fm_field(source, "title")
}
fn extract_fm_field(source: &str, key: &str) -> Option<String> {
let source = source.strip_prefix('\u{feff}').unwrap_or(source);
let lines: Vec<&str> = source.lines().collect();
if lines.first().map(|l| l.trim()) != Some("---") {
return None;
}
let prefix = format!("{key}:");
for line in lines.iter().skip(1) {
let t = line.trim();
if t == "---" {
break;
}
if let Some(rest) = t.strip_prefix(&prefix) {
return Some(rest.trim().trim_matches('"').to_string());
}
}
None
}
/// `verified:\n by: foo` → Some("foo")
fn extract_nested_by(source: &str, block: &str) -> Option<String> {
let source = source.strip_prefix('\u{feff}').unwrap_or(source);
let lines: Vec<&str> = source.lines().collect();
if lines.first().map(|l| l.trim()) != Some("---") {
return None;
}
let mut in_block = false;
for line in lines.iter().skip(1) {
let t = line.trim_end();
if t.trim() == "---" {
break;
}
if t.trim() == format!("{block}:") || t.trim().starts_with(&format!("{block}:")) {
// inline `verified: { by: x }` not supported; block form:
if t.trim() == format!("{block}:") {
in_block = true;
continue;
}
}
if in_block {
let trimmed = t.trim();
if trimmed.starts_with("by:") {
return Some(
trimmed
.trim_start_matches("by:")
.trim()
.trim_matches('"')
.to_string(),
);
}
if !trimmed.is_empty() && !line.starts_with(' ') && !line.starts_with('\t') {
break;
}
}
}
None
}
fn extract_fm_list(source: &str, key: &str) -> Vec<String> {
let source = source.strip_prefix('\u{feff}').unwrap_or(source);
let lines: Vec<&str> = source.lines().collect();
if lines.first().map(|l| l.trim()) != Some("---") {
return Vec::new();
}
let mut out = Vec::new();
let mut in_list = false;
let header = format!("{key}:");
for line in lines.iter().skip(1) {
let t = line.trim_end();
if t.trim() == "---" {
break;
}
if t.trim() == header || t.trim() == format!("{key}: []") {
if t.trim().ends_with("[]") {
return Vec::new();
}
in_list = true;
continue;
}
if in_list {
let trimmed = t.trim();
if let Some(rest) = trimmed.strip_prefix("- ") {
out.push(rest.trim().trim_matches('"').to_string());
} else if !trimmed.is_empty() && !line.starts_with(' ') && !line.starts_with('\t') {
break;
}
}
}
out
}
fn yaml_escape(s: &str) -> String {
if s.is_empty()
|| s.contains(':')
|| s.contains('#')
|| s.contains('"')
|| s.contains('\'')
|| s.contains('\n')
|| s.starts_with(' ')
{
format!("\"{}\"", s.replace('\\', "\\\\").replace('"', "\\\""))
} else {
s.to_string()
}
}
fn import_path(imp: &str) -> &str {
imp.split(" as ").next().unwrap_or(imp).trim()