attr-parser-fn

Crates.ioattr-parser-fn
lib.rsattr-parser-fn
version0.3.4
sourcesrc
created_at2024-05-06 07:55:28.048278
updated_at2024-08-19 03:38:13.029249
descriptionparse attribute procedual macros in functional way
homepage
repositoryhttps://github.com/Fancyflame/derive-attr
max_upload_size
id1230958
size28,923
FancyFlame (Fancyflame)

documentation

README

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!()
    };
}

Commit count: 3

cargo fmt