#attributes #functional #macro #parse-attributes #nom

attr-parser-fn

parse attribute procedual macros in functional way

2 unstable releases

0.2.0 May 6, 2024
0.1.0 May 6, 2024

#129 in Procedural macros

Download history 259/week @ 2024-05-05 6/week @ 2024-05-12 9/week @ 2024-05-19

274 downloads per month

MIT license

22KB
589 lines

derive-attr

Parse attribute procedual macros in functional way. Each tuple used whose length is up to 20.

Example

fn main() {
    let attr: Attribute = parse_quote! {
        #[my_attr(
            "hello",
            "world",
            122,
            conf1 = 114 + 514,
            key_value = SomeType<A, B>,
            path_only,
            nested(tea(green_tea)))
        ]
    };

    let parser = ParseArgs::new()
        .args::<(LitStr, LitStr)>()
        .opt_args::<(Lit, Lit)>()
        .rest_args::<Vec<Lit>>()
        .meta((
            ("path_only", path_only()),
            ("key_value", key_value::<Type>()),
            ("kv_optional", key_value::<Expr>()).optional(),
            conflicts((
                ("conf1", path_only()).value("conf1"),
                ("conf1", key_value::<Expr>()).value("conf1_expr"),
                ("conf2", key_value::<Expr>()).value("conf2"),
            )),
            (
                "nested",
                meta_list((
                    ("milk", path_only()),
                    (
                        "tea",
                        meta_list(conflicts((
                            ("red_tea", path_only()).value("red_tea"),
                            ("green_tea", path_only()).value("green_tea"),
                        ))),
                    ),
                )),
            ),
        ));

    let ParseArgs {
        args: (_, _),              // ("hello", "world")
        opt_args: (Some(_), None), // (Some(112), None)
        rest_args: _,              // []
        meta:
            (
                true,
                _, // SomeType<A, B>
                None,
                "conf1_expr",
                (false, "green_tea"),
            ),
    } = parser.parse_attrs(&attr).unwrap()
    else {
        unreachable!()
    };
}

Dependencies

~300–760KB
~18K SLoC