logparse-pretty-print 0.1.0

pretty print tree
Documentation
  • Coverage
  • 100%
    78 out of 78 items documented2 out of 2 items with examples
  • Size
  • Source code size: 72 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 8.13 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 40s Average build duration of successful builds.
  • all releases: 40s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • jdonszelmann

Pretty Printer

This crate defines a Wadler-style pretty-printing API.

Start with the static functions of PrettyTree.

Quick start

Let's pretty-print simple sexps! We want to pretty print sexps like

(1 2 3)

or, if the line would be too long, like

((1)
 (2 3)
 (4 5 6))

A simple symbolic expression consists of a numeric atom or a nested ordered list of symbolic expression children.

use pretty_print::*;
use SExp::*;
enum SExp {
    Atom(u32),
    List(Vec<SExp>),
}

impl SExp {
    /// Return a pretty printed format of self.
    pub fn to_doc(&self) -> PrettyTree {
        match self {
            Atom(x) => PrettyTree::text(x.to_string()),
            List(xs) => PrettyTree::text("(")
                .append(PrettyTree::join(xs.into_iter().map(|x| x.to_doc()), PrettyTree::line_or_space()).nest(1).group())
                .append(PrettyTree::text(")")),
        }
    }
    /// Return a pretty printed format of self.
    pub fn to_pretty(&self, width: usize) -> String {
        let mut w = Vec::new();
        self.to_doc().render(width, &mut w).unwrap();
        String::from_utf8(w).unwrap()
    }
}

fn main() {
    let atom = Atom(5);
    assert_eq!("5", atom.to_pretty(10));
    let list = List(vec![Atom(1), Atom(2), Atom(3)]);
    assert_eq!("(1 2 3)", list.to_pretty(10));
    assert_eq!(
        "\
(1
 2
 3)",
        list.to_pretty(5)
    );
}