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
use crate::syn_err;
pub trait Inflectable {
    fn inflect(&self, inflection: Inflection) -> String;
}

impl Inflectable for &str {
    fn inflect(&self, inflection: Inflection) -> String {
        inflection.apply(self)
    }
}

impl Inflectable for String {
    fn inflect(&self, inflection: Inflection) -> String {
        inflection.apply(self.as_str())
    }
}

#[derive(Copy, Clone, Debug)]
pub enum Inflection {
    Lower,
    Upper,
    Camel,
    Snake,
    Pascal,
    ScreamingSnake,
    Kebab,
    None,
}

impl Default for Inflection {
    fn default() -> Self {
        Self::None
    }
}

impl Inflection {
    pub fn apply(self, string: &str) -> String {
        use inflector::Inflector;

        match self {
            Inflection::Lower => string.to_lowercase(),
            Inflection::Upper => string.to_uppercase(),
            Inflection::Camel => string.to_camel_case(),
            Inflection::Snake => string.to_snake_case(),
            Inflection::Pascal => string.to_pascal_case(),
            Inflection::ScreamingSnake => string.to_screaming_snake_case(),
            Inflection::Kebab => string.to_kebab_case(),
            Inflection::None => string.to_string(),
        }
    }
}

impl TryFrom<String> for Inflection {
    type Error = syn::Error;

    fn try_from(value: String) -> syn::Result<Self> {
        Ok(
            match &*value.to_lowercase().replace("_", "").replace("-", "") {
                "lowercase" => Self::Lower,
                "uppercase" => Self::Upper,
                "camelcase" => Self::Camel,
                "snakecase" => Self::Snake,
                "pascalcase" => Self::Pascal,
                "screamingsnakecase" => Self::ScreamingSnake,
                "kebabcase" => Self::Kebab,
                _ => syn_err!("invalid inflection: '{}'", value),
            },
        )
    }
}