attribute-derive

docs.rs lib.rs MIT

Basicly clap for attribute macros: ```rust

[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, #[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, // 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: ```rust

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

```

Limitations

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

Parse methods

There are multiple ways of parsing a struct deriving Attribute.

For helper attributes there is: - Attribute::from_attributes which takes in an IntoIterator<Item = &'a syn::Attribute (e.g. a &Vec<syn::Attribute>). Most useful for derive macros. - Attribute::remove_attributes which takes an &mut Vec<syn::Attribute> and does not only parse the Attribute but also removes those matching. Useful for helper attributes for proc macros, where the helper attributes need to be removed.

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