anvil_zksync_core/formatter/mod.rs
1//! Format various types of transaction data in a human-readable format for
2//! displaying in the anvil-zksync output.
3
4pub mod address;
5pub mod errors;
6pub mod log;
7pub mod pubdata_bytes;
8pub mod transaction;
9pub mod util;
10
11use std::fmt::Write;
12
13/// Trait for types that can be formatted in a human-readable way in the
14/// anvil-zksync output log.
15pub trait PrettyFmt: std::fmt::Debug {
16 /// Formats the value in a pretty, colorful, and human-readable way, writing
17 /// to the provided writer.
18 fn pretty_fmt(&self, writer: &mut impl Write) -> std::fmt::Result;
19}
20
21/// Extension trait that provides a convenient method to get a pretty-formatted string.
22///
23/// This trait is automatically implemented for all types that implement `PrettyFmt`.
24pub trait ToPrettyString {
25 /// Converts the value to a pretty-formatted string.
26 fn to_string_pretty(&self) -> String;
27}
28
29impl<T> ToPrettyString for T
30where
31 T: PrettyFmt,
32{
33 fn to_string_pretty(&self) -> String {
34 let mut result = String::new();
35 if let Err(e) = self.pretty_fmt(&mut result) {
36 tracing::warn!(err = ?e, origin = ?self, "Error: Failed to pretty print.");
37 }
38 result
39 }
40}