6 releases

Uses new Rust 2021

0.3.1 Apr 21, 2022
0.3.0 Apr 21, 2022
0.2.2 Feb 12, 2022
0.1.1 Feb 6, 2022

#148 in Procedural macros

Download history 79/week @ 2022-08-07 61/week @ 2022-08-14 53/week @ 2022-08-21 119/week @ 2022-08-28 66/week @ 2022-09-04 60/week @ 2022-09-11 66/week @ 2022-09-18 67/week @ 2022-09-25 128/week @ 2022-10-02 84/week @ 2022-10-09 176/week @ 2022-10-16 115/week @ 2022-10-23 90/week @ 2022-10-30 113/week @ 2022-11-06 61/week @ 2022-11-13 82/week @ 2022-11-20

362 downloads per month
Used in 10 crates (2 directly)

MIT license

29KB
527 lines

attribute-derive

docs.rs lib.rs MIT

Basicly clap for attribute macros:

#[derive(Attribute)]
#[attribute(ident = "collection")]
#[attribute(invalid_field = "Error when an unsupported value is set (e.g. meaning=42")]
struct CollectionAttribute {
    // Options are optional by default (will be set to None if not specified)
    authority: Option<String>,
    #[attribute(missing = "Error when the value is not set")]
    name: String,
    // Any type implementing default can be flagged as default
    // This will be set to Vec::default() when not specified
    #[attribute(default)]
    #[attribute(expected = "Error when an error occured while parsing")]
    views: Vec<Type>,
    // Booleans can be used without assiging a value. as a flag.
    // If omitted they are set to false
    some_flag
}

Will be able to parse an attribute like this:

#[collection(authority="Some String", name = r#"Another string"#, views = [Option, ()])]

Limitations

There are some limitations in syntax parsing that will be lifted future releases.

  • literals in top level (meaning something like #[attr(42, 3.14, "hi")]
  • function like arguments (something like #[attr(view(a = "test"))]
  • other syntaxes, maybe something like key: value

Parse methods

There are multiple ways of parsing a struct deriving Attribute.

For helper attributes there is:

For parsing a single TokenStream e.g. for parsing the proc macro input there a two ways:

Dependencies

~255–670KB
~15K SLoC