Files
marqdo/src/host/path.rs
T
cflmy 8b0ea9ed7a 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 分别建表并由实测覆盖。
2026-10-22 15:39:18 +08:00

118 lines
3.2 KiB
Rust

//! Path algebra for Mid M2 (`lib/path`) — no I/O.
use crate::host::as_text;
use std::path::{Component, Path, PathBuf};
use crate::value::Value;
fn path_to_text(p: &Path) -> String {
if p.as_os_str().is_empty() {
".".into()
} else {
p.to_string_lossy().into_owned()
}
}
pub fn join(a: &Value, b: &Value) -> Result<Value, String> {
let a = as_text(a, "a")?;
let b = as_text(b, "b")?;
let out = Path::new(a).join(b);
Ok(Value::Text(path_to_text(&out)))
}
pub fn split(path: &Value) -> Result<Value, String> {
let p = as_text(path, "path")?;
let mut parts = Vec::new();
for c in Path::new(p).components() {
match c {
Component::RootDir | Component::CurDir | Component::ParentDir => {
parts.push(Value::Text(c.as_os_str().to_string_lossy().into_owned()));
}
Component::Prefix(prefix) => {
parts.push(Value::Text(prefix.as_os_str().to_string_lossy().into_owned()));
}
Component::Normal(s) => parts.push(Value::Text(s.to_string_lossy().into_owned())),
}
}
Ok(Value::List(parts))
}
pub fn file_name(path: &Value) -> Result<Value, String> {
let p = as_text(path, "path")?;
Ok(Value::Text(
Path::new(p)
.file_name()
.map(|s| s.to_string_lossy().into_owned())
.unwrap_or_default(),
))
}
pub fn parent(path: &Value) -> Result<Value, String> {
let p = as_text(path, "path")?;
let parent = Path::new(p).parent();
Ok(Value::Text(match parent {
Some(par) if !par.as_os_str().is_empty() => path_to_text(par),
_ => ".".into(),
}))
}
pub fn extension(path: &Value) -> Result<Value, String> {
let p = as_text(path, "path")?;
Ok(Value::Text(
Path::new(p)
.extension()
.map(|s| s.to_string_lossy().into_owned())
.unwrap_or_default(),
))
}
pub fn normalize(path: &Value) -> Result<Value, String> {
let p = as_text(path, "path")?;
let mut out = PathBuf::new();
for c in Path::new(p).components() {
match c {
Component::CurDir => {}
Component::ParentDir => {
match out.components().next_back() {
Some(Component::Normal(_)) => {
out.pop();
}
Some(Component::RootDir) | Some(Component::Prefix(_)) => {}
_ => out.push(".."),
}
}
other => out.push(other.as_os_str()),
}
}
Ok(Value::Text(path_to_text(&out)))
}
pub fn is_absolute(path: &Value) -> Result<Value, String> {
let p = as_text(path, "path")?;
Ok(Value::Bool(Path::new(p).is_absolute()))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn join_and_name() {
let j = join(
&Value::Text("a/b".into()),
&Value::Text("c.txt".into()),
)
.unwrap();
assert_eq!(j, Value::Text("a/b/c.txt".into()));
assert_eq!(
file_name(&Value::Text("a/b/c.txt".into())).unwrap(),
Value::Text("c.txt".into())
);
assert_eq!(
extension(&Value::Text("c.txt".into())).unwrap(),
Value::Text("txt".into())
);
}
}