arri_common/
enum_transformation.rs1use std::str::FromStr;
2
3#[derive(Debug, Eq, PartialEq, Clone)]
9pub enum EnumTransformation {
10 Uppercase,
12 Lowercase,
14 Snakecase,
16 Camelcase,
18 Pascalcase,
20}
21
22impl EnumTransformation {
23 pub fn apply(&self, value: &str) -> String {
33 use heck::{ToLowerCamelCase, ToPascalCase, ToSnakeCase};
34
35 match self {
36 Self::Uppercase => value.to_uppercase(),
37 Self::Lowercase => value.to_lowercase(),
38 Self::Snakecase => value.to_snake_case(),
39 Self::Camelcase => value.to_lower_camel_case(),
40 Self::Pascalcase => value.to_pascal_case(),
41 }
42 }
43}
44
45impl FromStr for EnumTransformation {
46 type Err = String;
47 fn from_str(s: &str) -> Result<Self, Self::Err> {
48 Self::try_from(s)
49 }
50}
51
52impl TryFrom<String> for EnumTransformation {
53 type Error = String;
54
55 fn try_from(value: String) -> Result<Self, Self::Error> {
56 Self::try_from(value.as_str())
57 }
58}
59
60impl TryFrom<&str> for EnumTransformation {
61 type Error = String;
62
63 fn try_from(value: &str) -> Result<Self, Self::Error> {
64 let normalized = value
65 .replace([' ', '-', '_'], "")
66 .to_lowercase()
67 .trim_end_matches("case")
68 .to_string();
69
70 Ok(match normalized.as_str() {
71 "upper" => Self::Uppercase,
72 "lower" => Self::Lowercase,
73 "snake" => Self::Snakecase,
74 "camel" => Self::Camelcase,
75 "pascal" => Self::Pascalcase,
76 _ => return Err(format!("Unknown transformation: {}", value)),
77 })
78 }
79}
80
81#[cfg(test)]
82mod tests {
83 use super::*;
84
85 #[test]
86 fn test_to_snake_case() {
87 let helper = EnumTransformation::Snakecase;
88 assert_eq!(helper.apply("hello world"), "hello_world");
89 assert_eq!(helper.apply("helloWorld"), "hello_world");
90 assert_eq!(helper.apply("HelloWorld"), "hello_world");
91 assert_eq!(helper.apply("hello_world"), "hello_world");
92 }
93
94 #[test]
95 fn test_to_camel_case() {
96 let helper = EnumTransformation::Camelcase;
97 assert_eq!(helper.apply("hello world"), "helloWorld");
98 assert_eq!(helper.apply("hello_world"), "helloWorld");
99 assert_eq!(helper.apply("helloWorld"), "helloWorld");
100 assert_eq!(helper.apply("HelloWorld"), "helloWorld");
101 }
102
103 #[test]
104 fn test_to_pascal_case() {
105 let helper = EnumTransformation::Pascalcase;
106 assert_eq!(helper.apply("hello world"), "HelloWorld");
107 assert_eq!(helper.apply("hello_world"), "HelloWorld");
108 assert_eq!(helper.apply("helloWorld"), "HelloWorld");
109 assert_eq!(helper.apply("HelloWorld"), "HelloWorld");
110 }
111
112 #[test]
113 fn test_to_uppercase() {
114 let helper = EnumTransformation::Uppercase;
115 assert_eq!(helper.apply("hello world"), "HELLO WORLD");
116 assert_eq!(helper.apply("hello_world"), "HELLO_WORLD");
117 assert_eq!(helper.apply("helloWorld"), "HELLOWORLD");
118 assert_eq!(helper.apply("HelloWorld"), "HELLOWORLD");
119 }
120
121 #[test]
122 fn test_to_lowercase() {
123 let helper = EnumTransformation::Lowercase;
124 assert_eq!(helper.apply("hello world"), "hello world");
125 assert_eq!(helper.apply("hello_world"), "hello_world");
126 assert_eq!(helper.apply("helloWorld"), "helloworld");
127 assert_eq!(helper.apply("HelloWorld"), "helloworld");
128 }
129
130 #[test]
131 fn parse_aliases() {
132 assert_eq!(
133 EnumTransformation::try_from("camel").unwrap(),
134 EnumTransformation::Camelcase
135 );
136 assert_eq!(
137 EnumTransformation::try_from("pascal").unwrap(),
138 EnumTransformation::Pascalcase
139 );
140 assert_eq!(
141 EnumTransformation::try_from("snake").unwrap(),
142 EnumTransformation::Snakecase
143 );
144 assert_eq!(
145 EnumTransformation::try_from("upper").unwrap(),
146 EnumTransformation::Uppercase
147 );
148 assert_eq!(
149 EnumTransformation::try_from("lower").unwrap(),
150 EnumTransformation::Lowercase
151 );
152 }
153}