summaryrefslogtreecommitdiff
path: root/src/default.rs
blob: 75efbb8ec4c0fa8be20cf135839a0869124a835b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use crate::TreeBuilder;

/// 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());
/// ```
pub fn 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 {
    use crate::default_tree;
    use crate::*;

    #[test]
    fn unnamed_branch() {
        add_leaf!("1");
        add_branch!();
        add_leaf!("1.1");
        {
            add_branch!();
            add_leaf!("1.1.1");
        }
        add_leaf!("1.2");
        default_tree().peek_print();
        assert_eq!(
            "\
1
├╼ 1.1
│ └╼ 1.1.1
└╼ 1.2",
            default_tree().string()
        );
    }
    #[test]
    fn named_branch() {
        add_branch!("11");
        {
            add_branch!("11.1");
            add_leaf!("11.1.1");
        }
        add_leaf!("11.2");
        default_tree().peek_print();
        assert_eq!(
            "\
11
├╼ 11.1
│ └╼ 11.1.1
└╼ 11.2",
            default_tree().string()
        );
    }

    #[test]
    fn leaf_with_value() {
        let value = add_leaf_value!(10);
        default_tree().peek_print();
        assert_eq!("10", default_tree().string());
        assert_eq!(10, value);
    }
}