ruma_macros/serde/
enum_as_ref_str.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
use proc_macro2::TokenStream;
use quote::{quote, ToTokens};
use syn::{Fields, FieldsNamed, FieldsUnnamed, ItemEnum};

use super::{
    attr::EnumAttrs,
    util::{get_enum_attributes, get_rename_rule},
};

pub fn expand_enum_as_ref_str(input: &ItemEnum) -> syn::Result<TokenStream> {
    let enum_name = &input.ident;
    let rename_rule = get_rename_rule(input)?;
    let branches: Vec<_> = input
        .variants
        .iter()
        .map(|v| {
            let variant_name = &v.ident;
            let EnumAttrs { rename, .. } = get_enum_attributes(v)?;
            let (field_capture, variant_str) = match (rename, &v.fields) {
                (None, Fields::Unit) => (
                    None,
                    rename_rule.apply_to_variant(&variant_name.to_string()).into_token_stream(),
                ),
                (Some(rename), Fields::Unit) => (None, rename.into_token_stream()),
                (None, Fields::Named(FieldsNamed { named: fields, .. }))
                | (None, Fields::Unnamed(FieldsUnnamed { unnamed: fields, .. })) => {
                    if fields.len() != 1 {
                        return Err(syn::Error::new_spanned(
                            v,
                            "multiple data fields are not supported",
                        ));
                    }

                    let capture = match &fields[0].ident {
                        Some(name) => quote! { { #name: inner } },
                        None => quote! { (inner) },
                    };

                    (Some(capture), quote! { &inner.0 })
                }
                (Some(_), _) => {
                    return Err(syn::Error::new_spanned(
                        v,
                        "ruma_enum(rename) is only allowed on unit variants",
                    ));
                }
            };

            Ok(quote! {
                #enum_name :: #variant_name #field_capture => #variant_str
            })
        })
        .collect::<syn::Result<_>>()?;

    Ok(quote! {
        #[automatically_derived]
        #[allow(deprecated)]
        impl ::std::convert::AsRef<::std::primitive::str> for #enum_name {
            fn as_ref(&self) -> &::std::primitive::str {
                match self { #(#branches),* }
            }
        }
    })
}