tabled_derive/
parse.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use proc_macro2::{Ident, Span};
use syn::{
    parenthesized, parse::Parse, punctuated::Punctuated, token, Attribute, LitBool, LitInt, LitStr,
    Token,
};

pub fn parse_attributes(
    attributes: &[Attribute],
) -> impl Iterator<Item = syn::Result<impl Iterator<Item = TabledAttr>>> + '_ {
    attributes
        .iter()
        .filter(|attr| attr.path.is_ident("tabled"))
        .map(|attr| attr.parse_args_with(Punctuated::<TabledAttr, Token![,]>::parse_terminated))
        .map(|result| result.map(IntoIterator::into_iter))
}

pub struct TabledAttr {
    pub ident: Ident,
    pub kind: TabledAttrKind,
}

impl TabledAttr {
    pub fn new(ident: Ident, kind: TabledAttrKind) -> Self {
        Self { ident, kind }
    }
}

#[derive(Clone)]
pub enum TabledAttrKind {
    Skip(LitBool),
    Inline(LitBool, Option<LitStr>),
    Rename(LitStr),
    RenameAll(LitStr),
    DisplayWith(LitStr, bool),
    Order(LitInt),
}

impl Parse for TabledAttr {
    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
        use TabledAttrKind::*;

        let name: Ident = input.parse()?;
        let name_str = name.to_string();

        if input.peek(Token![=]) {
            let assign_token = input.parse::<Token![=]>()?;

            if input.peek(LitStr) {
                let lit = input.parse::<LitStr>()?;

                match name_str.as_str() {
                    "rename" => return Ok(Self::new(name, Rename(lit))),
                    "rename_all" => return Ok(Self::new(name, RenameAll(lit))),
                    "display_with" => return Ok(Self::new(name, DisplayWith(lit, false))),
                    _ => {}
                }
            }

            if input.peek(LitBool) {
                let lit = input.parse::<LitBool>()?;

                match name_str.as_str() {
                    "skip" => return Ok(Self::new(name, Skip(lit))),
                    "inline" => return Ok(Self::new(name, Inline(lit, None))),
                    _ => {}
                }
            }

            if input.peek(LitInt) {
                let lit = input.parse::<LitInt>()?;

                if let "order" = name_str.as_str() {
                    return Ok(Self::new(name, Order(lit)));
                }
            }

            return Err(syn::Error::new(
                assign_token.span,
                "expected `string literal` or `expression` after `=`",
            ));
        }

        if input.peek(token::Paren) {
            let nested;
            let _paren = parenthesized!(nested in input);

            if nested.peek(LitStr) {
                let lit = nested.parse::<LitStr>()?;

                match name_str.as_str() {
                    "display_with" => {
                        let use_self = if nested.peek(Token![,]) {
                            let _comma = nested.parse::<Token![,]>()?;
                            if nested.peek(syn::Ident) {
                                let ident = nested.parse::<syn::Ident>()?;
                                ident == "args"
                            } else {
                                false
                            }
                        } else {
                            false
                        };

                        return Ok(Self::new(name, DisplayWith(lit, use_self)));
                    }
                    "inline" => {
                        return Ok(Self::new(
                            name,
                            Inline(LitBool::new(true, Span::call_site()), Some(lit)),
                        ))
                    }
                    _ => {}
                }
            }

            return Err(syn::Error::new(
                _paren.span,
                "expected a `string literal` in parenthesis",
            ));
        }

        match name_str.as_str() {
            "skip" => return Ok(Self::new(name, Skip(LitBool::new(true, Span::call_site())))),
            "inline" => {
                return Ok(Self::new(
                    name,
                    Inline(LitBool::new(true, Span::call_site()), None),
                ))
            }
            _ => {}
        }

        Err(syn::Error::new(
            name.span(),
            format!("unexpected attribute: {}", name_str),
        ))
    }
}