1#[cfg(test)]
2use strum_macros::EnumIter;
3
4/// Defines the type of casing a string can be.
5///
6/// ```
7/// use convert_case::{Case, Casing};
8///
9/// let super_mario_title: String = "super_mario_64".to_case(Case::Title);
10/// assert_eq!("Super Mario 64", super_mario_title);
11/// ```
12#[cfg_attr(test, derive(EnumIter))]
13#[derive(Eq, PartialEq, Clone, Copy, Debug)]
14pub enum Case {
15
16 /// Uppercase strings are delimited by spaces and all characters are uppercase.
17 ///
18 /// ```
19 /// use convert_case::{Case, Casing};
20 /// assert_eq!("MY VARIABLE NAME", "My variable NAME".to_case(Case::Upper))
21 /// ```
22 Upper,
23
24 /// Lowercase strings are delimited by spaces and all characters are lowercase.
25 ///
26 /// ```
27 /// use convert_case::{Case, Casing};
28 /// assert_eq!("my variable name", "My variable NAME".to_case(Case::Lower))
29 /// ```
30 Lower,
31
32 /// Title case strings are delimited by spaces. Only the leading character of
33 /// each word is uppercase. No inferences are made about language, so words
34 /// like "as", "to", and "for" will still be capitalized.
35 ///
36 /// ```
37 /// use convert_case::{Case, Casing};
38 /// assert_eq!("My Variable Name", "My variable NAME".to_case(Case::Title))
39 /// ```
40 Title,
41
42 /// Toggle case strings are delimited by spaces. All characters are uppercase except
43 /// for the leading character of each word, which is lowercase.
44 ///
45 /// ```
46 /// use convert_case::{Case, Casing};
47 /// assert_eq!("mY vARIABLE nAME", "My variable NAME".to_case(Case::Toggle))
48 /// ```
49 Toggle,
50
51 /// Camel case strings are lowercase, but for every word _except the first_ the
52 /// first letter is capitalized.
53 ///
54 /// ```
55 /// use convert_case::{Case, Casing};
56 /// assert_eq!("myVariableName", "My variable NAME".to_case(Case::Camel))
57 /// ```
58 Camel,
59
60 /// Pascal case strings are lowercase, but for every word the
61 /// first letter is capitalized.
62 ///
63 /// ```
64 /// use convert_case::{Case, Casing};
65 /// assert_eq!("MyVariableName", "My variable NAME".to_case(Case::Pascal))
66 /// ```
67 Pascal,
68
69 /// Upper camel case is an alternative name for Pascal case.
70 UpperCamel,
71
72 /// Snake case strings are delimited by underscores `_` and are all lowercase.
73 ///
74 /// ```
75 /// use convert_case::{Case, Casing};
76 /// assert_eq!("my_variable_name", "My variable NAME".to_case(Case::Snake))
77 /// ```
78 Snake,
79
80 /// Upper snake case strings are delimited by underscores `_` and are all uppercase.
81 ///
82 /// ```
83 /// use convert_case::{Case, Casing};
84 /// assert_eq!("MY_VARIABLE_NAME", "My variable NAME".to_case(Case::UpperSnake))
85 /// ```
86 UpperSnake,
87
88 /// Screaming snake case is an alternative name for upper snake case.
89 ScreamingSnake,
90
91 /// Kebab case strings are delimited by hyphens `-` and are all lowercase.
92 ///
93 /// ```
94 /// use convert_case::{Case, Casing};
95 /// assert_eq!("my-variable-name", "My variable NAME".to_case(Case::Kebab))
96 /// ```
97 Kebab,
98
99 /// Cobol case strings are delimited by hyphens `-` and are all uppercase.
100 ///
101 /// ```
102 /// use convert_case::{Case, Casing};
103 /// assert_eq!("MY-VARIABLE-NAME", "My variable NAME".to_case(Case::Cobol))
104 /// ```
105 Cobol,
106
107 /// Train case strings are delimited by hyphens `-`. All characters are lowercase
108 /// except for the leading character of each word.
109 ///
110 /// ```
111 /// use convert_case::{Case, Casing};
112 /// assert_eq!("My-Variable-Name", "My variable NAME".to_case(Case::Train))
113 /// ```
114 Train,
115
116 /// Flat case strings are all lowercase, with no delimiter. Converting to
117 /// this case is **lossy**. That is, word boundaries are lost.
118 ///
119 /// ```
120 /// use convert_case::{Case, Casing};
121 /// assert_eq!("myvariablename", "My variable NAME".to_case(Case::Flat))
122 /// ```
123 Flat,
124
125 /// Upper flat case strings are all uppercase, with no delimiter. Converting to
126 /// this case is **lossy**. That is, word boundaries are lost.
127 ///
128 /// ```
129 /// use convert_case::{Case, Casing};
130 /// assert_eq!("MYVARIABLENAME", "My variable NAME".to_case(Case::UpperFlat))
131 /// ```
132 UpperFlat,
133
134 /// Alternating case strings are delimited by spaces. Characters alternate between uppercase
135 /// and lowercase.
136 /// ```
137 /// use convert_case::{Case, Casing};
138 /// assert_eq!("mY vArIaBlE nAmE", "My variable NAME".to_case(Case::Alternating));
139 /// ```
140 Alternating,
141
142 /// Random case strings are delimited by spaces and characters are
143 /// randomly upper case or lower case. This uses the `rand` crate
144 /// and is only available with the "random" feature.
145 /// ```
146 /// use convert_case::{Case, Casing};
147 /// let new = "My variable NAME".to_case(Case::Random);
148 /// ```
149 /// `new` could be "My vaRIAbLE nAme" for example.
150 #[cfg(feature = "random")]
151 Random,
152
153 /// Pseudo-random case strings are delimited by spaces and characters are randomly
154 /// upper case or lower case, but there will never more than two consecutive lower
155 /// case or upper case letters in a row. This uses the `rand` crate and is
156 /// only available with the "random" feature.
157 /// ```
158 /// use convert_case::{Case, Casing};
159 /// let new = "My variable NAME".to_case(Case::Random);
160 /// ```
161 /// `new` could be "mY vArIAblE NamE" for example.
162 #[cfg(feature = "random")]
163 PseudoRandom,
164}
165
166impl Case {
167 // Created to avoid using the EnumIter trait from strum in
168 // final library. A test confirms that all cases are listed here.
169 /// Returns a vector with all case enum variants. This was
170 /// created for use in the `ccase` binary.
171 pub fn all_cases() -> Vec<Case> {
172 use Case::*;
173 vec![
174 Upper,
175 Lower,
176 Title,
177 Toggle,
178 Camel,
179 Pascal,
180 UpperCamel,
181 Snake,
182 UpperSnake,
183 ScreamingSnake,
184 Kebab,
185 Cobol,
186 Train,
187 Flat,
188 UpperFlat,
189 Alternating,
190
191 #[cfg(feature = "random")]
192 Random,
193 #[cfg(feature = "random")]
194 PseudoRandom,
195 ]
196 }
197}
198
199#[cfg(test)]
200mod test {
201
202 use super::*;
203 use strum::IntoEnumIterator;
204
205 #[test]
206 fn all_cases_in_iter() {
207 let all = Case::all_cases();
208 for case in Case::iter() {
209 assert!(all.contains(&case));
210 }
211 }
212}
213