cli_table_derive/
utils.rs

1use syn::{Attribute, Error, Lit, LitBool, Path, Result, spanned::Spanned};
2
3pub fn get_attributes(attrs: &[Attribute]) -> Result<Vec<(Path, Lit)>> {
4    let mut attributes = Vec::new();
5
6    for attribute in attrs {
7        if !attribute.path().is_ident("table") {
8            continue;
9        }
10
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 {
20                        value: true,
21                        span: path.span(),
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            ));
34        }
35    }
36
37    Ok(attributes)
38}