ruma_macros/serde/
attr.rs

1use syn::{
2    parse::{Parse, ParseStream},
3    LitStr, Token,
4};
5
6use super::case::RenameRule;
7
8mod kw {
9    syn::custom_keyword!(alias);
10    syn::custom_keyword!(rename);
11    syn::custom_keyword!(rename_all);
12}
13
14#[derive(Default)]
15pub struct EnumAttrs {
16    pub rename: Option<LitStr>,
17    pub aliases: Vec<LitStr>,
18}
19
20pub enum Attr {
21    Alias(LitStr),
22    Rename(LitStr),
23}
24
25impl Parse for Attr {
26    fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
27        let lookahead = input.lookahead1();
28        if lookahead.peek(kw::alias) {
29            let _: kw::alias = input.parse()?;
30            let _: Token![=] = input.parse()?;
31            Ok(Self::Alias(input.parse()?))
32        } else if lookahead.peek(kw::rename) {
33            let _: kw::rename = input.parse()?;
34            let _: Token![=] = input.parse()?;
35            Ok(Self::Rename(input.parse()?))
36        } else {
37            Err(lookahead.error())
38        }
39    }
40}
41
42pub struct RenameAllAttr(RenameRule);
43
44impl RenameAllAttr {
45    pub fn into_inner(self) -> RenameRule {
46        self.0
47    }
48}
49
50impl Parse for RenameAllAttr {
51    fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
52        let _: kw::rename_all = input.parse()?;
53        let _: Token![=] = input.parse()?;
54        let s: LitStr = input.parse()?;
55        Ok(Self(
56            s.value()
57                .parse()
58                .map_err(|_| syn::Error::new_spanned(s, "invalid value for rename_all"))?,
59        ))
60    }
61}