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={:?} transition={:?}",
47                redactor.resource_key(&command.key),
48                command.scope,
49                command.kind,
50                command.transition
51            )
52            .expect("writing to String cannot fail");
53        }
54
55        writeln!(&mut out, "Status:").expect("writing to String cannot fail");
56        for record in &self.status_records {
57            writeln!(
58                &mut out,
59                "  class={:?} key={:?} scope={:?} command_revision={:?} status_revision={:?} outcome={}",
60                record.class,
61                redactor.resource_key(&record.status.resource_key),
62                record.status.scope,
63                record.status.command_revision,
64                record.status.status_revision,
65                status_kind(&record.status.status)
66            )
67            .expect("writing to String cannot fail");
68        }
69
70        out
71    }
72}
73
74fn status_kind(status: &HostResourceOutcome) -> &'static str {
75    match status {
76        HostResourceOutcome::Unknown => "Unknown",
77        HostResourceOutcome::Open => "Open",
78        HostResourceOutcome::Failed(_) => "Failed",
79        HostResourceOutcome::Closed => "Closed",
80        HostResourceOutcome::Unsupported(_) => "Unsupported",
81    }
82}