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

#868 in Procedural macros

Download history 101/week @ 2025-11-06 119/week @ 2025-11-13 63/week @ 2025-11-20 78/week @ 2025-11-27 44/week @ 2025-12-04 34/week @ 2025-12-11 57/week @ 2025-12-18 66/week @ 2025-12-25 49/week @ 2026-01-01 26/week @ 2026-01-08 31/week @ 2026-01-15 68/week @ 2026-01-22 53/week @ 2026-01-29 30/week @ 2026-02-05 11/week @ 2026-02-12 57/week @ 2026-02-19

155 downloads per month
Used in 16 crates (6 directly)

MIT/Apache

34KB
810 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

~215–600KB
~13K SLoC