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
mod tool;

use regex::Error;

pub mod conventions;

pub use conventions::*;

pub enum CaseName {
    NoCase,
    SnakeCase,
    CamelCase,
    PascalCase,
    MacroCase,
    KebabCase,
    TrainCase,
    FlatCase,
}

pub trait Convention {
    fn to(&self, string: &str) -> Result<String, Error>;
    fn is(&self, string: &str) -> Result<bool, Error>;
}

/// The function `get_convention` returns a boxed trait object based on the given `CaseName` enum
/// variant.
///
/// Arguments:
///
/// * `name`: The `name` parameter is of type `CaseName`. It is an enum that represents different naming
/// conventions. The possible values for `CaseName` are:
///
/// Returns:
///
/// The function `get_convention` returns a boxed trait object that implements the `Convention` trait.
/// The specific implementation returned depends on the `CaseName` enum variant passed as an argument.
pub fn get_convention(name: CaseName) -> Box<dyn Convention> {
    match name {
        CaseName::NoCase => Box::new(NoCase),
        CaseName::SnakeCase => Box::new(SnakeCase),
        CaseName::CamelCase => Box::new(CamelCase),
        CaseName::PascalCase => Box::new(PascalCase),
        CaseName::MacroCase => Box::new(MacroCase),
        CaseName::KebabCase => Box::new(KebabCase),
        CaseName::FlatCase => Box::new(FlatCase),
        CaseName::TrainCase => Box::new(TrainCase),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn init() {
        dotenv::dotenv().ok();
        let _ = env_logger::try_init();
    }

    #[test]
    fn test_get_convention() {
        tests::init();

        let no_case = get_convention(CaseName::NoCase);

        assert_eq!(no_case.to("hello, world!").unwrap(), "hello, world!")
    }
}