Skip to main content

uts_core/codec/v1/timestamp/
fmt.rs

1use super::*;
2use crate::utils::Hexed;
3use core::fmt;
4
5impl<A: Allocator + Clone> fmt::Display for Timestamp<A> {
6    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
7        self.fmt_recurse(None, f, 0, true)
8    }
9}
10
11impl<A: Allocator + Clone> Timestamp<A> {
12    pub(crate) fn fmt(&self, input: Option<&[u8]>, f: &mut fmt::Formatter) -> fmt::Result {
13        let input = match input {
14            Some(input) => Some(input),
15            None => match self {
16                Self::Step(step) => step.input.get().map(|v| v.as_slice()),
17                Self::Attestation(_) => None,
18            },
19        };
20        self.fmt_recurse(input, f, 0, true)
21    }
22
23    fn fmt_recurse(
24        &self,
25        input: Option<&[u8]>,
26        f: &mut fmt::Formatter,
27        depth: usize,
28        first_line: bool,
29    ) -> fmt::Result {
30        fn indent(f: &mut fmt::Formatter, depth: usize, first_line: bool) -> fmt::Result {
31            if depth == 0 {
32                return Ok(());
33            }
34
35            for _ in 0..depth - 1 {
36                f.write_str("    ")?;
37            }
38            if first_line {
39                f.write_str("--->")?;
40            } else {
41                f.write_str("    ")?;
42            }
43            Ok(())
44        }
45
46        indent(f, depth, first_line)?;
47        match self {
48            Self::Step(step) if step.op == OpCode::FORK => {
49                writeln!(f, "fork")?;
50                for child in &step.next {
51                    child.fmt_recurse(input, f, depth + 1, true)?;
52                }
53                Ok(())
54            }
55            Self::Step(step) => {
56                let op = step.op;
57                if op.has_immediate() {
58                    writeln!(f, "execute {op} {}", Hexed(&step.data))?;
59                } else {
60                    writeln!(f, "execute {op}")?;
61                }
62
63                let result = if let Some(value) = step.next.first().and_then(|next| next.input()) {
64                    Some(SliceExt::to_vec_in(value, step.allocator().clone()))
65                } else if let Some(input) = input {
66                    let result = op.execute_in(input, &step.data, step.allocator().clone());
67                    indent(f, depth, false)?;
68                    writeln!(f, " result {}", Hexed(&result))?;
69                    Some(result)
70                } else {
71                    None
72                };
73
74                for child in &step.next {
75                    child.fmt_recurse(result.as_deref(), f, depth, false)?;
76                }
77                Ok(())
78            }
79            Self::Attestation(attestation) => {
80                writeln!(f, "result attested by {attestation}")
81            }
82        }
83    }
84}