16 releases (8 breaking)

0.9.1 Mar 23, 2024
0.8.1 Sep 27, 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 15909/week @ 2023-12-23 32104/week @ 2023-12-30 44055/week @ 2024-01-06 45709/week @ 2024-01-13 44069/week @ 2024-01-20 38810/week @ 2024-01-27 43985/week @ 2024-02-03 43449/week @ 2024-02-10 44823/week @ 2024-02-17 51866/week @ 2024-02-24 53488/week @ 2024-03-02 44197/week @ 2024-03-09 43739/week @ 2024-03-16 51815/week @ 2024-03-23 60766/week @ 2024-03-30 50916/week @ 2024-04-06

214,585 downloads per month
Used in 180 crates (14 directly)

MIT/Apache

66KB
1K SLoC

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
~24K SLoC