dsl_auto_type/auto_type/
case.rs

1use {heck::*, proc_macro2::Span, syn::Ident};
2
3#[derive(Clone, Copy, PartialEq, Eq)]
4pub enum Case {
5    DoNotChange,
6    UpperCamel,
7    Pascal,
8    LowerCamel,
9    Snake,
10    ShoutySnake,
11}
12
13impl Case {
14    pub(crate) fn ident_with_case(self, ident: &Ident) -> syn::Ident {
15        let s = ident.to_string();
16        let cased_s: String = match self {
17            Case::DoNotChange => s,
18            Case::UpperCamel => s.to_upper_camel_case(),
19            Case::Pascal => s.to_pascal_case(),
20            Case::LowerCamel => s.to_lower_camel_case(),
21            Case::Snake => s.to_snake_case(),
22            Case::ShoutySnake => s.to_shouty_snake_case(),
23        };
24        Ident::new(&cased_s, ident.span())
25    }
26}
27
28impl Case {
29    pub(crate) fn from_str(s: &str, span: Span) -> Result<Self, syn::Error> {
30        Ok(match s {
31            "dO_nOt_cHaNgE_cAsE" => Case::DoNotChange,
32            "UpperCamelCase" => Case::UpperCamel,
33            "PascalCase" => Case::Pascal,
34            "lowerCamelCase" => Case::LowerCamel,
35            "snake_case" => Case::Snake,
36            "SHOUTY_SNAKE_CASE" => Case::ShoutySnake,
37            other => {
38                return Err(syn::Error::new(
39                    span,
40                    format_args!(
41                        "Unknown case: {other}, expected one of: \
42                            `PascalCase`, `snake_case`, `UpperCamelCase`, `lowerCamelCase`, \
43                            `SHOUTY_SNAKE_CASE`, `dO_nOt_cHaNgE_cAsE`"
44                    ),
45                ))
46            }
47        })
48    }
49}