20 releases

0.10.2 Oct 30, 2024
0.9.2 Jun 22, 2024
0.9.1 Mar 23, 2024
0.8.1 Sep 27, 2023
0.2.2 Feb 12, 2022

#21 in Procedural macros

Download history 87453/week @ 2024-07-28 82111/week @ 2024-08-04 108898/week @ 2024-08-11 119138/week @ 2024-08-18 112834/week @ 2024-08-25 101126/week @ 2024-09-01 137088/week @ 2024-09-08 109570/week @ 2024-09-15 135447/week @ 2024-09-22 129849/week @ 2024-09-29 166746/week @ 2024-10-06 139185/week @ 2024-10-13 161729/week @ 2024-10-20 167743/week @ 2024-10-27 169186/week @ 2024-11-03 164654/week @ 2024-11-10

667,635 downloads per month
Used in 262 crates (19 directly)

MIT/Apache

67KB
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.1MB
~23K SLoC