| 1 |
use crate::TreeBuilder; |
| 2 |
|
| 3 |
/// Returns the default tree for the current thread |
| 4 |
/// |
| 5 |
/// # Example |
| 6 |
/// |
| 7 |
/// ``` |
| 8 |
/// use debug_tree::default_tree; |
| 9 |
/// default_tree().add_leaf("A new leaf"); |
| 10 |
/// assert_eq!("A new leaf", default_tree().peek_string()); |
| 11 |
/// ``` |
| 12 |
pub fn default_tree() -> TreeBuilder { |
| 13 |
thread_local! { |
| 14 |
static DEFAULT_BUILDER: TreeBuilder = TreeBuilder::new(); |
| 15 |
} |
| 16 |
DEFAULT_BUILDER.with(|f| f.clone()) |
| 17 |
} |
| 18 |
|
| 19 |
/// Adds a leaf to the default tree with the given text and formatting arguments |
| 20 |
/// |
| 21 |
/// # Arguments |
| 22 |
/// * `text...` - Formatted text arguments, as per `format!(...)`. |
| 23 |
/// |
| 24 |
/// # Example |
| 25 |
/// |
| 26 |
/// ``` |
| 27 |
/// #[macro_use] |
| 28 |
/// use debug_tree::{default_tree, add_leaf}; |
| 29 |
/// fn main() { |
| 30 |
/// add_leaf!("A {} leaf", "new"); |
| 31 |
/// assert_eq!("A new leaf", &default_tree().peek_string()); |
| 32 |
/// } |
| 33 |
/// ``` |
| 34 |
#[macro_export] |
| 35 |
macro_rules! add_leaf { |
| 36 |
($($arg:tt)*) => { |
| 37 |
if $crate::default::default_tree().is_enabled() { |
| 38 |
$crate::default::default_tree().add_leaf(&format!($($arg)*)) |
| 39 |
} |
| 40 |
}; |
| 41 |
} |
| 42 |
|
| 43 |
/// Adds the value as a leaf to the default tree. |
| 44 |
/// |
| 45 |
/// Returns the given `value` argument. |
| 46 |
/// |
| 47 |
/// # Arguments |
| 48 |
/// * `value` - An expression that implements the `Display` trait. |
| 49 |
/// |
| 50 |
/// # Example |
| 51 |
/// |
| 52 |
/// ``` |
| 53 |
/// #[macro_use] |
| 54 |
/// use debug_tree::{default_tree, add_leaf_value}; |
| 55 |
/// fn main() { |
| 56 |
/// let value = add_leaf_value!(10); |
| 57 |
/// assert_eq!("10", &default_tree().string()); |
| 58 |
/// assert_eq!(10, value); |
| 59 |
/// } |
| 60 |
/// ``` |
| 61 |
#[macro_export] |
| 62 |
macro_rules! add_leaf_value { |
| 63 |
($value:expr) => {{ |
| 64 |
let v = $value; |
| 65 |
if $crate::default::default_tree().is_enabled() { |
| 66 |
$crate::default::default_tree().add_leaf(&format!("{}", &v)); |
| 67 |
} |
| 68 |
v |
| 69 |
}}; |
| 70 |
} |
| 71 |
|
| 72 |
/// Adds a scoped branch to the default tree with the given text and formatting arguments |
| 73 |
/// The branch will be exited at the end of the current block. |
| 74 |
/// |
| 75 |
/// # Arguments |
| 76 |
/// * `text...` - Formatted text arguments, as per `format!(...)`. |
| 77 |
/// |
| 78 |
/// # Example |
| 79 |
/// |
| 80 |
/// ``` |
| 81 |
/// #[macro_use] |
| 82 |
/// use debug_tree::{default_tree, add_branch, add_leaf}; |
| 83 |
/// fn main() { |
| 84 |
/// { |
| 85 |
/// add_branch!("New {}", "Branch"); // _branch enters scope |
| 86 |
/// // tree is now pointed inside new branch. |
| 87 |
/// add_leaf!("Child of {}", "Branch"); |
| 88 |
/// // Block ends, so tree exits the current branch. |
| 89 |
/// } |
| 90 |
/// add_leaf!("Sibling of {}", "Branch"); |
| 91 |
/// assert_eq!("\ |
| 92 |
/// New Branch |
| 93 |
/// └╼ Child of Branch |
| 94 |
/// Sibling of Branch" , &default_tree().string()); |
| 95 |
/// } |
| 96 |
/// ``` |
| 97 |
#[macro_export] |
| 98 |
macro_rules! add_branch { |
| 99 |
() => { |
| 100 |
let _debug_tree_branch = if $crate::default::default_tree().is_enabled() { |
| 101 |
$crate::default::default_tree().enter_scoped() |
| 102 |
} else { |
| 103 |
$crate::scoped_branch::ScopedBranch::none() |
| 104 |
}; |
| 105 |
}; |
| 106 |
($($arg:tt)*) => { |
| 107 |
let _debug_tree_branch = if $crate::default::default_tree().is_enabled() { |
| 108 |
$crate::default::default_tree().add_branch(&format!($($arg)*)) |
| 109 |
} else { |
| 110 |
$crate::scoped_branch::ScopedBranch::none() |
| 111 |
}; |
| 112 |
}; |
| 113 |
|
| 114 |
} |
| 115 |
|
| 116 |
#[cfg(test)] |
| 117 |
mod test { |
| 118 |
use crate::default_tree; |
| 119 |
use crate::*; |
| 120 |
|
| 121 |
#[test] |
| 122 |
fn unnamed_branch() { |
| 123 |
add_leaf!("1"); |
| 124 |
add_branch!(); |
| 125 |
add_leaf!("1.1"); |
| 126 |
{ |
| 127 |
add_branch!(); |
| 128 |
add_leaf!("1.1.1"); |
| 129 |
} |
| 130 |
add_leaf!("1.2"); |
| 131 |
default_tree().peek_print(); |
| 132 |
assert_eq!( |
| 133 |
"\ |
| 134 |
1 |
| 135 |
├╼ 1.1 |
| 136 |
│ └╼ 1.1.1 |
| 137 |
└╼ 1.2", |
| 138 |
default_tree().string() |
| 139 |
); |
| 140 |
} |
| 141 |
#[test] |
| 142 |
fn named_branch() { |
| 143 |
add_branch!("11"); |
| 144 |
{ |
| 145 |
add_branch!("11.1"); |
| 146 |
add_leaf!("11.1.1"); |
| 147 |
} |
| 148 |
add_leaf!("11.2"); |
| 149 |
default_tree().peek_print(); |
| 150 |
assert_eq!( |
| 151 |
"\ |
| 152 |
11 |
| 153 |
├╼ 11.1 |
| 154 |
│ └╼ 11.1.1 |
| 155 |
└╼ 11.2", |
| 156 |
default_tree().string() |
| 157 |
); |
| 158 |
} |
| 159 |
|
| 160 |
#[test] |
| 161 |
fn leaf_with_value() { |
| 162 |
let value = add_leaf_value!(10); |
| 163 |
default_tree().peek_print(); |
| 164 |
assert_eq!("10", default_tree().string()); |
| 165 |
assert_eq!(10, value); |
| 166 |
} |
| 167 |
} |