convert_case 0.11.0

Convert strings into any case
Documentation
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
use crate::boundary::{self, Boundary};
use crate::pattern::Pattern;

use alloc::string::String;
use alloc::vec::Vec;

/// Defines the case of an identifier.
/// ```
/// use convert_case::ccase;
/// assert_eq!(ccase!(title, "super_mario_64"), "Super Mario 64");
///
/// use convert_case::{Case, Casing};
/// assert_eq!("super_mario_64".to_case(Case::Title), "Super Mario 64");
/// ```
///
/// A case is the pair of a [pattern](Pattern) and a delimiter (a string).  Given
/// a list of words, a pattern describes how to mutate the words and a delimiter is how the mutated
/// words are joined together.
///
/// | pattern | underscore `_` | hyphen `-` | empty string | space |
/// | ---: | --- | --- | --- | --- |
/// | [lowercase](Pattern::Lowercase) | [snake_case](Case::Snake) | [kebab-case](Case::Kebab) | [flatcase](Case::Flat) | [lower case](Case::Lower) |
/// | [uppercase](Pattern::Uppercase) | [CONSTANT_CASE](Case::Constant) | [COBOL-CASE](Case::Cobol) | [UPPERFLATCASE](Case::UpperFlat) | [UPPER CASE](Case::Upper) |
/// | [capital](Pattern::Capital) | [Ada_Case](Case::Ada) | [Train-Case](Case::Train) | [PascalCase](Case::Pascal) | [Title Case](Case::Title) |
/// | [camel](Pattern::Camel) | | | [camelCase](Case::Camel) |
///
/// There are additionally [`Case::Sentence`].
///
/// This crate provides the ability to convert "from" a case.  This introduces a different feature
/// of cases which are the [word boundaries](Boundary) that segment the identifier into words.  For example, a
/// snake case identifier `my_var_name` can be split on underscores `_` to segment into words.  A
/// camel case identifier `myVarName` is split where a lowercase letter is followed by an
/// uppercase letter.  Each case is also associated with a list of boundaries that are used when
/// converting "from" a particular case.
#[derive(Eq, PartialEq, Hash, Clone, Copy, Debug)]
#[non_exhaustive]
pub enum Case<'b> {
    /// Custom cases can be delimited by any static string slice and mutate words
    /// using any pattern.  Further, they can use any list of boundaries for
    /// splitting identifiers into words.
    ///
    /// This flexibility can create cases not present as another variant of the
    /// Case enum.  For instance, you could create a "dot case" like so.
    /// ```
    /// use convert_case::{Case, Casing, separator, Pattern};
    /// let dot_case = Case::Custom {
    ///     boundaries: &[separator!(".")],
    ///     pattern: Pattern::Lowercase,
    ///     delimiter: ".",
    /// };
    ///
    /// assert_eq!(
    ///     "myNewCase".to_case(dot_case),
    ///     "my.new.case",
    /// );
    /// assert_eq!(
    ///     "my.new.case".from_case(dot_case).to_case(Case::Title),
    ///     "My New Case",
    /// );
    /// ```
    Custom {
        boundaries: &'b [Boundary],
        pattern: Pattern,
        delimiter: &'static str,
    },

    /// Snake case strings are delimited by underscores `_` and are all lowercase.
    ///
    /// * Boundaries : [Underscore](Boundary::Underscore)
    /// * Pattern : [Lowercase](Pattern::Lowercase)
    /// * Delimiter : Underscore `"_"`
    ///
    /// ```
    /// use convert_case::ccase;
    /// assert_eq!(ccase!(snake, "My variable NAME"), "my_variable_name");
    ///
    /// use convert_case::{Case, Casing};
    /// assert_eq!("My variable NAME".to_case(Case::Snake), "my_variable_name");
    /// ```
    Snake,

    /// Constant case strings are delimited by underscores `_` and are all uppercase.
    /// * Boundaries: [Underscore](Boundary::Underscore)
    /// * Pattern: [Uppercase](Pattern::Uppercase)
    /// * Delimiter: Underscore `"_"`
    ///
    /// ```
    /// use convert_case::ccase;
    /// assert_eq!(ccase!(constant, "My variable NAME"), "MY_VARIABLE_NAME");
    ///
    /// use convert_case::{Case, Casing};
    /// assert_eq!("My variable NAME".to_case(Case::Constant), "MY_VARIABLE_NAME");
    /// ```
    Constant,

    /// Upper snake case is an alternative name for [constant case](Case::Constant).
    UpperSnake,

    /// Ada case strings are delimited by underscores `_`.  The leading letter of
    /// each word is uppercase, while the rest is lowercase.
    /// * Boundaries: [Underscore](Boundary::Underscore)
    /// * Pattern: [Capital](Pattern::Capital)
    /// * Delimiter: Underscore `"_"`
    ///
    /// ```
    /// use convert_case::ccase;
    /// assert_eq!(ccase!(ada, "My variable NAME"), "My_Variable_Name");
    ///
    /// use convert_case::{Case, Casing};
    /// assert_eq!("My variable NAME".to_case(Case::Ada), "My_Variable_Name");
    /// ```
    Ada,

    /// Kebab case strings are delimited by hyphens `-` and are all lowercase.
    /// * Boundaries: [Hyphen](Boundary::Hyphen)
    /// * Pattern: [Lowercase](Pattern::Lowercase)
    /// * Delimiter: Hyphen `"-"`
    ///
    /// ```
    /// use convert_case::ccase;
    /// assert_eq!(ccase!(kebab, "My variable NAME"), "my-variable-name");
    ///
    /// use convert_case::{Case, Casing};
    /// assert_eq!("My variable NAME".to_case(Case::Kebab), "my-variable-name");
    /// ```
    Kebab,

    /// Cobol case strings are delimited by hyphens `-` and are all uppercase.
    /// * Boundaries: [Hyphen](Boundary::Hyphen)
    /// * Pattern: [Uppercase](Pattern::Uppercase)
    /// * Delimiter: Hyphen `"-"`
    ///
    /// ```
    /// use convert_case::ccase;
    /// assert_eq!(ccase!(cobol, "My variable NAME"), "MY-VARIABLE-NAME");
    ///
    /// use convert_case::{Case, Casing};
    /// assert_eq!("My variable NAME".to_case(Case::Cobol), "MY-VARIABLE-NAME");
    /// ```
    Cobol,

    /// Upper kebab case is an alternative name for [Cobol case](Case::Cobol).
    UpperKebab,

    /// Train case strings are delimited by hyphens `-`.  The leading letter of
    /// each word is uppercase, while the rest is lowercase.
    /// * Boundaries: [Hyphen](Boundary::Hyphen)
    /// * Pattern: [Capital](Pattern::Capital)
    /// * Delimiter: Hyphen `"-"`
    ///
    /// ```
    /// use convert_case::ccase;
    /// assert_eq!(ccase!(train, "My variable NAME"), "My-Variable-Name");
    ///
    /// use convert_case::{Case, Casing};
    /// assert_eq!("My variable NAME".to_case(Case::Train), "My-Variable-Name");
    /// ```
    Train,

    /// Flat case strings are all lowercase, with no delimiter. Note that word boundaries are lost.
    /// * Boundaries: No boundaries
    /// * Pattern: [Lowercase](Pattern::Lowercase)
    /// * Delimiter: Empty string `""`
    ///
    /// ```
    /// use convert_case::ccase;
    /// assert_eq!(ccase!(flat, "My variable NAME"), "myvariablename");
    ///
    /// use convert_case::{Case, Casing};
    /// assert_eq!("My variable NAME".to_case(Case::Flat), "myvariablename");
    /// ```
    Flat,

    /// Upper flat case strings are all uppercase, with no delimiter. Note that word boundaries are lost.
    /// * Boundaries: No boundaries
    /// * Pattern: [Uppercase](Pattern::Uppercase)
    /// * Delimiter: Empty string `""`
    ///
    /// ```
    /// use convert_case::ccase;
    /// assert_eq!(ccase!(upper_flat, "My variable NAME"), "MYVARIABLENAME");
    ///
    /// use convert_case::{Case, Casing};
    /// assert_eq!("My variable NAME".to_case(Case::UpperFlat), "MYVARIABLENAME");
    /// ```
    UpperFlat,

    /// Pascal case strings are lowercase, but for every word the
    /// first letter is capitalized.
    /// * Boundaries: [LowerUpper](Boundary::LowerUpper), [DigitUpper](Boundary::DigitUpper),
    ///   [UpperDigit](Boundary::UpperDigit), [DigitLower](Boundary::DigitLower),
    ///   [LowerDigit](Boundary::LowerDigit), [Acronym](Boundary::Acronym)
    /// * Pattern: [Capital](`Pattern::Capital`)
    /// * Delimiter: Empty string `""`
    ///
    /// ```
    /// use convert_case::ccase;
    /// assert_eq!(ccase!(pascal, "My variable NAME"), "MyVariableName");
    ///
    /// use convert_case::{Case, Casing};
    /// assert_eq!("My variable NAME".to_case(Case::Pascal), "MyVariableName");
    /// ```
    Pascal,

    /// Upper camel case is an alternative name for [Pascal case](Case::Pascal).
    UpperCamel,

    /// Camel case strings are lowercase, but for every word _except the first_ the
    /// first letter is capitalized.
    /// * Boundaries: [LowerUpper](Boundary::LowerUpper), [DigitUpper](Boundary::DigitUpper),
    ///   [UpperDigit](Boundary::UpperDigit), [DigitLower](Boundary::DigitLower),
    ///   [LowerDigit](Boundary::LowerDigit), [Acronym](Boundary::Acronym)
    /// * Pattern: [Camel](`Pattern::Camel`)
    /// * Delimiter: Empty string `""`
    ///
    /// ```
    /// use convert_case::ccase;
    /// assert_eq!(ccase!(camel, "My variable NAME"), "myVariableName");
    ///
    /// use convert_case::{Case, Casing};
    /// assert_eq!("My variable NAME".to_case(Case::Camel), "myVariableName");
    /// ```
    Camel,

    /// Lowercase strings are delimited by spaces and all characters are lowercase.
    /// * Boundaries: [Space](`Boundary::Space`)
    /// * Pattern: [Lowercase](`Pattern::Lowercase`)
    /// * Delimiter: Space `" "`
    ///
    /// ```
    /// use convert_case::ccase;
    /// assert_eq!(ccase!(lower, "My variable NAME"), "my variable name");
    ///
    /// use convert_case::{Case, Casing};
    /// assert_eq!("My variable NAME".to_case(Case::Lower), "my variable name");
    /// ```
    Lower,

    /// Uppercase strings are delimited by spaces and all characters are uppercase.
    /// * Boundaries: [Space](`Boundary::Space`)
    /// * Pattern: [Uppercase](`Pattern::Uppercase`)
    /// * Delimiter: Space `" "`
    ///
    /// ```
    /// use convert_case::ccase;
    /// assert_eq!(ccase!(upper, "My variable NAME"), "MY VARIABLE NAME");
    ///
    /// use convert_case::{Case, Casing};
    /// assert_eq!("My variable NAME".to_case(Case::Upper), "MY VARIABLE NAME");
    /// ```
    Upper,

    /// 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.
    /// * Boundaries: [Space](`Boundary::Space`)
    /// * Pattern: [Capital](`Pattern::Capital`)
    /// * Delimiter: Space `" "`
    ///
    /// ```
    /// use convert_case::ccase;
    /// assert_eq!(ccase!(title, "My variable NAME"), "My Variable Name");
    ///
    /// use convert_case::{Case, Casing};
    /// assert_eq!("My variable NAME".to_case(Case::Title), "My Variable Name");
    /// ```
    Title,

    /// Sentence case strings are delimited by spaces. Only the leading character of
    /// the first word is uppercase.
    /// * Boundaries: [Space](`Boundary::Space`)
    /// * Pattern: [Sentence](`Pattern::Sentence`)
    /// * Delimiter: Space `" "`
    ///
    /// ```
    /// use convert_case::ccase;
    /// assert_eq!(ccase!(sentence, "My variable NAME"), "My variable name");
    ///
    /// use convert_case::{Case, Casing};
    /// assert_eq!("My variable NAME".to_case(Case::Sentence), "My variable name");
    /// ```
    Sentence,
}

