14 releases (7 breaking)

0.8.1 Sep 27, 2023
0.7.1 Sep 17, 2023
0.6.1 May 21, 2023
0.6.0 Mar 20, 2023
0.2.2 Feb 12, 2022

#26 in Procedural macros

Download history 37703/week @ 2023-11-01 32929/week @ 2023-11-08 35576/week @ 2023-11-15 27302/week @ 2023-11-22 53571/week @ 2023-11-29 46560/week @ 2023-12-06 40975/week @ 2023-12-13 25895/week @ 2023-12-20 21219/week @ 2023-12-27 40679/week @ 2024-01-03 42976/week @ 2024-01-10 48774/week @ 2024-01-17 40649/week @ 2024-01-24 41112/week @ 2024-01-31 44833/week @ 2024-02-07 32172/week @ 2024-02-14

169,491 downloads per month
Used in 165 crates (13 directly)

MIT license

42KB
709 lines

attribute-derive

docs.rs lib.rs MIT Documentation for main

Basically clap for attribute macros:

use attribute_derive::Attribute;
use syn::Type;

#[derive(Attribute)]
#[attribute(ident = collection)]
#[attribute(error(missing_field = "`{field}` was not specified"))]
struct CollectionAttribute {
    // Options are optional by default (will be set to None if not specified)
    authority: Option<String>,
    name: String,
    // Any type implementing default can be flagged as default
    // This will be set to Vec::default() when not specified
    #[attribute(optional)]
    views: Vec<Type>,
    // Booleans can be used without assigning a value, i.e., as a flag.
    // If omitted they are set to false
    some_flag: bool,
}

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

~0.7–1.2MB
~25K SLoC