TreeNode

Struct TreeNode 

Source
pub struct TreeNode { /* private fields */ }
Expand description

A node system that will display as a tree in the terminal using unicode characters. Originally designed to display the Abstract Syntax Tree of my Programming language.

§Example:

use cli_tree::TreeNode;

// This will print:
// Test Node
//  ├─Child 1
//  ├─Child 2
//  │ ├─Child 2.1
//  │ ├─Child 2.2
//  │ ├─Child 2.3
//  │ │ ├─Child 2.3.1
//  │ │ └─Child 2.3.2
//  │ └─Child 2.4
//  │   ├─Child 2.4.1
//  │   ├─Child 2.4.2
//  │   └─Child 2.4.3
//  └─Child 3

let mut node = TreeNode::new("Test Node");
// add a single child
node.add_child(TreeNode::new("Child 1"));
// add a child with children which also have children
node.add_child(TreeNode::new_with_children("Child 2",
               vec![TreeNode::new("Child 2.1"),
                    TreeNode::new("Child 2.2"),
                    TreeNode::new_with_children("Child 2.3",
                                  vec![TreeNode::new("Child 2.3.1"),
                                       TreeNode::new("Child 2.3.2")]),
                    TreeNode::new_with_children("Child 2.4",
                                  vec![TreeNode::new("Child 2.4.1"),
                                       TreeNode::new("Child 2.4.2"),
                                       TreeNode::new("Child 2.4.3")])]));
// add another singular child
node.add_child(TreeNode::new("Child 3"));
println!("{}", node);

Implementations§

Source§

impl TreeNode

Source

pub fn new<S: Into<String>>(name: S) -> TreeNode

Source

pub fn new_with_children<S: Into<String>>( name: S, children: Vec<TreeNode>, ) -> TreeNode

Source

pub fn add_child(&mut self, child: TreeNode)

Source

pub fn has_children(&self) -> bool

Trait Implementations§

Source§

impl Display for TreeNode

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.