Skip to main content

tree_with

Macro tree_with 

Source
macro_rules! tree_with {
    ($($tt:tt)*) => { ... };
}
Expand description

Helper macro for building a tree in place with a custom flavor.

This takes the same input as tree!, but builds the tree with Builder::new_with. The flavor is inferred from the type the result is used as, so it usually needs a type annotation such as let tree: Tree<_, MyFlavor> = ...

Like tree!, it expands to builder calls followed by ?, so it can only be used in a function returning a Result whose error type implements From<Error<F::Error>>, where F is the flavor of the tree.

ยงExamples

use syntree::{Empty, EmptyVec, Tree, TreeIndex};

syntree::flavor! {
    struct FlavorEmpty {
        type Index = Empty;
        type Indexes = EmptyVec<TreeIndex<Self>>;
    }
};

let tree: Tree<_, FlavorEmpty> = syntree::tree_with! {
    "root" => {
        "child" => {
            "token"
        },
        "child2"
    }
};

let expected: Tree<_, FlavorEmpty> = syntree::tree_with! {
    "root" => {
        "child" => {
            ("token", Empty)
        },
        "child2"
    }
};

assert_eq!(tree, expected);