Crates.io | from-attr |
lib.rs | from-attr |
version | 0.1.3 |
source | src |
created_at | 2024-02-01 11:35:16.623847 |
updated_at | 2024-10-12 06:26:56.812164 |
description | A crate that makes it easy to parse attributes. |
homepage | https://github.com/ZihanType/from-attr |
repository | https://github.com/ZihanType/from-attr |
max_upload_size | |
id | 1122996 |
size | 29,659 |
This crate provides some derive macros for parsing values from attributes.
This crate inspired by attribute-derive.
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.