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
#![deny(warnings)]
use std::ascii::*;

pub struct CamelOptions {
    pub new_word: bool,
    pub last_char: char,
    pub first_word: bool,
    pub injectable_char: char,
    pub has_seperator: bool,
    pub inverted: bool,
}

pub fn to_case_snake_like(convertable_string: String, replace_with: &str, case: &str) -> String {
    let seperators: &[char] = &[' ', '-', '_'];
    if convertable_string.contains(seperators) {
        to_snake_like_from_snake_like(convertable_string, replace_with, case)
    } else {
        to_snake_like_from_camel_or_class(convertable_string, replace_with, case)
    }
}

pub fn to_case_camel_like(convertable_string: String, camel_options: CamelOptions) -> String {
    let mut new_word: bool = camel_options.new_word;
    let mut first_word: bool = camel_options.first_word;
    let mut last_char: char = camel_options.last_char;
    convertable_string.chars()
        .fold("".to_string(),
              |mut result, character| if character == '-' || character == '_' || character == ' ' {
                  new_word = true;
                  result
              } else if character.is_numeric() {
                  new_word = true;
                  result.push(character);
                  result
              } else if new_word ||
                                                ((last_char.is_lowercase() &&
                                                  character.is_uppercase()) &&
                                                 (last_char != ' ')) {
                  new_word = false;
                  if !first_word && camel_options.has_seperator {
                      result.push(camel_options.injectable_char);
                  }
                  if !camel_options.inverted || first_word {
                      result.push(character.to_ascii_uppercase());
                  } else {
                      result.push(character.to_ascii_lowercase());
                  }
                  first_word = false;
                  result
              } else {
                  last_char = character;
                  result.push(character.to_ascii_lowercase());
                  result
              })
}

#[inline]
fn to_snake_like_from_snake_like(convertable_string: String,
                                 replace_with: &str,
                                 case: &str)
                                 -> String {
    let mut new_word: bool = false;
    let mut last_char: char = ' ';
    convertable_string.chars()
        .fold("".to_string(),
              |mut result, character| if character == '-' || character == '_' || character == ' ' {
                  new_word = true;
                  result.push(replace_with.chars().nth(0).unwrap_or('_'));
                  result
              } else if new_word ||
                                                ((last_char.is_lowercase() &&
                                                  character.is_uppercase()) &&
                                                 (last_char != ' ')) {
                  new_word = false;
                  if case == "lower" {
                      result.push(character.to_ascii_lowercase());
                  } else {
                      result.push(character.to_ascii_uppercase());
                  }
                  result
              } else {
                  last_char = character;
                  if case == "lower" {
                      result.push(character.to_ascii_lowercase());
                  } else {
                      result.push(character.to_ascii_uppercase());
                  }
                  result
              })
}

#[inline]
fn to_snake_like_from_camel_or_class(convertable_string: String,
                                     replace_with: &str,
                                     case: &str)
                                     -> String {
    let mut first_character: bool = true;
    convertable_string.chars()
        .enumerate()
        .fold("".to_string(),
              |mut acc, char_with_index| if char_with_index.1 ==
                                            char_with_index.1.to_ascii_uppercase() &&
                                            !first_character &&
                                            (convertable_string.chars()
                  .nth(char_with_index.0 + 1)
                  .unwrap_or('A')
                  .is_lowercase() ||
                                             convertable_string.chars()
                  .nth(char_with_index.0 - 1)
                  .unwrap_or('A')
                  .is_lowercase()) {
                  if case == "lower" {
                      acc.push(replace_with.chars().nth(0).unwrap_or('_'));
                      acc.push(char_with_index.1.to_ascii_lowercase());
                      acc
                  } else {
                      acc.push(replace_with.chars().nth(0).unwrap_or('_'));
                      acc.push(char_with_index.1.to_ascii_uppercase());
                      acc
                  }
              } else {
                  first_character = false;
                  if case == "lower" {
                      acc.push(char_with_index.1.to_ascii_lowercase());
                      acc
                  } else {
                      acc.push(char_with_index.1.to_ascii_uppercase());
                      acc
                  }
              })
}

#[macro_export]
macro_rules! define_test_group {
    ($module_name: ident,
     $method: ident,
     $use_mod: ident,
     $expected: expr,
     $expected_plural: expr) => {
        #[cfg(test)]
        mod $module_name {
            use ::cases::$use_mod::$method;
            define_tests![
                $method;
                from_camel_case             => "fooBar"     => $expected,
                from_class_case             => "FooBar"     => $expected,
                from_screaming_snake_case   => "FOO_BAR"    => $expected,
                from_kebab_case             => "foo-bar"    => $expected,
                from_pascal_case            => "FooBar"     => $expected,
                from_sentence_case          => "Foo bar"    => $expected,
                from_snake_case             => "foo_bar"    => $expected,
                from_title_case             => "Foo Bar"    => $expected,
                from_table_case             => "foo_bars"   => $expected_plural,
                from_train_case             => "Foo-Bars"   => $expected_plural
            ];
        }
    }
}

macro_rules! define_tests{
    ($method: ident; $($test_name:ident => $to_convert:expr => $expected:expr ), *) => {
        $(
            #[test]
            fn $test_name() {
                assert_eq!($method($to_convert.to_string()), $expected.to_string())
            }
        )*
    }
}