1mod util;
2
3use crate::util::{to_camel_case, to_lower_case, to_snake_case, to_title_case, to_upper_case};
4
5pub struct TransCase {
6 words: Vec<String>,
7}
8
9pub enum Case {
12 Upper,
14 Lower,
16 Title,
18 Snake,
20 Camel,
22}
23
24impl TransCase {
42 pub fn new(value: &str) -> Self {
43 let words = value
44 .replace("-", " ")
45 .replace("_", " ")
46 .split(" ")
47 .map(|word| word.to_string())
48 .collect::<Vec<String>>();
49
50 Self { words }
51 }
52
53 pub fn case(&self, case: Case) -> String {
54 use Case::*;
55
56 match case {
57 Upper => to_upper_case(&self.words.join(" ")),
58 Lower => to_lower_case(&self.words.join(" ")),
59 Title => to_title_case(&self.words.join(" ")),
60 Snake => to_snake_case(&self.words.join(" ")),
61 Camel => to_camel_case(&self.words.join(" ")),
62 }
63 }
64}
65
66#[cfg(test)]
67mod tests {
68 use super::*;
69
70 #[test]
71 fn test_trans_case() {
72 let sentence = TransCase::new("Tanveer_evan mashup");
73
74 assert_eq!(
75 sentence.case(Case::Upper),
76 String::from("TANVEER EVAN MASHUP")
77 );
78 assert_eq!(
79 sentence.case(Case::Lower),
80 String::from("tanveer evan mashup")
81 );
82 assert_eq!(
83 sentence.case(Case::Title),
84 String::from("Tanveer Evan Mashup")
85 );
86 assert_eq!(
87 sentence.case(Case::Snake),
88 String::from("tanveer_evan_mashup")
89 );
90 assert_eq!(
91 sentence.case(Case::Camel),
92 String::from("tanveerEvanMashup")
93 );
94 }
95}