1 unstable release

0.1.0 Feb 1, 2024

#235 in Procedural macros

Download history 18/week @ 2024-02-16 29/week @ 2024-02-23 22/week @ 2024-03-01 29/week @ 2024-03-08 36/week @ 2024-03-15 30/week @ 2024-03-22 54/week @ 2024-03-29 18/week @ 2024-04-05

140 downloads per month
Used in 4 crates (2 directly)

MIT/Apache

32KB
747 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

~0.4–0.9MB
~20K SLoC