fix(cli): 程序名占位符在构造时渲染,两条出口(人类可读 / --json)不再分叉(M82 r3 复审 P2)

r2 只在人类可读通道渲染 `%prog%`,JSON 两条出口(错误信封的 message/hint、Report 的
warnings)直接序列化未渲染文本,于是 `--json lst` 吐出 `"hint":"%prog% --help 查看命令列表"`
——连 `ull` 自己的 JSON 都被这轮改动弄坏了。

改成**构造时渲染**,一个入口管住所有出口:
- `CliError::usage/user/internal/with_hint` 统一过 `render()`(错误只有这一处成形);
- `Report::new` 渲染 human 行,`Outcome::with_warnings` 渲染 warnings;
- `print_report` / `print_error` 不再各自渲染(渲染点只有一个,不会再有第三条出口漏掉)。

回归测试三条:`json_error_envelope_never_leaks_the_placeholder`、
`json_warnings_never_leak_the_placeholder`、`human_lines_render_the_placeholder_too`
(后者同时断言「账户名恰好叫 ull」这种用户数据不被改)。
This commit is contained in:
AskaEth committed 2026-11-02 08:08:16 +08:00
1 parent 2f8bbd1746
commit 8ca1e1e965
2 files changed
+75 -13

No files matched your search

+12 -4
View File
@@ -56,6 +56,14 @@ pub struct CliError {
hint: Option<String>,
}
/// 把模板里的程序名占位符渲染掉(M82 r3)。
///
/// 渲染放在**构造时**,而不是打印时:错误有两条出口(人类可读 stderr、`--json` 信封),
/// 只在打印处渲染会漏掉 JSON 那条(r2 就是这么漏的,`%prog%` 直接出现在了 JSON 里)。
fn render(text: impl Into<String>) -> String {
crate::cli::program::fill(&text.into())
}
impl CliError {
/// 用法错误(参数不对)。
#[must_use]
@@ -63,7 +71,7 @@ impl CliError {
Self {
kind: ErrorKind::User,
code: "usage",
message: message.into(),
message: render(message),
hint: None,
}
}
@@ -74,7 +82,7 @@ impl CliError {
Self {
kind: ErrorKind::User,
code,
message: message.into(),
message: render(message),
hint: None,
}
}
@@ -85,7 +93,7 @@ impl CliError {
Self {
kind: ErrorKind::Internal,
code,
message: message.into(),
message: render(message),
hint: None,
}
}
@@ -99,7 +107,7 @@ impl CliError {
/// 补一句提示。
#[must_use]
pub fn with_hint(mut self, hint: impl Into<String>) -> Self {
self.hint = Some(hint.into());
self.hint = Some(render(hint));
self
}