ruma_macros/serde/
attr.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
use syn::{
    parse::{Parse, ParseStream},
    LitStr, Token,
};

use super::case::RenameRule;

mod kw {
    syn::custom_keyword!(alias);
    syn::custom_keyword!(rename);
    syn::custom_keyword!(rename_all);
}

#[derive(Default)]
pub struct EnumAttrs {
    pub rename: Option<LitStr>,
    pub aliases: Vec<LitStr>,
}

pub enum Attr {
    Alias(LitStr),
    Rename(LitStr),
}

impl Parse for Attr {
    fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
        let lookahead = input.lookahead1();
        if lookahead.peek(kw::alias) {
            let _: kw::alias = input.parse()?;
            let _: Token![=] = input.parse()?;
            Ok(Self::Alias(input.parse()?))
        } else if lookahead.peek(kw::rename) {
            let _: kw::rename = input.parse()?;
            let _: Token![=] = input.parse()?;
            Ok(Self::Rename(input.parse()?))
        } else {
            Err(lookahead.error())
        }
    }
}

pub struct RenameAllAttr(RenameRule);

impl RenameAllAttr {
    pub fn into_inner(self) -> RenameRule {
        self.0
    }
}

impl Parse for RenameAllAttr {
    fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
        let _: kw::rename_all = input.parse()?;
        let _: Token![=] = input.parse()?;
        let s: LitStr = input.parse()?;
        Ok(Self(
            s.value()
                .parse()
                .map_err(|_| syn::Error::new_spanned(s, "invalid value for rename_all"))?,
        ))
    }
}