Skip to main content

DisplayPretty

Trait DisplayPretty 

Source
pub trait DisplayPretty {
    type Options: ?Sized;

    // Required method
    fn fmt(&self, f: &mut Formatter<'_>, options: &Self::Options) -> Result;

    // Provided method
    fn display<'a>(
        &'a self,
        options: &'a Self::Options,
    ) -> PrettyDisplay<'a, Self> { ... }
}
Expand description

A trait for formatting values in a human-friendly, readable representation.

DisplayPretty is used when you want to maximize readability with indentation, whitespace, and multi-line output. This is ideal for debugging, documentation, or when human comprehension is more important than space efficiency.

§Use Cases

  • Debugging: Easily readable output for complex data structures
  • Pretty-printing ASTs: Multi-line formatted abstract syntax trees
  • Configuration files: Human-editable output formats
  • Error messages: Clear, well-formatted diagnostic information

§Options Type

The associated Options type typically carries indentation information. Common patterns include:

  • usize: Current indentation level
  • Custom struct: With indent size, style preferences, etc.

§Examples

§Simple Pretty Display

use tokora::utils::DisplayPretty;

struct Point { x: i32, y: i32 }

impl DisplayPretty for Point {
    type Options = ();

    fn fmt(&self, f: &mut core::fmt::Formatter<'_>, _: &()) -> core::fmt::Result {
        writeln!(f, "Point {{")?;
        writeln!(f, "  x: {},", self.x)?;
        writeln!(f, "  y: {}", self.y)?;
        write!(f, "}}")
    }
}

let p = Point { x: 10, y: 20 };
println!("{}", p.display(&()));
// Output:
// Point {
//   x: 10,
//   y: 20
// }

§With Indentation

use tokora::utils::DisplayPretty;

struct Tree {
    value: i32,
    children: Vec<Tree>,
}

impl DisplayPretty for Tree {
    type Options = usize; // indentation level

    fn fmt(&self, f: &mut core::fmt::Formatter<'_>, indent: &usize) -> core::fmt::Result {
        let spaces = "  ".repeat(*indent);
        writeln!(f, "{}Tree {{", spaces)?;
        writeln!(f, "{}  value: {}", spaces, self.value)?;
        if !self.children.is_empty() {
            writeln!(f, "{}  children: [", spaces)?;
            for child in &self.children {
                child.fmt(f, &(indent + 2))?;
            }
            writeln!(f, "{}  ]", spaces)?;
        }
        write!(f, "{}}}", spaces)
    }
}

let tree = Tree {
    value: 1,
    children: vec![
        Tree { value: 2, children: vec![] },
        Tree { value: 3, children: vec![] },
    ],
};
println!("{}", tree.display(&0));

Required Associated Types§

Source

type Options: ?Sized

The options type for controlling pretty formatting.

Required Methods§

Source

fn fmt(&self, f: &mut Formatter<'_>, options: &Self::Options) -> Result

Formats the value in a human-friendly, readable way.

Provided Methods§

Source

fn display<'a>(&'a self, options: &'a Self::Options) -> PrettyDisplay<'a, Self>

Returns a wrapper that implements Display for pretty formatting.

§Example
ⓘ
let pretty = value.display(&0); // 0 indentation
println!("{}", pretty);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<T: DisplayPretty + ?Sized> DisplayPretty for &T

Source§

type Options = <T as DisplayPretty>::Options

Source§

fn fmt(&self, f: &mut Formatter<'_>, options: &Self::Options) -> Result

Implementors§

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for Ampersand<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for AmpersandEqual<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for Angle<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for Apostrophe<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for Arrow<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for Asterisk<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for AsteriskEqual<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for At<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for Backslash<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for BackslashEqual<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for Backtick<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for Brace<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for Bracket<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for Caret<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for CaretEqual<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for CarriageReturn<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for CarriageReturnNewline<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for CloseAngle<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for CloseBrace<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for CloseBracket<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for CloseParen<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for Colon<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for ColonEqual<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for Comma<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for Decrement<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for Dollar<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for Dot<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for DoubleColon<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for DoubleQuote<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for Equal<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for Exclamation<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for Exponentiation<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for ExponentiationEqual<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for FatArrow<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for GreaterThanOrEqual<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for Hash<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for Hyphen<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for HyphenEqual<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for Increment<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for LessThanOrEqual<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for LogicalAnd<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for LogicalEqual<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for LogicalNotEqual<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for LogicalOr<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for Newline<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for NullCoalesce<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for OpenAngle<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for OpenBrace<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for OpenBracket<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for OpenParen<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for OptionalChain<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for Paren<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for Percent<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for PercentEqual<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for Pipe<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for PipeArrow<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for PipeEqual<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for Plus<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for PlusEqual<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for Question<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for SarEqual<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for Semicolon<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for ShiftArithmeticRight<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for ShiftLeft<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for ShiftRight<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for ShlEqual<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for ShrEqual<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for Slash<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for SlashEqual<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for Space<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for Spread<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for StrictEqual<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for StrictGreaterThanOrEqual<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for StrictLessThanOrEqual<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for StrictNotEqual<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for Tab<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for Tilde<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for Trivia<S, C, Lang>

Source§

impl<S, C, Lang: ?Sized> DisplayPretty for Underscore<S, C, Lang>