3个版本
0.1.2 | 2024年5月28日 |
---|---|
0.1.1 | 2024年5月7日 |
0.1.0 | 2024年2月1日 |
#1537 在 过程宏
每月 131 次下载
用于 7 个crate(直接使用2个)
5KB
from-attr
此crate提供了一些从属性中解析值的derive宏。
灵感来源
此crate受到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);
更多示例可以在示例目录中找到。
依赖项
~255–680KB
~16K SLoC