display_tree

Crates.iodisplay_tree
lib.rsdisplay_tree
version1.1.2
sourcesrc
created_at2022-12-19 21:34:36.18905
updated_at2022-12-20 04:07:33.274573
descriptionSimple, automatic, and customizable tree pretty-printing
homepage
repositoryhttps://github.com/captain-camel/display_tree
max_upload_size
id741675
size46,152
Cameron Delong (captain-camel)

documentation

https://docs.rs/display_tree/*/display_tree/

README

display_tree

Simple, automatic, and customizable tree pretty-printing in Rust.

Example

This crate provides tools to easily pretty-print data as a tree, including a trait that represents the ability to be printed as a tree, and a derive macro to automatically implement it for your types

See the crate-level documentation to get started.

Examples

use display_tree::{AsTree, CharSet, DisplayTree, StyleBuilder};

// A tree representing a numerical expression.
#[derive(DisplayTree)]
enum Expr {
    Int(i32),
    BinOp {
        #[node_label]
        op: char,
        #[tree]
        left: Box<Self>,
        #[tree]
        right: Box<Self>,
    },
    UnaryOp {
        #[node_label]
        op: char,
        #[tree]
        arg: Box<Self>,
    },
}

let expr: Expr = Expr::BinOp {
    op: '+',
    left: Box::new(Expr::UnaryOp {
        op: '-',
        arg: Box::new(Expr::Int(2)),
    }),
    right: Box::new(Expr::Int(7)),
};

assert_eq!(
    format!(
        "{}",
        AsTree::new(&expr)
            .indentation(1)
            .char_set(CharSet::DOUBLE_LINE)
    ),
    concat!(
        "+\n",
        "╠═ -\n",
        "║  ╚═ Int\n",
        "║     ╚═ 2\n",
        "╚═ Int\n",
        "   ╚═ 7",
    ),
);
Commit count: 34

cargo fmt