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
pub mod core;
use crate::core::str_comps::StringCompounds;
use crate::core::spec_chars::SpecChars;

pub struct TypoCase {
    compounds: Vec<String>
}

impl TypoCase {

    pub fn new(_string: String) -> TypoCase {
        return TypoCase { 
            compounds: StringCompounds::new(_string).extract()
        };
    }

    pub fn pascal_case(&self) -> String {
        return self.compounds.iter()
        .map(|s| {
            let (head, tail) = (&s[0..1], &s[1..]);
            return format!("{}{}", head.to_uppercase(), tail)
        })
        .collect::<Vec<String>>().join("");
    }

    pub fn camel_case(&self) -> String {
        let pascal_case = self.pascal_case();
        if pascal_case.len() == 0 {
            return pascal_case;
        }
        let (head, tail) = (&pascal_case[0..1], &pascal_case[1..]);
        
        return format!("{}{}", head.to_lowercase(), tail);
    }

    pub fn snake_case(&self) -> String {
        return self.join_on_spec_char(SpecChars::UnderScore);
    }

    pub fn constant_case(&self) -> String {
        return self.snake_case().to_uppercase();
    }

    pub fn kebab_case(&self) -> String {
        return self.join_on_spec_char(SpecChars::Dash);
    }

    fn join_on_spec_char(&self, spec_char: SpecChars) -> String {
        return self.compounds.iter()
        .map(|s| s.to_string())
        .collect::<Vec<String>>()
        .join(&spec_char.to_string());
    }

}

pub struct Config {
    pub transformation: String,
    pub string: String
}

impl Config {
    pub fn new(mut args: std::env::Args) -> Result<Config, &'static str> {
        args.next();

        let transformation = match args.next() {
            Some(arg) => arg,
            None => return Err("Missing typocase transformation argument."),
        };
        let string = match args.next() {
            Some(arg) => arg,
            None => return Err("Missing string to transform argument."),
        };

        Ok(Config { transformation, string })
    }
}