4 releases

0.1.3 Oct 12, 2024
0.1.2 May 28, 2024
0.1.1 May 7, 2024
0.1.0 Feb 1, 2024

#2398 in Procedural macros

Download history 11/week @ 2024-08-17 54/week @ 2024-08-24 36/week @ 2024-08-31 18/week @ 2024-09-07 21/week @ 2024-09-14 45/week @ 2024-09-21 39/week @ 2024-09-28 16/week @ 2024-10-05 279/week @ 2024-10-12 112/week @ 2024-10-19 64/week @ 2024-10-26 54/week @ 2024-11-02 27/week @ 2024-11-09 33/week @ 2024-11-16 50/week @ 2024-11-23 54/week @ 2024-11-30

168 downloads per month
Used in 8 crates (via from-attr)

MIT/Apache

31KB
698 lines

from-attr

Crates.io version docs.rs docs

This crate provides some derive macros for parsing values from attributes.

Inspired

This crate inspired by attribute-derive.

Example

use from_attr::FromAttr;
use syn::{parse_quote, Expr, LitStr, Type};

#[derive(FromAttr)]
#[attribute(idents = [test])]
struct Test {
    a: LitStr,
    b: Option<String>,
    c: Type,
    d: Expr,
    e: Vec<Type>,
    f: bool,
    g: bool,
}

let attrs = [
    parse_quote!(#[test(a = "a", b = "b", c = (), d = if true { "a" } else { "b" }, e = [(), Debug], f)]),
];

let test = Test::from_attributes(&attrs).unwrap().unwrap().value;

assert_eq!(test.a.value(), "a");
assert_eq!(test.b.unwrap(), "b");
assert!(matches!(test.c, Type::Tuple(_)));
assert!(matches!(test.d, Expr::If(_)));
assert!(test.e.len() == 2);
assert!(test.f);
assert!(!test.g);

More examples can be found in the examples directories.

Dependencies

~310–750KB
~17K SLoC