按审计结论清理冗余(不改变行为):
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 分别建表并由实测覆盖。
103 lines
2.6 KiB
Rust
103 lines
2.6 KiB
Rust
//! Regex host primitives for Mid M1 (`lib/re`).
|
|
|
|
use crate::host::as_text;
|
|
use crate::value::Value;
|
|
use regex::Regex;
|
|
|
|
|
|
fn compile(pattern: &str) -> Result<Regex, String> {
|
|
Regex::new(pattern).map_err(|e| format!("invalid regex: {e}"))
|
|
}
|
|
|
|
pub fn is_match(text: &Value, pattern: &Value) -> Result<Value, String> {
|
|
let t = as_text(text, "text")?;
|
|
let p = as_text(pattern, "pattern")?;
|
|
let re = compile(p)?;
|
|
Ok(Value::Bool(re.is_match(t)))
|
|
}
|
|
|
|
pub fn find(text: &Value, pattern: &Value) -> Result<Value, String> {
|
|
let t = as_text(text, "text")?;
|
|
let p = as_text(pattern, "pattern")?;
|
|
let re = compile(p)?;
|
|
Ok(match re.find(t) {
|
|
Some(m) => Value::Text(m.as_str().to_string()),
|
|
None => Value::None,
|
|
})
|
|
}
|
|
|
|
pub fn find_all(text: &Value, pattern: &Value) -> Result<Value, String> {
|
|
let t = as_text(text, "text")?;
|
|
let p = as_text(pattern, "pattern")?;
|
|
let re = compile(p)?;
|
|
let xs: Vec<Value> = re
|
|
.find_iter(t)
|
|
.map(|m| Value::Text(m.as_str().to_string()))
|
|
.collect();
|
|
Ok(Value::List(xs))
|
|
}
|
|
|
|
pub fn replace(
|
|
text: &Value,
|
|
pattern: &Value,
|
|
with: &Value,
|
|
count: Option<&Value>,
|
|
) -> Result<Value, String> {
|
|
let t = as_text(text, "text")?;
|
|
let p = as_text(pattern, "pattern")?;
|
|
let w = as_text(with, "with")?;
|
|
let re = compile(p)?;
|
|
let limit = match count {
|
|
None | Some(Value::None) => None,
|
|
Some(Value::Int(n)) => {
|
|
if *n < 0 {
|
|
return Err("count must be non-negative".into());
|
|
}
|
|
Some(*n as usize)
|
|
}
|
|
Some(_) => return Err("count needs int".into()),
|
|
};
|
|
let out = match limit {
|
|
None => re.replace_all(t, w).into_owned(),
|
|
Some(0) => t.to_string(),
|
|
Some(n) => re.replacen(t, n, w).into_owned(),
|
|
};
|
|
Ok(Value::Text(out))
|
|
}
|
|
|
|
pub fn split(text: &Value, pattern: &Value) -> Result<Value, String> {
|
|
let t = as_text(text, "text")?;
|
|
let p = as_text(pattern, "pattern")?;
|
|
let re = compile(p)?;
|
|
let xs: Vec<Value> = re
|
|
.split(t)
|
|
.map(|s| Value::Text(s.to_string()))
|
|
.collect();
|
|
Ok(Value::List(xs))
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn match_and_find() {
|
|
assert_eq!(
|
|
is_match(
|
|
&Value::Text("abc123".into()),
|
|
&Value::Text(r"\d+".into())
|
|
)
|
|
.unwrap(),
|
|
Value::Bool(true)
|
|
);
|
|
assert_eq!(
|
|
find(
|
|
&Value::Text("ab12cd34".into()),
|
|
&Value::Text(r"\d+".into())
|
|
)
|
|
.unwrap(),
|
|
Value::Text("12".into())
|
|
);
|
|
}
|
|
}
|