返回 CodeWhale
receipts.rs
根目录 / crates / tui / src / commands / groups / debug / receipts.rs
1 //! Portable `/receipts` parsing and rendering. The host reads the authoritative
2 //! session/approval records and projects their shared receipt shape.
3
4 use super::CommandResult;
5 use crate::diagnostics_reports::receipts::{render_json_block, render_markdown};
6 use codewhale_command_contract::facets::DebugReceiptError;
7 use codewhale_command_contract::handler::{CommandCapabilities, CommandContexts, CommandHandler};
8 use codewhale_command_contract::metadata::{CommandInfo, RegisterCommand};
9
10 pub(in crate::commands) struct ReceiptsCmd;
11 impl RegisterCommand<CommandResult> for ReceiptsCmd {
12 fn info() -> &'static CommandInfo {
13 &CommandInfo {
14 name: "receipts",
15 aliases: &["receipt"],
16 usage: "/receipts [json] [<turn>]",
17 description_key: "cmd_receipts_description",
18 }
19 }
20 fn handler() -> CommandHandler<CommandResult> {
21 CommandHandler::Contextual {
22 capabilities: CommandCapabilities::DEBUG_RECEIPTS,
23 handler: receipts,
24 }
25 }
26 }
27
28 pub(super) fn receipts(contexts: CommandContexts<'_>, arg: Option<&str>) -> CommandResult {
29 let mut parts = contexts.into_parts();
30 let Some(receipts) = parts.debug_receipts.as_deref_mut() else {
31 return CommandResult::error("Command capability unavailable: debug_receipts");
32 };
33 let mut json = false;
34 let mut turn: Option<&str> = None;
35 for word in arg.unwrap_or_default().split_whitespace() {
36 match word {
37 "json" => json = true,
38 word if word.parse::<usize>().is_ok() => turn = Some(word),
39 other => {
40 return CommandResult::error(format!(
41 "Unknown argument '{other}'. Use /receipts [json] [<turn>]."
42 ));
43 }
44 }
45 }
46 match receipts.receipt(turn) {
47 Ok(receipt) if json => CommandResult::message(render_json_block(&receipt)),
48 Ok(receipt) => CommandResult::message(render_markdown(&receipt).trim_end().to_string()),
49 Err(DebugReceiptError::ApprovalLog(error)) => CommandResult::error(format!(
50 "Could not read this session's approval log: {error}"
51 )),
52 Err(DebugReceiptError::Build(error)) => CommandResult::error(error),
53 }
54 }
55
55 lines RUST