21 releases

0.10.3 Nov 17, 2024
0.10.1 Aug 27, 2024
0.9.2 Jun 22, 2024
0.9.1 Mar 23, 2024
0.2.2 Feb 12, 2022

#20 in Procedural macros

Download history 182496/week @ 2024-10-24 148532/week @ 2024-10-31 171238/week @ 2024-11-07 151154/week @ 2024-11-14 127305/week @ 2024-11-21 66236/week @ 2024-11-28 136577/week @ 2024-12-05 153753/week @ 2024-12-12 60064/week @ 2024-12-19 34213/week @ 2024-12-26 135113/week @ 2025-01-02 165224/week @ 2025-01-09 121638/week @ 2025-01-16 135731/week @ 2025-01-23 137885/week @ 2025-01-30 154852/week @ 2025-02-06

579,469 downloads per month
Used in 283 crates (21 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.6–1.1MB
~22K SLoC