3 releases

0.1.2 May 28, 2024
0.1.1 May 7, 2024
0.1.0 Feb 1, 2024

#1506 in Procedural macros

Download history 33/week @ 2024-03-11 15/week @ 2024-03-18 43/week @ 2024-03-25 56/week @ 2024-04-01 19/week @ 2024-04-08 6/week @ 2024-04-15 9/week @ 2024-04-22 172/week @ 2024-05-06 41/week @ 2024-05-13 94/week @ 2024-05-20 206/week @ 2024-05-27 41/week @ 2024-06-03 61/week @ 2024-06-10 14/week @ 2024-06-17 29/week @ 2024-06-24

153 downloads per month
Used in 7 crates (2 directly)

MIT/Apache

5KB

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

~275–710KB
~17K SLoC