impl Case<'_> {
    /// Returns the boundaries used in the corresponding case.  That is, where can word boundaries
    /// be distinguished in a string of the given case.  The table outlines which cases use which
    /// set of boundaries.
    ///
    /// | Cases | Boundaries |
    /// | --- | --- |
    /// | Snake, Constant, UpperSnake, Ada | [Underscore](Boundary::Underscore)  |
    /// | Kebab, Cobol, UpperKebab, Train | [Hyphen](Boundary::Hyphen) |
    /// | Lower, Upper, Title | [Space](Boundary::Space) |
    /// | Pascal, UpperCamel, Camel | [LowerUpper](Boundary::LowerUpper), [LowerDigit](Boundary::LowerDigit), [UpperDigit](Boundary::UpperDigit), [DigitLower](Boundary::DigitLower), [DigitUpper](Boundary::DigitUpper), [Acronym](Boundary::Acronym) |
    /// | Flat, UpperFlat | No boundaries |
    pub fn boundaries(&self) -> &[Boundary] {
        use Case::*;
        match self {
            Snake | Constant | UpperSnake | Ada => &[Boundary::Underscore],
            Kebab | Cobol | UpperKebab | Train => &[Boundary::Hyphen],
            Upper | Lower | Title | Sentence => &[Boundary::Space],
            Camel | UpperCamel | Pascal => &[
                Boundary::LowerUpper,
                Boundary::Acronym,
                Boundary::LowerDigit,
                Boundary::UpperDigit,
                Boundary::DigitLower,
                Boundary::DigitUpper,
            ],
            UpperFlat | Flat => &[],
            Custom { boundaries, .. } => boundaries,
        }
    }

    /// Returns the delimiter used in the corresponding case.  The following
    /// table outlines which cases use which delimiter.
    ///
    /// | Cases | Delimiter |
    /// | --- | --- |
    /// | Snake, Constant, UpperSnake, Ada | Underscore `"_"` |
    /// | Kebab, Cobol, UpperKebab, Train | Hyphen `"-"` |
    /// | Upper, Lower, Title, Sentence | Space `" "` |
    /// | Flat, UpperFlat, Pascal, UpperCamel, Camel | Empty string `""` |
    pub const fn delimiter(&self) -> &'static str {
        use Case::*;
        match self {
            Snake | Constant | UpperSnake | Ada => "_",
            Kebab | Cobol | UpperKebab | Train => "-",
            Upper | Lower | Title | Sentence => " ",
            Flat | UpperFlat | Pascal | UpperCamel | Camel => "",
            Custom {
                delimiter: delim, ..
            } => delim,
        }
    }

    /// Returns the pattern used in the corresponding case.  The following
    /// table outlines which cases use which pattern.
    ///
    /// | Cases | Pattern |
    /// | --- | --- |
    /// | Constant, UpperSnake, Cobol, UpperKebab, UpperFlat, Upper | [Uppercase](Pattern::Uppercase) |
    /// | Snake, Kebab, Flat, Lower | [Lowercase](Pattern::Lowercase) |
    /// | Ada, Train, Pascal, UpperCamel, Title | [Capital](Pattern::Capital) |
    /// | Camel | [Camel](Pattern::Camel) |
    pub const fn pattern(&self) -> Pattern {
        use Case::*;
        match self {
            Constant | UpperSnake | Cobol | UpperKebab | UpperFlat | Upper => Pattern::Uppercase,
            Snake | Kebab | Flat | Lower => Pattern::Lowercase,
            Ada | Train | Pascal | UpperCamel | Title => Pattern::Capital,
            Camel => Pattern::Camel,
            Sentence => Pattern::Sentence,
            Custom { pattern, .. } => *pattern,
        }
    }

    /// Split an identifier into words based on the boundaries of this case.
    /// ```
    /// use convert_case::Case;
    /// assert_eq!(
    ///     Case::Pascal.split(&"getTotalLength"),
    ///     vec!["get", "Total", "Length"],
    /// );
    /// ```
    pub fn split<T>(self, s: &T) -> Vec<&str>
    where
        T: AsRef<str>,
    {
        boundary::split(s, self.boundaries())
    }

    /// Mutate a list of words based on the pattern of this case.
    /// ```
    /// use convert_case::Case;
    /// assert_eq!(
    ///     Case::Snake.mutate(&["get", "Total", "Length"]),
    ///     vec!["get", "total", "length"],
    /// );
    /// ```
    pub fn mutate(self, words: &[&str]) -> Vec<String> {
        self.pattern().mutate(words)
    }

    /// Join a list of words into a single identifier using the delimiter of this case.
    /// ```
    /// use convert_case::Case;
    /// assert_eq!(
    ///     Case::Snake.join(&[
    ///         String::from("get"),
    ///         String::from("total"),
    ///         String::from("length")
    ///     ]),
    ///     String::from("get_total_length"),
    /// );
    /// ```
    pub fn join(self, words: &[String]) -> String {
        words.join(self.delimiter())
    }

    /// Array of all non-custom case enum variants.  Does not include aliases.
    pub fn all_cases() -> &'static [Case<'static>] {
        use Case::*;
        &[
            Snake, Constant, Ada, Kebab, Cobol, Train, Flat, UpperFlat, Pascal, Camel, Upper,
            Lower, Title, Sentence,
        ]
    }
}