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