Files
marqdo/src/host/time.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

91 lines
2.6 KiB
Rust

//! Time host primitives.
use crate::host::as_text;
use std::time::Duration;
#[cfg(not(target_arch = "wasm32"))]
use std::time::{SystemTime, UNIX_EPOCH};
use chrono::{DateTime, NaiveDateTime, Utc};
use crate::host::HostContext;
use crate::value::Value;
pub fn now_unix() -> Result<Value, String> {
#[cfg(target_arch = "wasm32")]
{
// No wall clock on wasm32-unknown-unknown without JS bridge (C4).
return Err("now_unix unavailable in browser wasm (no clock bridge yet)".into());
}
#[cfg(not(target_arch = "wasm32"))]
{
let secs = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_err(|e| format!("now_unix: {e}"))?
.as_secs();
Ok(Value::Int(secs as i64))
}
}
pub fn now_ms() -> Result<Value, String> {
#[cfg(target_arch = "wasm32")]
{
return Err("now_ms unavailable in browser wasm (no clock bridge yet)".into());
}
#[cfg(not(target_arch = "wasm32"))]
{
let ms = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_err(|e| format!("now_ms: {e}"))?
.as_millis();
Ok(Value::Int(ms as i64))
}
}
fn as_i64(v: &Value, label: &str) -> Result<i64, String> {
match v {
Value::Int(n) => Ok(*n),
_ => Err(format!("{label} must be int")),
}
}
pub fn format_time(unix: &Value, pattern: &Value) -> Result<Value, String> {
let secs = as_i64(unix, "unix")?;
let pat = as_text(pattern, "pattern")?;
let dt = DateTime::<Utc>::from_timestamp(secs, 0)
.ok_or_else(|| format!("invalid unix timestamp: {secs}"))?;
Ok(Value::Text(dt.format(pat).to_string()))
}
pub fn parse_time(text: &Value, pattern: &Value) -> Result<Value, String> {
let t = as_text(text, "text")?;
let pat = as_text(pattern, "pattern")?;
let dt = NaiveDateTime::parse_from_str(t, pat).map_err(|e| format!("parse_time: {e}"))?;
Ok(Value::Int(dt.and_utc().timestamp()))
}
pub fn sleep_ms(ctx: &HostContext, ms: &Value) -> Result<Value, String> {
let n = as_i64(ms, "ms")?;
if n < 0 {
return Err("sleep_ms must be >= 0".into());
}
let n = n as u64;
if let Some(lim) = ctx.sleep_limit_ms {
if n > lim {
return Err(format!("sleep_ms {n} exceeds limit {lim}"));
}
}
if n > 0 {
#[cfg(not(target_arch = "wasm32"))]
{
std::thread::sleep(Duration::from_millis(n));
}
#[cfg(target_arch = "wasm32")]
{
let _ = Duration::from_millis(n);
// Browser wasm has no blocking sleep; ignore (C0/C1).
}
}
Ok(Value::None)
}