Skip to main content

trellis_testing/
output_ledger_dump.rs

1use core::fmt::{Debug, Write};
2
3use crate::OutputLedger;
4
5impl<O> OutputLedger<O> {
6    /// Returns deterministic debug output for output ledger snapshots.
7    pub fn to_debug_string(&self) -> String
8    where
9        O: Debug,
10    {
11        self.to_redacted_debug_string(|value| format!("{value:?}"))
12    }
13
14    /// Returns deterministic redacted debug output for output ledger snapshots.
15    pub fn to_redacted_debug_string(&self, redact: impl Fn(&O) -> String) -> String {
16        let mut out = String::new();
17        writeln!(&mut out, "OutputLedger").expect("writing to String cannot fail");
18
19        writeln!(&mut out, "Outputs:").expect("writing to String cannot fail");
20        for (key, snapshot) in &self.outputs {
21            let state = snapshot
22                .state
23                .as_ref()
24                .map(&redact)
25                .unwrap_or_else(|| "None".to_owned());
26            writeln!(
27                &mut out,
28                "  key={key:?} scope={:?} tx={:?} revision={:?} cleared={} state={}",
29                snapshot.scope, snapshot.transaction_id, snapshot.revision, snapshot.cleared, state
30            )
31            .expect("writing to String cannot fail");
32        }
33
34        writeln!(&mut out, "Closed scopes: {:?}", self.closed_scopes)
35            .expect("writing to String cannot fail");
36        writeln!(&mut out, "Errors: {:?}", self.errors).expect("writing to String cannot fail");
37        out
38    }
39}