Skip to main content

ic_host_process/tool/diagnostics/
mod.rs

1//! Explicit captured-evidence rendering; ordinary error display stays unchanged.
2
3#[cfg(test)]
4mod tests;
5
6use crate::tool::ToolError;
7use std::fmt::Write as _;
8
9/// Format bounded process output and cleanup evidence with caller-selected context.
10///
11/// Shared error display omits captured bytes; callers use this projection when
12/// their diagnostic contract needs the original status, streams and cleanup errors.
13/// Stream bytes use lossy UTF-8 with trailing whitespace trimmed. This is a raw
14/// diagnostic projection, not terminal escaping, redaction or output admission.
15#[must_use]
16pub fn format_failure_evidence(context: &str, error: &ToolError) -> String {
17    let mut message = format!("{context}: {error}");
18    if let Some(evidence) = error.evidence() {
19        let _ = write!(
20            message,
21            "\nstatus: {:?}\nstdout:\n{}\nstderr:\n{}",
22            evidence.status,
23            String::from_utf8_lossy(&evidence.stdout).trim_end(),
24            String::from_utf8_lossy(&evidence.stderr).trim_end(),
25        );
26    }
27    message
28}