|
1 |
| -use syn::{spanned::Spanned, Attribute, Error, Lit, LitBool, Meta, NestedMeta, Path, Result}; |
| 1 | +use syn::{Attribute, Error, Lit, LitBool, Path, Result, spanned::Spanned}; |
2 | 2 |
|
3 | 3 | pub fn get_attributes(attrs: &[Attribute]) -> Result<Vec<(Path, Lit)>> {
|
4 | 4 | let mut attributes = Vec::new();
|
5 | 5 |
|
6 | 6 | for attribute in attrs {
|
7 |
| - if !attribute.path.is_ident("table") { |
| 7 | + if !attribute.path().is_ident("table") { |
8 | 8 | continue;
|
9 | 9 | }
|
10 | 10 |
|
11 |
| - let meta = attribute.parse_meta()?; |
12 |
| - |
13 |
| - let meta_list = match meta { |
14 |
| - Meta::List(meta_list) => Ok(meta_list), |
15 |
| - bad => Err(Error::new_spanned( |
16 |
| - bad, |
17 |
| - "Attributes should be of type: #[table(key = \"value\", ..)]", |
18 |
| - )), |
19 |
| - }?; |
20 |
| - |
21 |
| - for nested_meta in meta_list.nested.into_iter() { |
22 |
| - let meta = match nested_meta { |
23 |
| - NestedMeta::Meta(meta) => Ok(meta), |
24 |
| - bad => Err(Error::new_spanned( |
25 |
| - bad, |
26 |
| - "Attributes should be of type: #[table(key = \"value\", ..)]", |
27 |
| - )), |
28 |
| - }?; |
29 |
| - |
30 |
| - match meta { |
31 |
| - Meta::Path(path) => { |
32 |
| - let lit = LitBool { |
| 11 | + if attribute |
| 12 | + .parse_nested_meta(|meta| { |
| 13 | + let path = meta.path.clone(); |
| 14 | + let lit = meta |
| 15 | + .value() |
| 16 | + .ok() |
| 17 | + .map(|v| v.parse()) |
| 18 | + .transpose()? |
| 19 | + .unwrap_or(Lit::from(LitBool { |
33 | 20 | value: true,
|
34 | 21 | span: path.span(),
|
35 |
| - } |
36 |
| - .into(); |
37 |
| - attributes.push((path, lit)); |
38 |
| - } |
39 |
| - Meta::NameValue(meta_name_value) => { |
40 |
| - attributes.push((meta_name_value.path, meta_name_value.lit)); |
41 |
| - } |
42 |
| - bad => return Err(Error::new_spanned( |
43 |
| - bad, |
44 |
| - "Attributes should be of type: #[table(key = \"value\", ..)] or #[table(bool)]", |
45 |
| - )), |
46 |
| - } |
| 22 | + })); |
| 23 | + |
| 24 | + attributes.push((path, lit)); |
| 25 | + |
| 26 | + Ok(()) |
| 27 | + }) |
| 28 | + .is_err() |
| 29 | + { |
| 30 | + return Err(Error::new_spanned( |
| 31 | + attribute, |
| 32 | + "Attributes should be of type: #[table(key = \"value\", ..)] or #[table(bool)]", |
| 33 | + )); |
47 | 34 | }
|
48 | 35 | }
|
49 | 36 |
|
|
0 commit comments