pub struct TreeBuilder(/* private fields */);
Expand description
Reference wrapper for TreeBuilderBase
Implementations§
Source§impl TreeBuilder
impl TreeBuilder
Sourcepub fn new() -> TreeBuilder
pub fn new() -> TreeBuilder
Returns a new TreeBuilder
with an empty Tree
.
§Example
use debug_tree::TreeBuilder;
let tree = TreeBuilder::new();
Sourcepub fn set_config_override(&self, config: TreeConfig)
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());
Sourcepub fn remove_config_override(&self)
pub fn remove_config_override(&self)
Remove the configuration override The default configuration will be used instead
Sourcepub fn update_config_override<F: Fn(&mut TreeConfig)>(&self, update: F)
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());
Sourcepub fn get_config_override(&self) -> Option<TreeConfig>
pub fn get_config_override(&self) -> Option<TreeConfig>
Returns the optional configuration override.
Sourcepub fn has_config_override(&self) -> bool
pub fn has_config_override(&self) -> bool
Returns whether a configuration override is set.
Sourcepub fn add_branch(&self, text: &str) -> ScopedBranch
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());
Sourcepub fn enter_scoped(&self) -> ScopedBranch
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());
Sourcepub fn enter(&self)
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());
Sourcepub fn exit(&self) -> bool
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());
Sourcepub fn depth(&self) -> usize
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());
Sourcepub fn peek_print(&self)
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
Sourcepub fn print(&self)
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
Sourcepub fn peek_string(&self) -> String
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());
Sourcepub fn string(&self) -> String
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());
Sourcepub fn peek_write(&self, path: &str) -> Result<()>
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");
Sourcepub fn write(&self, path: &str) -> Result<()>
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(), "");
Sourcepub fn clear(&self)
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());
Sourcepub fn set_enabled(&self, enabled: bool)
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());
Sourcepub fn is_enabled(&self) -> bool
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
impl AsTree for TreeBuilder
fn as_tree(&self) -> TreeBuilder
fn is_tree_enabled(&self) -> bool
Source§impl Clone for TreeBuilder
impl Clone for TreeBuilder
Source§fn clone(&self) -> TreeBuilder
fn clone(&self) -> TreeBuilder
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more