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:
1 parent
6ae0d39e5a
commit
8b0ea9ed7a
21 files changed
+101
-377
No files matched your search
+1
-6
@@ -1,13 +1,8 @@
|
||||
//! Minimal RFC4180 CSV for Mid M4 (`lib/csv`).
|
||||
|
||||
use crate::host::as_text;
|
||||
use crate::value::Value;
|
||||
|
||||
fn as_text<'a>(v: &'a Value, label: &str) -> Result<&'a str, String> {
|
||||
match v {
|
||||
Value::Text(s) => Ok(s.as_str()),
|
||||
_ => Err(format!("{label} needs text")),
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse CSV text into a list of maps (first row = headers).
|
||||
pub fn parse(text: &Value) -> Result<Value, String> {
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
//! Moments are plain maps: `{unix, zone, iso}`. Zone is a fixed offset (`+08:00`)
|
||||
//! or alias (`UTC`); a few fixed-offset IANA names are accepted. Full tzdb is out of scope.
|
||||
|
||||
use crate::host::as_text;
|
||||
use chrono::{DateTime, Duration, FixedOffset, NaiveDate, NaiveDateTime, TimeZone, Utc};
|
||||
|
||||
use crate::value::Value;
|
||||
@@ -14,12 +15,6 @@ fn as_i64(v: &Value, label: &str) -> Result<i64, String> {
|
||||
}
|
||||
}
|
||||
|
||||
fn as_text<'a>(v: &'a Value, label: &str) -> Result<&'a str, String> {
|
||||
match v {
|
||||
Value::Text(s) => Ok(s.as_str()),
|
||||
_ => Err(format!("{label} must be text")),
|
||||
}
|
||||
}
|
||||
|
||||
fn optional_i64(v: Option<&Value>, label: &str) -> Result<i64, String> {
|
||||
match v {
|
||||
|
||||
@@ -1,14 +1,9 @@
|
||||
//! Base64 / hex encoding for Mid M1 (`lib/encoding`).
|
||||
//! No third-party crates — standard alphabet + padding.
|
||||
|
||||
use crate::host::as_text;
|
||||
use crate::value::Value;
|
||||
|
||||
fn as_text<'a>(v: &'a Value, label: &str) -> Result<&'a str, String> {
|
||||
match v {
|
||||
Value::Text(s) => Ok(s.as_str()),
|
||||
_ => Err(format!("{label} needs text")),
|
||||
}
|
||||
}
|
||||
|
||||
const B64: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
|
||||
|
||||
+1
-6
@@ -3,6 +3,7 @@
|
||||
//! F0: safe one-shot execution + structured `invoke` records.
|
||||
//! Later phases add language registry, JSON bridge, parallel fan-out and sessions.
|
||||
|
||||
use crate::host::as_text;
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
use std::io::{Read, Write};
|
||||
use std::panic::{catch_unwind, AssertUnwindSafe};
|
||||
@@ -26,12 +27,6 @@ const MAX_OUTPUT_LIMIT: usize = 64 * 1024 * 1024;
|
||||
const INPUT_ENV_CAP: usize = 8 * 1024;
|
||||
const FAILURE_TAIL: usize = 4096;
|
||||
|
||||
fn as_text<'a>(v: &'a Value, what: &str) -> Result<&'a str, String> {
|
||||
match v {
|
||||
Value::Text(s) => Ok(s.as_str()),
|
||||
_ => Err(format!("{what} must be text")),
|
||||
}
|
||||
}
|
||||
|
||||
fn opt_text(v: Option<&Value>) -> Result<Option<String>, String> {
|
||||
match v {
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
//! Protocol: one JSON request per stdin line, one JSON response per original
|
||||
//! stdout line. User stdout/stderr are captured inside the bootstrap.
|
||||
|
||||
use crate::host::as_text;
|
||||
use std::io::{BufRead, BufReader, Read, Write};
|
||||
use std::process::{Child, Command, Stdio};
|
||||
use std::sync::mpsc::{self, Receiver};
|
||||
@@ -171,12 +172,6 @@ impl Drop for Session {
|
||||
}
|
||||
}
|
||||
|
||||
fn as_text<'a>(v: &'a Value, what: &str) -> Result<&'a str, String> {
|
||||
match v {
|
||||
Value::Text(s) => Ok(s.as_str()),
|
||||
_ => Err(format!("{what} must be text")),
|
||||
}
|
||||
}
|
||||
|
||||
fn opt_text(v: Option<&Value>) -> Result<Option<String>, String> {
|
||||
match v {
|
||||
|
||||
+1
-6
@@ -1,5 +1,6 @@
|
||||
//! Digest / HMAC host primitives for Mid M3 (`lib/hash`).
|
||||
|
||||
use crate::host::as_text;
|
||||
use hmac::{Hmac, Mac};
|
||||
use md5::Md5;
|
||||
use sha1::Sha1;
|
||||
@@ -9,12 +10,6 @@ use crate::value::Value;
|
||||
|
||||
type HmacSha256 = Hmac<Sha256>;
|
||||
|
||||
fn as_text<'a>(v: &'a Value, label: &str) -> Result<&'a str, String> {
|
||||
match v {
|
||||
Value::Text(s) => Ok(s.as_str()),
|
||||
_ => Err(format!("{label} needs text")),
|
||||
}
|
||||
}
|
||||
|
||||
fn to_hex(bytes: &[u8]) -> String {
|
||||
bytes.iter().map(|b| format!("{b:02x}")).collect()
|
||||
|
||||
@@ -1,13 +1,8 @@
|
||||
//! HTML escape / unescape for Mid2 M8 (`lib/html`).
|
||||
|
||||
use crate::host::as_text;
|
||||
use crate::value::Value;
|
||||
|
||||
fn as_text<'a>(v: &'a Value, label: &str) -> Result<&'a str, String> {
|
||||
match v {
|
||||
Value::Text(s) => Ok(s.as_str()),
|
||||
_ => Err(format!("{label} must be text")),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn escape(text: &Value) -> Result<Value, String> {
|
||||
let s = as_text(text, "text")?;
|
||||
|
||||
+1
-6
@@ -1,5 +1,6 @@
|
||||
//! Log level gate for Mid M6 (`lib/log`).
|
||||
|
||||
use crate::host::as_text;
|
||||
use crate::host::HostContext;
|
||||
use crate::value::Value;
|
||||
|
||||
@@ -32,12 +33,6 @@ impl Level {
|
||||
}
|
||||
}
|
||||
|
||||
fn as_text<'a>(v: &'a Value, label: &str) -> Result<&'a str, String> {
|
||||
match v {
|
||||
Value::Text(s) => Ok(s.as_str()),
|
||||
_ => Err(format!("{label} needs text")),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_level(ctx: &mut HostContext, level: &Value) -> Result<Value, String> {
|
||||
let s = as_text(level, "level")?;
|
||||
|
||||
@@ -49,6 +49,17 @@ mod sys;
|
||||
pub mod writeback;
|
||||
|
||||
pub use dispatch::{call_host, HostFn};
|
||||
|
||||
/// Shared `text` argument extraction for host modules.
|
||||
///
|
||||
/// `<TAB>` slimming: this used to be copy-pasted in 17 host modules with two
|
||||
/// different wordings; one implementation keeps messages consistent.
|
||||
pub fn as_text<'a>(v: &'a crate::value::Value, label: &str) -> Result<&'a str, String> {
|
||||
match v {
|
||||
crate::value::Value::Text(s) => Ok(s.as_str()),
|
||||
_ => Err(format!("{label} needs text")),
|
||||
}
|
||||
}
|
||||
pub use fs::path_under_root;
|
||||
pub use plugin::PluginState;
|
||||
|
||||
|
||||
+1
-6
@@ -1,5 +1,6 @@
|
||||
//! HTTP host primitives (HTTP and HTTPS via ureq).
|
||||
|
||||
use crate::host::as_text;
|
||||
use std::io::{Read, Write};
|
||||
#[cfg(feature = "net-host")]
|
||||
use std::io::{BufRead, BufReader};
|
||||
@@ -10,12 +11,6 @@ use crate::value::Value;
|
||||
#[cfg(feature = "net-host")]
|
||||
use crate::host::HostContext;
|
||||
|
||||
fn as_text<'a>(v: &'a Value, label: &str) -> Result<&'a str, String> {
|
||||
match v {
|
||||
Value::Text(s) => Ok(s.as_str()),
|
||||
_ => Err(format!("{label} must be text")),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "net-host")]
|
||||
fn headers_from_value(headers: Option<&Value>) -> Result<Vec<(String, String)>, String> {
|
||||
|
||||
+1
-6
@@ -1,15 +1,10 @@
|
||||
//! 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 as_text<'a>(v: &'a Value, label: &str) -> Result<&'a str, String> {
|
||||
match v {
|
||||
Value::Text(s) => Ok(s.as_str()),
|
||||
_ => Err(format!("{label} needs text")),
|
||||
}
|
||||
}
|
||||
|
||||
fn path_to_text(p: &Path) -> String {
|
||||
if p.as_os_str().is_empty() {
|
||||
|
||||
+1
-6
@@ -1,5 +1,6 @@
|
||||
//! Native plugin loader (C ABI v1/v2). Shared libs are not linked into marqdo.
|
||||
|
||||
use crate::host::as_text;
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::collections::HashMap;
|
||||
use std::ffi::{CStr, CString};
|
||||
@@ -170,12 +171,6 @@ impl PluginState {
|
||||
}
|
||||
}
|
||||
|
||||
fn as_text<'a>(v: &'a Value, label: &str) -> Result<&'a str, String> {
|
||||
match v {
|
||||
Value::Text(s) => Ok(s.as_str()),
|
||||
_ => Err(format!("{label} must be text")),
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
fn malloc(n: usize) -> *mut c_void;
|
||||
|
||||
+1
-6
@@ -1,14 +1,9 @@
|
||||
//! Regex host primitives for Mid M1 (`lib/re`).
|
||||
|
||||
use crate::host::as_text;
|
||||
use crate::value::Value;
|
||||
use regex::Regex;
|
||||
|
||||
fn as_text<'a>(v: &'a Value, label: &str) -> Result<&'a str, String> {
|
||||
match v {
|
||||
Value::Text(s) => Ok(s.as_str()),
|
||||
_ => Err(format!("{label} needs text")),
|
||||
}
|
||||
}
|
||||
|
||||
fn compile(pattern: &str) -> Result<Regex, String> {
|
||||
Regex::new(pattern).map_err(|e| format!("invalid regex: {e}"))
|
||||
|
||||
+1
-6
@@ -4,6 +4,7 @@
|
||||
//! Quiet file children get `MARQDO_EVENT_FORWARD` so LLM stream events published in the
|
||||
//! child process are tailed into the parent EventBus while `wait` blocks (view SSE).
|
||||
|
||||
use crate::host::as_text;
|
||||
use std::collections::HashMap;
|
||||
use std::io::{Read, Seek, SeekFrom};
|
||||
use std::path::PathBuf;
|
||||
@@ -136,12 +137,6 @@ pub(crate) struct FunctionTask {
|
||||
handle: Option<JoinHandle<()>>,
|
||||
}
|
||||
|
||||
fn as_text<'a>(v: &'a Value, label: &str) -> Result<&'a str, String> {
|
||||
match v {
|
||||
Value::Text(s) => Ok(s.as_str()),
|
||||
_ => Err(format!("{label} must be text")),
|
||||
}
|
||||
}
|
||||
|
||||
fn as_code(v: &Value) -> Result<&CodeBlock, String> {
|
||||
match v {
|
||||
|
||||
+1
-6
@@ -1,17 +1,12 @@
|
||||
//! System / process host primitives.
|
||||
|
||||
use crate::host::as_text;
|
||||
#[cfg(feature = "exec-host")]
|
||||
use std::process::Command;
|
||||
|
||||
use crate::host::HostContext;
|
||||
use crate::value::Value;
|
||||
|
||||
fn as_text<'a>(v: &'a Value, label: &str) -> Result<&'a str, String> {
|
||||
match v {
|
||||
Value::Text(s) => Ok(s.as_str()),
|
||||
_ => Err(format!("{label} must be text")),
|
||||
}
|
||||
}
|
||||
|
||||
fn as_i64(v: &Value, label: &str) -> Result<i64, String> {
|
||||
match v {
|
||||
|
||||
@@ -1,13 +1,8 @@
|
||||
//! Text helpers for Mid M4 (`lib/text` thicken).
|
||||
|
||||
use crate::host::as_text;
|
||||
use crate::value::Value;
|
||||
|
||||
fn as_text<'a>(v: &'a Value, label: &str) -> Result<&'a str, String> {
|
||||
match v {
|
||||
Value::Text(s) => Ok(s.as_str()),
|
||||
_ => Err(format!("{label} needs text")),
|
||||
}
|
||||
}
|
||||
|
||||
fn as_usize(v: &Value, label: &str) -> Result<usize, String> {
|
||||
match v {
|
||||
|
||||
+1
-6
@@ -1,5 +1,6 @@
|
||||
//! Time host primitives.
|
||||
|
||||
use crate::host::as_text;
|
||||
use std::time::Duration;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
@@ -47,12 +48,6 @@ fn as_i64(v: &Value, label: &str) -> Result<i64, String> {
|
||||
}
|
||||
}
|
||||
|
||||
fn as_text<'a>(v: &'a Value, label: &str) -> Result<&'a str, String> {
|
||||
match v {
|
||||
Value::Text(s) => Ok(s.as_str()),
|
||||
_ => Err(format!("{label} must be text")),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn format_time(unix: &Value, pattern: &Value) -> Result<Value, String> {
|
||||
let secs = as_i64(unix, "unix")?;
|
||||
|
||||
@@ -2,15 +2,10 @@
|
||||
//! Subset: comments, keys, strings, ints, floats, bools, arrays of scalars,
|
||||
//! `[table]` / dotted tables. No datetime, inline tables, or array-of-tables.
|
||||
|
||||
use crate::host::as_text;
|
||||
use crate::host::json::json_to_value;
|
||||
use crate::value::Value;
|
||||
|
||||
fn as_text<'a>(v: &'a Value, label: &str) -> Result<&'a str, String> {
|
||||
match v {
|
||||
Value::Text(s) => Ok(s.as_str()),
|
||||
_ => Err(format!("{label} must be text")),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse(text: &Value) -> Result<Value, String> {
|
||||
let raw = as_text(text, "text")?;
|
||||
|
||||
+1
-6
@@ -2,14 +2,9 @@
|
||||
//! Distinct from `net::url_encode` (single-segment form encoding).
|
||||
//! Hand-rolled subset (no `url` crate): scheme://[userinfo@]host[:port]/path[?query][#frag].
|
||||
|
||||
use crate::host::as_text;
|
||||
use crate::value::Value;
|
||||
|
||||
fn as_text<'a>(v: &'a Value, label: &str) -> Result<&'a str, String> {
|
||||
match v {
|
||||
Value::Text(s) => Ok(s.as_str()),
|
||||
_ => Err(format!("{label} must be text")),
|
||||
}
|
||||
}
|
||||
|
||||
fn encode_component(s: &str) -> String {
|
||||
let mut out = String::new();
|
||||
|
||||
Reference in new issue
Block a user