pub enum Pattern {
Noop,
Lowercase,
Uppercase,
Capital,
Camel,
Sentence,
Custom(fn(&[&str]) -> Vec<String>),
}Expand description
Transformations on a list of words.
A pattern is a function that maps a list of words into another list after changing the casing of each letter. How a patterns mutates each letter can be dependent on the word the letters are present in.
§Custom Pattern
A pattern is a function that maps from a borrowed list of words &[&str] to
an owned list of owned words Vec<String> by applying a transformation.
One example of custom behavior might be a pattern
that detects a fixed list of two-letter acronyms, and capitalizes them
appropriately on output.
use convert_case::{Converter, Pattern};
fn pascal_upper_acronyms(words: &[&str]) -> Vec<String> {
Pattern::Capital.mutate(words).into_iter()
.map(|word| match word.as_ref() {
"Io" | "Xml" => word.to_uppercase(),
_ => word,
})
.collect()
}
let acronym_converter = Converter::new()
.set_pattern(Pattern::Custom(pascal_upper_acronyms));
assert_eq!(acronym_converter.convert("io_stream"), "IOStream");
assert_eq!(acronym_converter.convert("xml request"), "XMLRequest");Another example might be a one that explicitly adds leading and trailing double underscores. We do this by modifying the words directly, which will get passed as-is to the join function.
use convert_case::{Converter, Pattern};
fn snake_dunder(mut words: &[&str]) -> Vec<String> {
words
.into_iter()
.map(|word| word.to_lowercase())
.enumerate()
.map(|(i, word)| {
if words.len() == 1 {
format!("__{}__", word)
} else if i == 0 {
format!("__{}", word)
} else if i == words.len() - 1 {
format!("{}__", word)
} else {
word
}
})
.collect()
}
let dunder_converter = Converter::new()
.set_pattern(Pattern::Custom(snake_dunder))
.set_delim("_");
assert_eq!(dunder_converter.convert("getAttr"), "__get_attr__");
assert_eq!(dunder_converter.convert("ITER"), "__iter__");Variants§
Noop
The no-op pattern performs no mutations.
assert_eq!(
Pattern::Noop.mutate(&["Case", "CONVERSION", "library"]),
vec!["Case", "CONVERSION", "library"],
);Lowercase
Makes all words lowercase.
assert_eq!(
Pattern::Lowercase.mutate(&["Case", "CONVERSION", "library"]),
vec!["case", "conversion", "library"],
);Uppercase
Makes all words uppercase.
assert_eq!(
Pattern::Uppercase.mutate(&["Case", "CONVERSION", "library"]),
vec!["CASE", "CONVERSION", "LIBRARY"],
);Capital
Makes the first letter of each word uppercase and the remaining letters of each word lowercase.
assert_eq!(
Pattern::Capital.mutate(&["Case", "CONVERSION", "library"]),
vec!["Case", "Conversion", "Library"],
);Camel
Makes the first non-empty word lowercase and the remaining capitalized.
assert_eq!(
Pattern::Camel.mutate(&["Case", "CONVERSION", "library"]),
vec!["case", "Conversion", "Library"],
);Sentence
Makes the first non-empty word capitalized and the remaining lowercase.
assert_eq!(
Pattern::Sentence.mutate(&["Case", "CONVERSION", "library"]),
vec!["Case", "conversion", "library"],
);Custom(fn(&[&str]) -> Vec<String>)
Define custom behavior to transform a set of words.
See the Pattern documentation for examples.