nb_tree/
macros.rs

1/// Force AST node to an expression to improve diagnostics in pattern position.
2#[doc(hidden)]
3#[macro_export]
4macro_rules! __rust_force_expr {
5    ($e:expr) => {
6        $e
7    };
8}
9
10//TODO: not rely on vec?
11/// Build a [`Path`] from a list of expressions.
12/// ```
13/// use nb_tree::{path, path::Path};
14/// let path: Path<String> = path!["a", "b", "c", "d", "e", "f"];
15/// assert_eq!(path, "/a/b/c/d/e/f".into());
16/// ```
17///
18/// [`Path`]: crate::path::Path
19#[macro_export]
20macro_rules! path {
21    () => (
22        $crate::__rust_force_expr!($crate::path::Path::new())
23    );
24    ($elem:expr; $n:expr) => (
25        $crate::__rust_force_expr!($crate::path::Path::from(std::vec::from_elem($elem, $n)))
26    );
27    ($($x:expr),+ $(,)?) => (
28        $crate::__rust_force_expr!($crate::path::Path::from(<[_]>::into_vec(
29            std::boxed::Box::new([$($x),+])
30        )))
31    );
32}
33
34//TODO: tree macro
35//inspiration: https://docs.rs/serde_json/latest/src/serde_json/macros.rs.html#54-59
36
37#[cfg(test)]
38mod tests {
39    use crate::{path, prelude::Path /*, tree, tree::Tree*/};
40
41    #[test]
42    fn path() {
43        let path: Path<(i32, bool)> = path![(7, false), (2, true), (8, true)];
44        assert_eq!(path, vec![(7, false), (2, true), (8, true)].into());
45    }
46
47    /*
48    #[test]
49    fn tree() {
50        let small_tree: Tree<i32, i32> = tree! {5};
51        let st: Tree<i32, i32> = Tree::new();
52        st.i("/", 5);
53        assert_eq!(small_tree, st);
54
55        let tree: Tree<String, usize> = tree! {
56            "Hello!",
57            [
58                0 => {"This"},
59                1 => {"Is"},
60                2 => {"A"},
61                3 => {"Tree!",
62                    [
63                        9 => {"hi"},
64                        2 => {"there"},
65                    ]
66                }
67            ]
68        };
69        let t: Tree<String, usize> = Tree::new();
70        t.i("/", "Hello!".into())
71            .i("/0", "This".into())
72            .i("/1", "Is".into())
73            .i("/2", "A".into())
74            .i("/3", "Tree".into())
75            .i("/3/9", "hi".into())
76            .i("/3/2", "there".into());
77        assert_eq!(small_tree, t);
78    }*/
79}