/// Returns the default tree for the current thread /// /// # Example /// /// ``` /// use debug_tree::default_tree; /// default_tree().add_leaf("A new leaf"); /// assert_eq!("A new leaf", default_tree().peek_string()); /// ``` pubfn default_tree() -> TreeBuilder {
thread_local! { static DEFAULT_BUILDER: TreeBuilder = TreeBuilder::new();
}
DEFAULT_BUILDER.with(|f| f.clone())
}
/// Adds a leaf to the default tree with the given text and formatting arguments /// /// # Arguments /// * `text...` - Formatted text arguments, as per `format!(...)`. /// /// # Example /// /// ``` /// #[macro_use] /// use debug_tree::{default_tree, add_leaf}; /// fn main() { /// add_leaf!("A {} leaf", "new"); /// assert_eq!("A new leaf", &default_tree().peek_string()); /// } /// ``` #[macro_export]
macro_rules! add_leaf {
($($arg:tt)*) => { if $crate::default::default_tree().is_enabled() {
$crate::default::default_tree().add_leaf(&format!($($arg)*))
}
};
}
/// Adds the value as a leaf to the default tree. /// /// Returns the given `value` argument. /// /// # Arguments /// * `value` - An expression that implements the `Display` trait. /// /// # Example /// /// ``` /// #[macro_use] /// use debug_tree::{default_tree, add_leaf_value}; /// fn main() { /// let value = add_leaf_value!(10); /// assert_eq!("10", &default_tree().string()); /// assert_eq!(10, value); /// } /// ``` #[macro_export]
macro_rules! add_leaf_value {
($value:expr) => {{ let v = $value; if $crate::default::default_tree().is_enabled() {
$crate::default::default_tree().add_leaf(&format!("{}", &v));
}
v
}};
}
/// Adds a scoped branch to the default tree with the given text and formatting arguments /// The branch will be exited at the end of the current block. /// /// # Arguments /// * `text...` - Formatted text arguments, as per `format!(...)`. /// /// # Example /// /// ``` /// #[macro_use] /// use debug_tree::{default_tree, add_branch, add_leaf}; /// fn main() { /// { /// add_branch!("New {}", "Branch"); // _branch enters scope /// // tree is now pointed inside new branch. /// add_leaf!("Child of {}", "Branch"); /// // Block ends, so tree exits the current branch. /// } /// add_leaf!("Sibling of {}", "Branch"); /// assert_eq!("\ /// New Branch /// └╼ Child of Branch /// Sibling of Branch" , &default_tree().string()); /// } /// ``` #[macro_export]
macro_rules! add_branch {
() => { let _debug_tree_branch = if $crate::default::default_tree().is_enabled() {
$crate::default::default_tree().enter_scoped()
} else {
$crate::scoped_branch::ScopedBranch::none()
};
};
($($arg:tt)*) => { let _debug_tree_branch = if $crate::default::default_tree().is_enabled() {
$crate::default::default_tree().add_branch(&format!($($arg)*))
} else {
$crate::scoped_branch::ScopedBranch::none()
};
};
}
#[cfg(test)] mod test { usecrate::default_tree; usecrate::*;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.