Macro diagnostics_hierarchy::hierarchy

source ·
macro_rules! hierarchy {
    (@build $hierarchy:expr,) => { ... };
    (@build $hierarchy:expr, var $key:ident: { $($sub:tt)* }) => { ... };
    (@build $hierarchy:expr, var $key:ident: { $($sub:tt)* }, $($rest:tt)*) => { ... };
    (@build $hierarchy:expr, var $key:ident: $value:expr) => { ... };
    (@build $hierarchy:expr, var $key:ident: $value:expr, $($rest:tt)*) => { ... };
    (@build $hierarchy:expr, $key:ident: $($rest:tt)+) => { ... };
    (@build $hierarchy:expr, $key:tt: $($rest:tt)+) => { ... };
    (@build $hierarchy:expr, $key:expr => $value:expr) => { ... };
    (@build $hierarchy:expr, $key:expr => $value:expr, $($rest:tt)*) => { ... };
    (var $key:ident: { $($rest:tt)* }) => { ... };
    ($key:ident: $($rest:tt)+) => { ... };
    ($key:tt: $($rest:tt)+) => { ... };
}
Expand description

This macro simplifies creating diagnostics hierarchies, to remove the need of writing multiple nested hierarchies and manually writing all properties.

Example:

let hierarchy = hierarchy!{
    root: {
        some_int_property: 2,
        some_nested_child: {
            some_string_property: "foo",
        }
    }
};

Using names like this will create a DiagnosticsHierarchy where Key is of type String.

It’s possible to use expressions that resolve to some other type. In the following example, we use Field variants as the key. The function bar() returns a Field::Bar.

enum Field {
    Foo,
    Bar,
}

let hierarchy = hierarchy!{
    root: {
        Field::Foo => "bar",
        bar() => 2,
    }
};

The only requirement (as in any DiagnosticsHierarchy’s Key) it must implement AsRef<str>.

It’s also possible to use existing variables as keys. For example:

let my_var = "my_key".to_string();
let hierarchy = hierarchy!{
    root: {
        var my_var: "foo",
    }
};