Struct TreeBuilder

Source
pub struct TreeBuilder(/* private fields */);
Expand description

Reference wrapper for TreeBuilderBase

Implementations§

Source§

impl TreeBuilder

Source

pub fn new() -> TreeBuilder

Returns a new TreeBuilder with an empty Tree.

§Example
use debug_tree::TreeBuilder;
let tree = TreeBuilder::new();
Source

pub fn set_config_override(&self, config: TreeConfig)

Set the configuration override for displaying trees

§Example
use debug_tree::{TreeBuilder, add_branch_to, add_leaf_to, TreeSymbols, TreeConfig};
let tree = TreeBuilder::new();
{
    add_branch_to!(tree, "1");
    {
        add_branch_to!(tree, "1.1");
        add_leaf_to!(tree, "1.1.1");
        add_leaf_to!(tree, "1.1.2");
    }
    add_leaf_to!(tree, "1.2");
}
add_leaf_to!(tree, "2");
tree.set_config_override(TreeConfig::new()
    .show_first_level()
    .symbols(TreeSymbols::with_rounded()));
tree.peek_print();
assert_eq!("\
├╼ 1
│ ├╼ 1.1
│ │ ├╼ 1.1.1
│ │ ╰╼ 1.1.2
│ ╰╼ 1.2
╰╼ 2" , &tree.string());
Source

pub fn remove_config_override(&self)

Remove the configuration override The default configuration will be used instead

Source

pub fn update_config_override<F: Fn(&mut TreeConfig)>(&self, update: F)

Update the configuration override for displaying trees If an override doesn’t yet exist, it is created.

§Example
use debug_tree::{TreeBuilder, add_branch_to, add_leaf_to, TreeSymbols};
let tree = TreeBuilder::new();
{
    add_branch_to!(tree, "1");
    {
        add_branch_to!(tree, "1.1");
        add_leaf_to!(tree, "1.1.1");
        add_leaf_to!(tree, "1.1.2");
    }
    add_leaf_to!(tree, "1.2");
}
add_leaf_to!(tree, "2");
tree.update_config_override(|x|{
    x.indent = 3;
    x.symbols = TreeSymbols::with_rounded();
    x.show_first_level = true;
});
tree.peek_print();
assert_eq!("\
├─╼ 1
│  ├─╼ 1.1
│  │  ├─╼ 1.1.1
│  │  ╰─╼ 1.1.2
│  ╰─╼ 1.2
╰─╼ 2" , &tree.string());
Source

pub fn get_config_override(&self) -> Option<TreeConfig>

Returns the optional configuration override.

Source

pub fn has_config_override(&self) -> bool

Returns whether a configuration override is set.

Source

pub fn add_branch(&self, text: &str) -> ScopedBranch

Adds a new branch with text, text and returns a ScopedBranch. When the returned ScopedBranch goes out of scope, (likely the end of the current block), or if its release() method is called, the tree will step back out of the added branch.

§Arguments
  • text - A string slice to use as the newly added branch’s text.
§Examples

Exiting branch when end of scope is reached.

use debug_tree::TreeBuilder;
let tree = TreeBuilder::new();
{
    let _branch = tree.add_branch("Branch"); // _branch enters scope
    // tree is now pointed inside new branch.
    tree.add_leaf("Child of Branch");
    // _branch leaves scope, tree moves up to parent branch.
}
tree.add_leaf("Sibling of Branch");
assert_eq!("\
Branch
└╼ Child of Branch
Sibling of Branch" , &tree.string());

Using release() before out of scope.

use debug_tree::TreeBuilder;
let tree = TreeBuilder::new();
{
    let mut branch = tree.add_branch("Branch"); // branch enters scope
    // tree is now pointed inside new branch.
    tree.add_leaf("Child of Branch");
    branch.release();
    tree.add_leaf("Sibling of Branch");
    // branch leaves scope, but no effect because its `release()` method has already been called
}
assert_eq!("\
Branch
└╼ Child of Branch
Sibling of Branch", &tree.string());
Source

pub fn enter_scoped(&self) -> ScopedBranch

Adds a new branch with text, text and returns a ScopedBranch. When the returned ScopedBranch goes out of scope, (likely the end of the current block), or if its release() method is called, the tree tree will step back out of the added branch.

§Arguments
  • text - A string slice to use as the newly added branch’s text.
§Examples

Stepping out of branch when end of scope is reached.

use debug_tree::TreeBuilder;
let tree = TreeBuilder::new();
{
    tree.add_leaf("Branch");
    let _branch = tree.enter_scoped(); // _branch enters scope
    // tree is now pointed inside new branch.
    tree.add_leaf("Child of Branch");
    // _branch leaves scope, tree moves up to parent branch.
}
tree.add_leaf("Sibling of Branch");
assert_eq!("\
Branch
└╼ Child of Branch
Sibling of Branch", &tree.string());

Using release() before out of scope.

use debug_tree::TreeBuilder;
let tree = TreeBuilder::new();
{
    tree.add_leaf("Branch");
    let mut branch = tree.enter_scoped(); // branch enters scope
    // tree is now pointed inside new branch.
    tree.add_leaf("Child of Branch");
    branch.release();
    tree.add_leaf("Sibling of Branch");
    // branch leaves scope, but no effect because its `release()` method has already been called
}
assert_eq!("\
Branch
└╼ Child of Branch
Sibling of Branch", &tree.string());
Source

pub fn add_leaf(&self, text: &str)

Adds a leaf to current branch with the given text, text.

§Arguments
  • text - A string slice to use as the newly added leaf’s text.
§Example
use debug_tree::TreeBuilder;
let tree = TreeBuilder::new();
tree.add_leaf("New leaf");
Source

pub fn enter(&self)

Steps into a new child branch. Stepping out of the branch requires calling exit().

§Example
use debug_tree::TreeBuilder;
let tree = TreeBuilder::new();
tree.add_leaf("Branch");
tree.enter();
tree.add_leaf("Child of Branch");
assert_eq!("\
Branch
└╼ Child of Branch", &tree.string());
Source

pub fn exit(&self) -> bool

Exits the current branch, to the parent branch. If no parent branch exists, no action is taken

§Example
use debug_tree::TreeBuilder;
let tree = TreeBuilder::new();
tree.add_leaf("Branch");
tree.enter();
tree.add_leaf("Child of Branch");
tree.exit();
tree.add_leaf("Sibling of Branch");
assert_eq!("\
Branch
└╼ Child of Branch
Sibling of Branch", &tree.string());
Source

pub fn depth(&self) -> usize

Returns the depth of the current branch The initial depth when no branches have been adeed is 0.

§Example
use debug_tree::TreeBuilder;
let tree = TreeBuilder::new();
assert_eq!(0, tree.depth());
let _b = tree.add_branch("Branch");
assert_eq!(1, tree.depth());
let _b = tree.add_branch("Child branch");
assert_eq!(2, tree.depth());
Source

pub fn peek_print(&self)

Prints the tree without clearing.

§Example
use debug_tree::TreeBuilder;
let tree = TreeBuilder::new();
tree.add_leaf("Leaf");
tree.peek_print();
// Leaf
tree.peek_print();
// Leaf
// Leaf 2
Source

pub fn print(&self)

Prints the tree and then clears it.

§Example
use debug_tree::TreeBuilder;
let tree = TreeBuilder::new();
tree.add_leaf("Leaf");
tree.print();
// Leaf
tree.add_leaf("Leaf 2");
tree.print();
// Leaf 2
Source

pub fn peek_string(&self) -> String

Returns the tree as a string without clearing the tree.

§Example
use debug_tree::TreeBuilder;
let tree = TreeBuilder::new();
tree.add_leaf("Leaf");
assert_eq!("Leaf", tree.peek_string());
tree.add_leaf("Leaf 2");
assert_eq!("Leaf\nLeaf 2", tree.peek_string());
Source

pub fn string(&self) -> String

Returns the tree as a string and clears the tree.

§Example
use debug_tree::TreeBuilder;
let tree = TreeBuilder::new();
tree.add_leaf("Leaf");
assert_eq!("Leaf", tree.string());
tree.add_leaf("Leaf 2");
assert_eq!("Leaf 2", tree.string());
Source

pub fn peek_write(&self, path: &str) -> Result<()>

Writes the tree to file without clearing.

§Example
use debug_tree::TreeBuilder;
use std::fs::{read_to_string, create_dir};
use std::io::Read;
let tree = TreeBuilder::new();
create_dir("test_out").ok();
tree.add_leaf("Leaf");
assert_eq!(tree.peek_string(), "Leaf");
tree.peek_write("test_out/peek_write.txt");
assert_eq!(read_to_string("test_out/peek_write.txt").unwrap(), "Leaf");
assert_eq!(tree.peek_string(), "Leaf");
Source

pub fn write(&self, path: &str) -> Result<()>

Writes the tree to file without clearing.

§Example
use debug_tree::TreeBuilder;
use std::io::Read;
use std::fs::{read_to_string, create_dir};
let tree = TreeBuilder::new();
create_dir("test_out").ok();
tree.add_leaf("Leaf");
assert_eq!(tree.peek_string(), "Leaf");
tree.write("test_out/write.txt");
assert_eq!(read_to_string("test_out/write.txt").unwrap(), "Leaf");
assert_eq!(tree.peek_string(), "");
Source

pub fn clear(&self)

Clears the tree.

§Example
use debug_tree::TreeBuilder;
let tree = TreeBuilder::new();
tree.add_leaf("Leaf");
assert_eq!("Leaf", tree.peek_string());
tree.clear();
assert_eq!("", tree.peek_string());
Source

pub fn set_enabled(&self, enabled: bool)

Sets the enabled state of the tree.

If not enabled, the tree will not be modified by adding leaves or branches. Additionally, if called using the add_… macros, arguments will not be processed. This is particularly useful for suppressing output in production, with very little overhead.

§Example
#[macro_use]
use debug_tree::{TreeBuilder, add_leaf_to};
let mut tree = TreeBuilder::new();
tree.add_leaf("Leaf 1");
tree.set_enabled(false);
add_leaf_to!(tree, "Leaf 2");
tree.set_enabled(true);
add_leaf_to!(tree, "Leaf 3");
assert_eq!("Leaf 1\nLeaf 3", tree.peek_string());
Source

pub fn is_enabled(&self) -> bool

Returns the enabled state of the tree.

§Example
use debug_tree::TreeBuilder;
let mut tree = TreeBuilder::new();
assert_eq!(true, tree.is_enabled());
tree.set_enabled(false);
assert_eq!(false, tree.is_enabled());

Trait Implementations§

Source§

impl AsTree for TreeBuilder

Source§

impl Clone for TreeBuilder

Source§

fn clone(&self) -> TreeBuilder

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TreeBuilder

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.