match_options

Macro match_options 

Source
macro_rules! match_options {
    (@arms($expr:tt)
        $($level1:ident).+ $(as $name1:tt)?
        $({$(
            $($level2:ident).+ $(as $name2:tt)?
            $({$(
                $($level3:ident).+ $(as $name3:tt)?
                $({$(
                    $($level4:ident).+ $(as $name4:tt)?
                ),+ $(,)?})?
            ),+ $(,)?})?
        ),+ $(,)?})?
        $(if $guard:expr)?
        => $code:expr
        $(, $($t:tt)*)?
    ) => { ... };
    (@last $i1:ident) => { ... };
    (@last $i1:ident $i2:ident) => { ... };
    (@last $i1:ident $i2:ident $i3:ident) => { ... };
    (@last $i1:ident $i2:ident $i3:ident $i4:ident) => { ... };
    (@last($renamed:pat) $($_:tt)*) => { ... };
    (@arms($expr:tt)) => { ... };
    (@arms($expr:tt) _ => $e:expr $(,)?) => { ... };
    (match $expr:tt {$($t:tt)+}) => { ... };
    ($i:ident $expr:tt) => { ... };
    (match $expr:tt {}) => { ... };
}
Expand description

Expand into a let-chain matching option chain

§Roughly de-sugar

  • foo.bar.baz -> let Some(baz) = value.foo().bar().baz()
  • foo.bar.baz as x -> let Some(x) = value.foo().bar().baz()
  • foo.bar { a, b.c } -> let Some(bar) = value.foo().bar() && let Some(a) = bar.a() && let Some(c) = bar.b().c()

§Examples

match_options! {match atom {
    l_paren as _ => {},
    l_brack as _ => {
        // ...
    },
    ident => ident.text(),
    string => {},
    matches if matches.text() == "<>" => {},
    matches => {},
    _ => unreachable!(),
}}