use std::fmt;
use crate::observability::Inspect;
pub struct LaidOut<'a> {
of: &'a dyn Inspect,
}
pub trait InspectExt: Inspect {
fn inspect(&self) -> LaidOut<'_>
where
Self: Sized,
{
LaidOut { of: self }
}
}
impl<T: Inspect> InspectExt for T {}
impl fmt::Display for LaidOut<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
rows(f, self.of, 0)
}
}
fn rows(f: &mut fmt::Formatter<'_>, of: &dyn Inspect, depth: usize) -> fmt::Result {
let indent = " ".repeat(depth);
for index in 0..of.field_count() {
let Some(view) = of.field_at(index) else {
break;
};
writeln!(
f,
"{indent}{name:<20} {at:<28} {ty:<22} {shown}",
name = view.declared,
at = view.at,
ty = view.type_name,
shown = view.shown,
)?;
if let Some(gone) = &view.disagreement {
writeln!(f, "{indent} ! {}", gone.reason)?;
}
if let Some(inside) = view.inside {
rows(f, inside, depth + 1)?;
}
}
Ok(())
}