Macro laby::frag_match

source ·
frag_match!() { /* proc-macro */ }
Expand description

Wraps a match or if expression returning Render into one.

This macro allows a match or if expression to return different types of Render implementations. This would otherwise be disallowed because all branches of a match or if expression must return the same type of Render implementation.

This macro was named frag_match because it uses a set of Option variables for each variant and the frag! macro for rendering.

Expansion

// frag_match!(match $expr { $pat => $expr, ... })
{
    let mut variant1 = None, mut variant2 = None, ..;

    match $expr {
        $pat => variant1 = Some($expr),
        $pat => variant2 = Some($expr), ..
    }

    frag!(variant1, variant2, ..)
}

Examples

This example adds different types of nodes to a Vec<T> using the frag_match! macro.

let mut vec = Vec::new();

for value in ["div", "span", "img"] {
    vec.push(frag_match!(match value {
        "div" => div!(),
        "span" => span!(),
        "img" => img!(),
        _ => unreachable!(),
    }));
}

let s = render!(iter!(vec));
assert_eq!(s, "<div></div><span></span><img>");