from_attr/macros.rs
1/// Implement the [`ConvertParsed<Type = syn::MetaList>`](crate::ConvertParsed) trait for the type.
2///
3/// Requires that the type has implemented the [`FromAttr`](trait@crate::FromAttr) and [`AttributeIdent`](crate::AttributeIdent) traits.
4///
5/// Generally used for parsing nested attributes.
6///
7/// # Example
8///
9/// ```rust
10/// use from_attr::{FromAttr, convert_parsed_from_meta_list};
11/// use syn::parse_quote;
12///
13/// #[derive(FromAttr, PartialEq, Eq, Debug)]
14/// #[attribute(idents = [inner])]
15/// struct Inner {
16/// a: usize,
17/// }
18///
19/// convert_parsed_from_meta_list!(Inner);
20///
21/// #[derive(FromAttr, PartialEq, Eq, Debug)]
22/// #[attribute(idents = [outer])]
23/// struct Outer {
24/// a: usize,
25/// b: Inner,
26/// }
27///
28/// let attrs = [parse_quote!(#[outer(a = 1, b = inner(a = 10))])];
29///
30/// assert_eq!(
31/// Outer::from_attributes(&attrs).unwrap().unwrap().value,
32/// Outer {
33/// a: 1,
34/// b: Inner { a: 10 }
35/// }
36/// );
37/// ```
38#[macro_export]
39macro_rules! convert_parsed_from_meta_list {
40 ($ty:ty) => {
41 impl $crate::ConvertParsed for $ty {
42 type Type = $crate::__internal::syn::MetaList;
43
44 fn convert(
45 path_value: $crate::PathValue<Self::Type>,
46 ) -> $crate::__internal::syn::Result<Self> {
47 match <Self as $crate::FromAttr>::from_meta_list(&path_value.value)? {
48 Some(a) => Ok(a),
49 None => {
50 let idents = <Self as $crate::AttributeIdent>::IDENTS
51 .iter()
52 .map(|ident| format!("`{}`", *ident))
53 .collect::<::std::vec::Vec<_>>()
54 .join(", ");
55
56 Err($crate::__internal::syn::Error::new(
57 path_value.path,
58 format!("expected idents: {}", idents),
59 ))
60 }
61 }
62 }
63 }
64 };
65}