- 新建 audit/utils.rs(39行 risk_str/risk_from_str/truncate_chars) + audit/diff.rs(29行 build_write_file_diff) - audit/mod.rs 416→379: mod utils/diff + re-export(pub(crate) use risk_str/risk_from_str) + 删原fn audit拆分完成: reason/restore/finalize/cache/data_change/utils/diff七子模块, mod.rs余379(process_tool_calls主+DTO+list_tool_executions) 主代grep核验(不cargo--workspace避anthropic_compat.rs其他会话中间态df-ai E0599): mod.rs mod utils/diff印证; agent隔离自验cargo 0+test 98 git add指定(audit/*, 不含anthropic_compat.rs/docs其他会话改动)
40 lines
1.5 KiB
Rust
40 lines
1.5 KiB
Rust
//! audit 共享纯 helper:RiskLevel ↔ 字符串转换 + 字符串安全截断。
|
||
//!
|
||
//! 第五批从 audit/mod.rs 抽离,行为零变更:
|
||
//! - `risk_str` / `risk_from_str`:finalize / restore 通过 `super::` 引用(pub(crate))。
|
||
//! - `truncate_chars`:仅 mod.rs 的 list_tool_executions DTO 装配用(pub(super))。
|
||
//!
|
||
//! re-export 维持原路径透明:`super::risk_str` / `super::risk_from_str` / `super::truncate_chars`
|
||
//! 在 audit/mod.rs 内通过 `use` 引入后裸名可用,外部子模块的 `super::risk_*` 引用不变。
|
||
|
||
use df_ai::ai_tools::RiskLevel;
|
||
|
||
/// RiskLevel → 审计记录字符串(low/medium/high)
|
||
pub(crate) fn risk_str(r: RiskLevel) -> &'static str {
|
||
match r {
|
||
RiskLevel::Low => "low",
|
||
RiskLevel::Medium => "medium",
|
||
RiskLevel::High => "high",
|
||
}
|
||
}
|
||
|
||
/// 审计记录字符串 → RiskLevel(启动重建 pending_approvals 用,未知串返回 None 跳过)
|
||
pub(crate) fn risk_from_str(s: &str) -> Option<RiskLevel> {
|
||
match s {
|
||
"low" => Some(RiskLevel::Low),
|
||
"medium" => Some(RiskLevel::Medium),
|
||
"high" => Some(RiskLevel::High),
|
||
_ => None,
|
||
}
|
||
}
|
||
|
||
/// 截断字符串到 max 字符(按 char_indices 边界切,避免切坏中文/emoji)
|
||
pub(super) fn truncate_chars(s: &str, max: usize) -> String {
|
||
if s.chars().count() <= max {
|
||
return s.to_string();
|
||
}
|
||
let mut out: String = s.chars().take(max).collect();
|
||
out.push('…');
|
||
out
|
||
}
|