macro_rules! get_path {
    (@access $root:ident,) => { ... };
    (@access $root:ident, !) => { ... };
    (@access $root:ident, . $field:ident $( $tail:tt )*) => { ... };
    (@access $root:ident, [ $index:literal ] $( $tail:tt )*) => { ... };
    ($root:ident $( $tail:tt )*) => { ... };
}
Expand description

Returns a reference to the typed Annotated value at a given path.

The return type depends on the path expression used. By default, this macro will resolve to an Option<&Annotated<T>>, where the option is Some if the path exists. Note that if the annotated value at the specificed path is empty, this returns Some with an empty annotated.

When used with an exclamation mark after the path, this macro unwraps to an &Annotated<T>.

§Syntax

A path starts with the name of a variable holding an Annotated. Access to children of this type depend on the value type:

  • To access a struct field, use a period followed by the field’s name, for instance (.field).
  • To access an array element, use the element’s numeric index in brackets, for instance [0].
  • To access an object’s element, use the element’s quoted string key in brackets, for instance ["key_name"].

Paths can be chained, so a valid path expression is data.values["key"].field.

To unwrap the annotated field at the destination, append an exclamation mark at the end of the path, for instance: data.field!.

§Panics

Panics when unwrap (!) is used and there is an empty field along the path. Since get_path! always returns the final Annotated, the final field can be empty without panicking.

§Example

use relay_protocol::{get_path, Annotated, Object};

struct Inner {
    value: Annotated<u64>,
}

struct Outer {
    inners: Annotated<Object<Inner>>,
}

let outer = Annotated::new(Outer {
    inners: Annotated::new(Object::from([(
        "key".to_string(),
        Annotated::new(Inner {
            value: Annotated::new(1),
        }),
    )])),
});

assert_eq!(get_path!(outer.inners["key"].value), Some(&Annotated::new(1)));
assert_eq!(get_path!(outer.inners["key"].value!), &Annotated::new(1));