Migrate fmt to use Display trait (#44)

Migrate fmt to use Display trait to simplify the usage of it.

Also with this, in the same snipped of code we can print types from
backend, middleware and frontend; which before needed to import the two
different `Printer` structs (frontend::Printer and mock_main::Printer).

Before:
```
let printer = mock_main::Printer { skip_none: false };
let mut w = io::stdout();
printer.fmt_mock_main_pod(&mut w, &pod).unwrap();

let printer = frontend::Printer { skip_none: false };
printer.fmt_main_pod_builder(&mut w, &pod_builder).unwrap();
```

now:
```
println!("{:#}", pod);
println!("{:#}", pod_builder);
```

And the equivalent to the old `skip_none: true`, now is done by just
using `println!("{}", pod);
This commit is contained in:
arnaucube 2025-02-10 12:37:11 +01:00 committed by GitHub
parent f23a78dcb8
commit 5236b46214
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 176 additions and 173 deletions

View file

@ -293,6 +293,16 @@ pub enum StatementArg {
Key(AnchoredKey),
}
impl fmt::Display for StatementArg {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
StatementArg::None => write!(f, "none"),
StatementArg::Literal(v) => write!(f, "{}", v),
StatementArg::Key(r) => write!(f, "{}.{}", r.0, r.1),
}
}
}
impl StatementArg {
pub fn is_none(&self) -> bool {
matches!(self, Self::None)
@ -332,6 +342,21 @@ impl ToFields for StatementArg {
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Statement(pub NativeStatement, pub Vec<StatementArg>);
impl fmt::Display for Statement {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?} ", self.0)?;
for (i, arg) in self.1.iter().enumerate() {
if !(!f.alternate() && arg.is_none()) {
if i != 0 {
write!(f, " ")?;
}
write!(f, "{}", arg)?;
}
}
Ok(())
}
}
impl Statement {
pub fn is_none(&self) -> bool {
matches!(self.0, NativeStatement::None)