Skip to main content

trellis_testing/
resource_ledger_dump.rs

1use core::fmt::Write;
2
3use trellis_core::HostResourceOutcome;
4
5use crate::{ResourceLedger, TraceRedactor};
6
7impl<C> ResourceLedger<C> {
8    /// Returns deterministic redacted debug output for resource ledger snapshots.
9    pub fn to_redacted_debug_string(&self, redactor: &impl TraceRedactor) -> String {
10        let mut out = String::new();
11        writeln!(&mut out, "ResourceLedger").expect("writing to String cannot fail");
12
13        writeln!(&mut out, "Live:").expect("writing to String cannot fail");
14        for (key, snapshot) in &self.resources {
15            writeln!(
16                &mut out,
17                "  key={:?} owners={:?} open={} close={} replace={} revision={:?} generation={}",
18                redactor.resource_key(key),
19                snapshot.owners,
20                snapshot.open_count,
21                snapshot.close_count,
22                snapshot.replace_count,
23                snapshot.command_revision,
24                snapshot.generation
25            )
26            .expect("writing to String cannot fail");
27        }
28
29        writeln!(&mut out, "History:").expect("writing to String cannot fail");
30        for (key, snapshot) in &self.history {
31            writeln!(
32                &mut out,
33                "  key={:?} owners={:?} revision={:?} generation={}",
34                redactor.resource_key(key),
35                snapshot.owners,
36                snapshot.command_revision,
37                snapshot.generation
38            )
39            .expect("writing to String cannot fail");
40        }
41
42        writeln!(&mut out, "Commands:").expect("writing to String cannot fail");
43        for command in &self.command_trace {
44            writeln!(
45                &mut out,
46                "  key={:?} scope={:?} kind={:?}",
47                redactor.resource_key(&command.key),
48                command.scope,
49                command.kind
50            )
51            .expect("writing to String cannot fail");
52        }
53
54        writeln!(&mut out, "Status:").expect("writing to String cannot fail");
55        for record in &self.status_records {
56            writeln!(
57                &mut out,
58                "  class={:?} key={:?} scope={:?} command_revision={:?} status_revision={:?} outcome={}",
59                record.class,
60                redactor.resource_key(&record.status.resource_key),
61                record.status.scope,
62                record.status.command_revision,
63                record.status.status_revision,
64                status_kind(&record.status.status)
65            )
66            .expect("writing to String cannot fail");
67        }
68
69        out
70    }
71}
72
73fn status_kind(status: &HostResourceOutcome) -> &'static str {
74    match status {
75        HostResourceOutcome::Unknown => "Unknown",
76        HostResourceOutcome::Open => "Open",
77        HostResourceOutcome::Failed(_) => "Failed",
78        HostResourceOutcome::Closed => "Closed",
79        HostResourceOutcome::Unsupported(_) => "Unsupported",
80    }
81}