diff --git a/Cargo.toml b/Cargo.toml index 94581571..ad7fa189 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,5 +30,5 @@ heavyweight = ['regex', 'lazy_static'] name = "inflector" [dependencies] -regex = {version = "1.1", optional = true} -lazy_static = {version = "1.2.0", optional = true} +regex = { version = "1.5.4", optional = true } +lazy_static = { version = "1.4.0", optional = true } diff --git a/src/cases/camelcase/mod.rs b/src/cases/camelcase/mod.rs index b09ddc65..ba11464b 100644 --- a/src/cases/camelcase/mod.rs +++ b/src/cases/camelcase/mod.rs @@ -1,5 +1,5 @@ #![deny(warnings)] -use cases::case::*; +use crate::cases::case::*; /// Converts a `&str` to camelCase `String` /// @@ -84,7 +84,7 @@ pub fn to_camel_case(non_camelized_string: &str) -> String { has_seperator: false, inverted: false, }; - to_case_camel_like(&non_camelized_string, options) + to_case_camel_like(non_camelized_string, options) } /// Determines if a `&str` is camelCase bool`` @@ -168,7 +168,7 @@ pub fn to_camel_case(non_camelized_string: &str) -> String { /// assert!(asserted_bool == false); /// ``` pub fn is_camel_case(test_string: &str) -> bool { - to_camel_case(&test_string.clone()) == test_string + to_camel_case(test_string) == test_string } #[cfg(all(feature = "unstable", test))] @@ -211,8 +211,8 @@ mod benchmarks { #[cfg(test)] mod tests { - use ::to_camel_case; - use ::is_camel_case; + use crate::is_camel_case; + use crate::to_camel_case; #[test] fn from_camel_case() { @@ -307,7 +307,8 @@ mod tests { #[test] fn wrapped_in_bad_chars() { - let convertable_string: String = "-!#$%wrapped in bad chars&*^*&(&*^&(<>><>))".to_owned(); + let convertable_string: String = + "-!#$%wrapped in bad chars&*^*&(&*^&(<>><>))".to_owned(); let expected: String = "wrappedInBadChars".to_owned(); assert_eq!(to_camel_case(&convertable_string), expected) } @@ -367,4 +368,3 @@ mod tests { assert_eq!(is_camel_case(&convertable_string), false) } } - diff --git a/src/cases/case/mod.rs b/src/cases/case/mod.rs index bb4c3d2e..a1425ff5 100644 --- a/src/cases/case/mod.rs +++ b/src/cases/case/mod.rs @@ -1,6 +1,6 @@ #![deny(warnings)] -#[allow(unknown_lints)] -#[allow(unused_imports)] +#![allow(unknown_lints)] +#![allow(unused_imports)] use std::ascii::*; pub struct CamelOptions { @@ -19,9 +19,9 @@ pub fn to_case_snake_like(convertable_string: &str, replace_with: &str, case: &s if char_is_seperator(&char_with_index.1) { if !first_character { first_character = true; - result.push(replace_with.chars().nth(0).unwrap_or('_')); + result.push(replace_with.chars().next().unwrap_or('_')); } - } else if requires_seperator(char_with_index, first_character, &convertable_string) { + } else if requires_seperator(char_with_index, first_character, convertable_string) { first_character = false; result = snake_like_with_seperator(result, replace_with, &char_with_index.1, case) } else { @@ -62,7 +62,12 @@ pub fn to_case_camel_like(convertable_string: &str, camel_options: CamelOptions) } #[inline] -fn append_on_new_word(mut result: String, first_word: bool, character: char, camel_options: &CamelOptions) -> String { +fn append_on_new_word( + mut result: String, + first_word: bool, + character: char, + camel_options: &CamelOptions, +) -> String { if not_first_word_and_has_seperator(first_word, camel_options.has_seperator) { result.push(camel_options.injectable_char); } @@ -82,11 +87,12 @@ fn first_word_or_not_inverted(first_word: bool, inverted: bool) -> bool { !inverted || first_word } - -fn last_char_lower_current_is_upper_or_new_word(new_word: bool, last_char: char, character: char) -> bool{ - new_word || - ((last_char.is_lowercase() && character.is_uppercase()) && - (last_char != ' ')) +fn last_char_lower_current_is_upper_or_new_word( + new_word: bool, + last_char: char, + character: char, +) -> bool { + new_word || ((last_char.is_lowercase() && character.is_uppercase()) && (last_char != ' ')) } fn char_is_seperator(character: &char) -> bool { @@ -102,10 +108,14 @@ fn is_not_alphanumeric(character: char) -> bool { } #[inline] -fn requires_seperator(char_with_index: (usize, char), first_character: bool, convertable_string: &str) -> bool { - !first_character && - char_is_uppercase(char_with_index.1) && - next_or_previous_char_is_lowercase(convertable_string, char_with_index.0) +fn requires_seperator( + char_with_index: (usize, char), + first_character: bool, + convertable_string: &str, +) -> bool { + !first_character + && char_is_uppercase(char_with_index.1) + && next_or_previous_char_is_lowercase(convertable_string, char_with_index.0) } #[inline] @@ -120,21 +130,34 @@ fn snake_like_no_seperator(mut accumlator: String, current_char: &char, case: &s } #[inline] -fn snake_like_with_seperator(mut accumlator: String, replace_with: &str, current_char: &char, case: &str) -> String { +fn snake_like_with_seperator( + mut accumlator: String, + replace_with: &str, + current_char: &char, + case: &str, +) -> String { if case == "lower" { - accumlator.push(replace_with.chars().nth(0).unwrap_or('_')); + accumlator.push(replace_with.chars().next().unwrap_or('_')); accumlator.push(current_char.to_ascii_lowercase()); accumlator } else { - accumlator.push(replace_with.chars().nth(0).unwrap_or('_')); + accumlator.push(replace_with.chars().next().unwrap_or('_')); accumlator.push(current_char.to_ascii_uppercase()); accumlator } } fn next_or_previous_char_is_lowercase(convertable_string: &str, char_with_index: usize) -> bool { - convertable_string.chars().nth(char_with_index + 1).unwrap_or('A').is_lowercase() || - convertable_string.chars().nth(char_with_index - 1).unwrap_or('A').is_lowercase() + convertable_string + .chars() + .nth(char_with_index + 1) + .unwrap_or('A') + .is_lowercase() + || convertable_string + .chars() + .nth(char_with_index - 1) + .unwrap_or('A') + .is_lowercase() } fn char_is_uppercase(test_char: char) -> bool { @@ -161,7 +184,6 @@ fn test_is_not_alphanumeric_on_is_not_alphanumeric() { assert!(is_not_alphanumeric('_')) } - #[test] fn test_char_is_uppercase_when_it_is() { assert_eq!(char_is_uppercase('A'), true) @@ -184,22 +206,34 @@ fn test_next_or_previous_char_is_lowercase_false() { #[test] fn snake_like_with_seperator_lowers() { - assert_eq!(snake_like_with_seperator("".to_owned(), "^", &'c', "lower"), "^c".to_string()) + assert_eq!( + snake_like_with_seperator("".to_owned(), "^", &'c', "lower"), + "^c".to_string() + ) } #[test] fn snake_like_with_seperator_upper() { - assert_eq!(snake_like_with_seperator("".to_owned(), "^", &'c', "upper"), "^C".to_string()) + assert_eq!( + snake_like_with_seperator("".to_owned(), "^", &'c', "upper"), + "^C".to_string() + ) } #[test] fn snake_like_no_seperator_lower() { - assert_eq!(snake_like_no_seperator("".to_owned(), &'C', "lower"), "c".to_string()) + assert_eq!( + snake_like_no_seperator("".to_owned(), &'C', "lower"), + "c".to_string() + ) } #[test] fn snake_like_no_seperator_upper() { - assert_eq!(snake_like_no_seperator("".to_owned(), &'c', "upper"), "C".to_string()) + assert_eq!( + snake_like_no_seperator("".to_owned(), &'c', "upper"), + "C".to_string() + ) } #[test] @@ -249,27 +283,42 @@ fn test_char_is_seperator_when_not() { #[test] fn test_last_char_lower_current_is_upper_or_new_word_with_new_word() { - assert_eq!(last_char_lower_current_is_upper_or_new_word(true, ' ', '-'), true) + assert_eq!( + last_char_lower_current_is_upper_or_new_word(true, ' ', '-'), + true + ) } #[test] fn test_last_char_lower_current_is_upper_or_new_word_last_char_space() { - assert_eq!(last_char_lower_current_is_upper_or_new_word(false, ' ', '-'), false) + assert_eq!( + last_char_lower_current_is_upper_or_new_word(false, ' ', '-'), + false + ) } #[test] fn test_last_char_lower_current_is_upper_or_new_word_last_char_lower_current_upper() { - assert_eq!(last_char_lower_current_is_upper_or_new_word(false, 'a', 'A'), true) + assert_eq!( + last_char_lower_current_is_upper_or_new_word(false, 'a', 'A'), + true + ) } #[test] fn test_last_char_lower_current_is_upper_or_new_word_last_char_upper_current_upper() { - assert_eq!(last_char_lower_current_is_upper_or_new_word(false, 'A', 'A'), false) + assert_eq!( + last_char_lower_current_is_upper_or_new_word(false, 'A', 'A'), + false + ) } #[test] fn test_last_char_lower_current_is_upper_or_new_word_last_char_upper_current_lower() { - assert_eq!(last_char_lower_current_is_upper_or_new_word(false, 'A', 'a'), false) + assert_eq!( + last_char_lower_current_is_upper_or_new_word(false, 'A', 'a'), + false + ) } #[test] diff --git a/src/cases/classcase/mod.rs b/src/cases/classcase/mod.rs index e0b6ef6f..552e14a0 100644 --- a/src/cases/classcase/mod.rs +++ b/src/cases/classcase/mod.rs @@ -1,7 +1,7 @@ #![deny(warnings)] -use cases::case::*; +use crate::cases::case::*; #[cfg(feature = "heavyweight")] -use string::singularize::to_singular; +use crate::string::singularize::to_singular; #[cfg(feature = "heavyweight")] /// Converts a `&str` to `ClassCase` `String` /// @@ -193,7 +193,7 @@ pub fn to_class_case(non_class_case_string: &str) -> String { /// /// ``` pub fn is_class_case(test_string: &str) -> bool { - to_class_case(&test_string.clone()) == test_string + to_class_case(test_string) == test_string } #[cfg(all(feature = "unstable", test))] @@ -221,8 +221,8 @@ mod benchmarks { #[cfg(test)] #[cfg(feature = "heavyweight")] mod tests { - use ::to_class_case; - use ::is_class_case; + use crate::is_class_case; + use crate::to_class_case; #[test] fn from_camel_case() { @@ -324,7 +324,8 @@ mod tests { #[test] fn wrapped_in_bad_chars() { - let convertable_string: String = "-!#$%wrapped in bad chars&*^*&(&*^&(<>><>))".to_owned(); + let convertable_string: String = + "-!#$%wrapped in bad chars&*^*&(&*^&(<>><>))".to_owned(); let expected: String = "WrappedInBadChar".to_owned(); assert_eq!(to_class_case(&convertable_string), expected) } @@ -390,4 +391,3 @@ mod tests { assert_eq!(is_class_case(&convertable_string), true) } } - diff --git a/src/cases/kebabcase/mod.rs b/src/cases/kebabcase/mod.rs index d79f2736..8cb20d6b 100644 --- a/src/cases/kebabcase/mod.rs +++ b/src/cases/kebabcase/mod.rs @@ -1,5 +1,5 @@ #![deny(warnings)] -use cases::case::*; +use crate::cases::case::*; /// Determines if a `&str` is `kebab-case` /// /// ``` @@ -58,7 +58,7 @@ use cases::case::*; /// /// ``` pub fn is_kebab_case(test_string: &str) -> bool { - test_string == to_kebab_case(test_string.clone()) + test_string == to_kebab_case(test_string) } /// Converts a `&str` to `kebab-case` `String` @@ -152,8 +152,8 @@ mod benchmarks { #[cfg(test)] mod tests { - use ::to_kebab_case; - use ::is_kebab_case; + use crate::is_kebab_case; + use crate::to_kebab_case; #[test] fn from_camel_case() { @@ -259,4 +259,3 @@ mod tests { assert_eq!(is_kebab_case(&convertable_string), false) } } - diff --git a/src/cases/pascalcase/mod.rs b/src/cases/pascalcase/mod.rs index 248f91ab..1562cfd9 100644 --- a/src/cases/pascalcase/mod.rs +++ b/src/cases/pascalcase/mod.rs @@ -1,5 +1,5 @@ #![deny(warnings)] -use cases::case::*; +use crate::cases::case::*; /// Converts a `&str` to pascalCase `String` /// /// ``` @@ -159,7 +159,7 @@ pub fn to_pascal_case(non_pascalized_string: &str) -> String { /// assert!(asserted_bool == false); /// ``` pub fn is_pascal_case(test_string: &str) -> bool { - to_pascal_case(test_string.clone()) == test_string + to_pascal_case(test_string) == test_string } #[cfg(all(feature = "unstable", test))] @@ -202,8 +202,8 @@ mod benchmarks { #[cfg(test)] mod tests { - use ::to_pascal_case; - use ::is_pascal_case; + use crate::is_pascal_case; + use crate::to_pascal_case; #[test] fn from_camel_case() { @@ -298,7 +298,8 @@ mod tests { #[test] fn wrapped_in_bad_chars() { - let convertable_string: String = "-!#$%wrapped in bad chars&*^*&(&*^&(<>><>))".to_owned(); + let convertable_string: String = + "-!#$%wrapped in bad chars&*^*&(&*^&(<>><>))".to_owned(); let expected: String = "WrappedInBadChars".to_owned(); assert_eq!(to_pascal_case(&convertable_string), expected) } diff --git a/src/cases/screamingsnakecase/mod.rs b/src/cases/screamingsnakecase/mod.rs index 9bec7a55..a3773c48 100644 --- a/src/cases/screamingsnakecase/mod.rs +++ b/src/cases/screamingsnakecase/mod.rs @@ -1,5 +1,5 @@ #![deny(warnings)] -use cases::case::*; +use crate::cases::case::*; /// Converts a `&str` to `SCREAMING_SNAKE_CASE` `String` /// /// ``` @@ -121,10 +121,9 @@ pub fn to_screaming_snake_case(non_snake_case_string: &str) -> String { /// /// ``` pub fn is_screaming_snake_case(test_string: &str) -> bool { - test_string == to_screaming_snake_case(test_string.clone()) + test_string == to_screaming_snake_case(test_string) } - #[cfg(all(feature = "unstable", test))] mod benchmarks { extern crate test; @@ -139,13 +138,12 @@ mod benchmarks { fn bench_is_screaming_snake(b: &mut Bencher) { b.iter(|| super::is_screaming_snake_case("Foo bar")); } - } #[cfg(test)] mod tests { - use ::to_screaming_snake_case; - use ::is_screaming_snake_case; + use crate::is_screaming_snake_case; + use crate::to_screaming_snake_case; #[test] fn from_camel_case() { diff --git a/src/cases/sentencecase/mod.rs b/src/cases/sentencecase/mod.rs index 5a99d217..8e827f50 100644 --- a/src/cases/sentencecase/mod.rs +++ b/src/cases/sentencecase/mod.rs @@ -1,5 +1,5 @@ #![deny(warnings)] -use cases::case::*; +use crate::cases::case::*; /// Converts a `&str` to `Sentence case` `String` /// /// ``` @@ -127,7 +127,7 @@ pub fn to_sentence_case(non_sentence_case_string: &str) -> String { /// /// ``` pub fn is_sentence_case(test_string: &str) -> bool { - test_string == to_sentence_case(test_string.clone()) + test_string == to_sentence_case(test_string) } #[cfg(all(feature = "unstable", test))] @@ -149,13 +149,12 @@ mod benchmarks { fn bench_sentence_from_snake(b: &mut Bencher) { b.iter(|| super::to_sentence_case("foo_bar")); } - } #[cfg(test)] mod tests { - use ::to_sentence_case; - use ::is_sentence_case; + use crate::is_sentence_case; + use crate::to_sentence_case; #[test] fn from_camel_case() { @@ -250,7 +249,8 @@ mod tests { #[test] fn wrapped_in_bad_chars() { - let convertable_string: String = "-!#$%wrapped in bad chars&*^*&(&*^&(<>><>))".to_owned(); + let convertable_string: String = + "-!#$%wrapped in bad chars&*^*&(&*^&(<>><>))".to_owned(); let expected: String = "Wrapped in bad chars".to_owned(); assert_eq!(to_sentence_case(&convertable_string), expected) } @@ -262,7 +262,6 @@ mod tests { assert_eq!(to_sentence_case(&convertable_string), expected) } - #[test] fn is_correct_from_camel_case() { let convertable_string: String = "fooBar".to_owned(); diff --git a/src/cases/snakecase/mod.rs b/src/cases/snakecase/mod.rs index a56894b1..0d878aad 100644 --- a/src/cases/snakecase/mod.rs +++ b/src/cases/snakecase/mod.rs @@ -1,5 +1,5 @@ #![deny(warnings)] -use cases::case::*; +use crate::cases::case::*; /// Converts a `&str` to `snake_case` `String` /// /// ``` @@ -144,7 +144,7 @@ pub fn to_snake_case(non_snake_case_string: &str) -> String { /// /// ``` pub fn is_snake_case(test_string: &str) -> bool { - test_string == to_snake_case(test_string.clone()) + test_string == to_snake_case(test_string) } #[cfg(all(feature = "unstable", test))] @@ -171,13 +171,12 @@ mod benchmarks { fn bench_is_snake(b: &mut Bencher) { b.iter(|| super::is_snake_case("Foo bar")); } - } #[cfg(test)] mod tests { - use ::to_snake_case; - use ::is_snake_case; + use crate::is_snake_case; + use crate::to_snake_case; #[test] fn from_camel_case() { @@ -272,7 +271,8 @@ mod tests { #[test] fn wrapped_in_bad_chars() { - let convertable_string: String = "-!#$%wrapped in bad chars&*^*&(&*^&(<>><>))".to_owned(); + let convertable_string: String = + "-!#$%wrapped in bad chars&*^*&(&*^&(<>><>))".to_owned(); let expected: String = "wrapped_in_bad_chars".to_owned(); assert_eq!(to_snake_case(&convertable_string), expected) } diff --git a/src/cases/tablecase/mod.rs b/src/cases/tablecase/mod.rs index 7224cc43..27b4ee84 100644 --- a/src/cases/tablecase/mod.rs +++ b/src/cases/tablecase/mod.rs @@ -1,8 +1,8 @@ #![deny(warnings)] #[cfg(feature = "heavyweight")] -use string::pluralize::to_plural; +use crate::cases::case::*; #[cfg(feature = "heavyweight")] -use cases::case::*; +use crate::string::pluralize::to_plural; #[cfg(feature = "heavyweight")] /// Converts a `&str` to `table-case` `String` /// @@ -126,7 +126,7 @@ pub fn to_table_case(non_table_case_string: &str) -> String { /// assert!(asserted_bool == false); /// ``` pub fn is_table_case(test_string: &str) -> bool { - to_table_case(&test_string.clone()) == test_string + to_table_case(test_string) == test_string } #[cfg(all(feature = "unstable", test))] @@ -149,8 +149,8 @@ mod benchmarks { #[cfg(test)] #[cfg(feature = "heavyweight")] mod tests { - use ::to_table_case; - use ::is_table_case; + use crate::is_table_case; + use crate::to_table_case; #[test] fn from_camel_case() { diff --git a/src/cases/titlecase/mod.rs b/src/cases/titlecase/mod.rs index eb0de257..c1587abf 100644 --- a/src/cases/titlecase/mod.rs +++ b/src/cases/titlecase/mod.rs @@ -1,5 +1,5 @@ #![deny(warnings)] -use cases::case::*; +use crate::cases::case::*; /// Converts a `&str` to `Title Case` `String` /// /// ``` @@ -121,7 +121,7 @@ pub fn to_title_case(non_title_case_string: &str) -> String { /// /// ``` pub fn is_title_case(test_string: &str) -> bool { - test_string == to_title_case(test_string.clone()) + test_string == to_title_case(test_string) } #[cfg(all(feature = "unstable", test))] @@ -143,14 +143,12 @@ mod benchmarks { fn bench_title_from_snake(b: &mut Bencher) { b.iter(|| super::to_title_case("foo_bar")); } - } - #[cfg(test)] mod tests { - use ::to_title_case; - use ::is_title_case; + use crate::is_title_case; + use crate::to_title_case; #[test] fn from_camel_case() { @@ -245,7 +243,8 @@ mod tests { #[test] fn wrapped_in_bad_chars() { - let convertable_string: String = "-!#$%wrapped in bad chars&*^*&(&*^&(<>><>))".to_owned(); + let convertable_string: String = + "-!#$%wrapped in bad chars&*^*&(&*^&(<>><>))".to_owned(); let expected: String = "Wrapped In Bad Chars".to_owned(); assert_eq!(to_title_case(&convertable_string), expected) } @@ -305,4 +304,3 @@ mod tests { assert_eq!(is_title_case(&convertable_string), false) } } - diff --git a/src/cases/traincase/mod.rs b/src/cases/traincase/mod.rs index cb2e8828..b86ef935 100644 --- a/src/cases/traincase/mod.rs +++ b/src/cases/traincase/mod.rs @@ -1,5 +1,5 @@ #![deny(warnings)] -use cases::case::*; +use crate::cases::case::*; /// Determines if a `&str` is `Train-Case` /// /// ``` @@ -58,10 +58,9 @@ use cases::case::*; /// /// ``` pub fn is_train_case(test_string: &str) -> bool { - test_string == to_train_case(test_string.clone()) + test_string == to_train_case(test_string) } - /// Converts a `&str` to `Train-Case` `String` /// /// ``` @@ -157,13 +156,12 @@ mod benchmarks { fn bench_train_from_snake(b: &mut Bencher) { b.iter(|| super::to_train_case("test_test_test")); } - } #[cfg(test)] mod tests { - use ::to_train_case; - use ::is_train_case; + use crate::is_train_case; + use crate::to_train_case; #[test] fn from_camel_case() { @@ -258,7 +256,8 @@ mod tests { #[test] fn wrapped_in_bad_chars() { - let convertable_string: String = "-!#$%wrapped in bad chars&*^*&(&*^&(<>><>))".to_owned(); + let convertable_string: String = + "-!#$%wrapped in bad chars&*^*&(&*^&(<>><>))".to_owned(); let expected: String = "Wrapped-In-Bad-Chars".to_owned(); assert_eq!(to_train_case(&convertable_string), expected) } diff --git a/src/lib.rs b/src/lib.rs index f52ea29f..58aaf5a5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,10 @@ -#![deny(warnings, unused_variables, missing_docs, unsafe_code, unused_extern_crates)] +#![deny( + warnings, + unused_variables, + missing_docs, + unsafe_code, + unused_extern_crates +)] #![cfg_attr(feature = "unstable", feature(test))] //! Adds String based inflections for Rust. Snake, kebab, train, camel, @@ -16,7 +22,8 @@ extern crate regex; #[cfg(feature = "heavyweight")] -#[macro_use] extern crate lazy_static; +#[macro_use] +extern crate lazy_static; /// Provides case inflections /// - Camel case @@ -33,9 +40,6 @@ pub mod cases; /// - Ordinalize /// - Deordinalize pub mod numbers; -/// Provides suffix inflections -/// - Foreign key -pub mod suffix; /// Provides string inflections /// - Deconstantize /// - Demodulize @@ -43,61 +47,62 @@ pub mod suffix; /// - Singularize #[cfg(feature = "heavyweight")] pub mod string; - +/// Provides suffix inflections +/// - Foreign key +pub mod suffix; #[cfg(feature = "heavyweight")] -use cases::classcase::to_class_case; +use crate::cases::classcase::is_class_case; #[cfg(feature = "heavyweight")] -use cases::classcase::is_class_case; +use crate::cases::classcase::to_class_case; -use cases::camelcase::to_camel_case; -use cases::camelcase::is_camel_case; +use crate::cases::camelcase::is_camel_case; +use crate::cases::camelcase::to_camel_case; -use cases::pascalcase::to_pascal_case; -use cases::pascalcase::is_pascal_case; +use crate::cases::pascalcase::is_pascal_case; +use crate::cases::pascalcase::to_pascal_case; -use cases::snakecase::to_snake_case; -use cases::snakecase::is_snake_case; +use crate::cases::snakecase::is_snake_case; +use crate::cases::snakecase::to_snake_case; -use cases::screamingsnakecase::to_screaming_snake_case; -use cases::screamingsnakecase::is_screaming_snake_case; +use crate::cases::screamingsnakecase::is_screaming_snake_case; +use crate::cases::screamingsnakecase::to_screaming_snake_case; -use cases::kebabcase::to_kebab_case; -use cases::kebabcase::is_kebab_case; +use crate::cases::kebabcase::is_kebab_case; +use crate::cases::kebabcase::to_kebab_case; -use cases::traincase::to_train_case; -use cases::traincase::is_train_case; +use crate::cases::traincase::is_train_case; +use crate::cases::traincase::to_train_case; -use cases::sentencecase::to_sentence_case; -use cases::sentencecase::is_sentence_case; +use crate::cases::sentencecase::is_sentence_case; +use crate::cases::sentencecase::to_sentence_case; -use cases::titlecase::to_title_case; -use cases::titlecase::is_title_case; +use crate::cases::titlecase::is_title_case; +use crate::cases::titlecase::to_title_case; #[cfg(feature = "heavyweight")] -use cases::tablecase::to_table_case; +use crate::cases::tablecase::is_table_case; #[cfg(feature = "heavyweight")] -use cases::tablecase::is_table_case; +use crate::cases::tablecase::to_table_case; -use numbers::ordinalize::ordinalize; -use numbers::deordinalize::deordinalize; +use crate::numbers::deordinalize::deordinalize; +use crate::numbers::ordinalize::ordinalize; -use suffix::foreignkey::to_foreign_key; -use suffix::foreignkey::is_foreign_key; +use crate::suffix::foreignkey::is_foreign_key; +use crate::suffix::foreignkey::to_foreign_key; #[cfg(feature = "heavyweight")] -use string::demodulize::demodulize; +use crate::string::deconstantize::deconstantize; #[cfg(feature = "heavyweight")] -use string::deconstantize::deconstantize; +use crate::string::demodulize::demodulize; #[cfg(feature = "heavyweight")] -use string::pluralize::to_plural; +use crate::string::pluralize::to_plural; #[cfg(feature = "heavyweight")] -use string::singularize::to_singular; +use crate::string::singularize::to_singular; #[allow(missing_docs)] pub trait Inflector { - fn to_camel_case(&self) -> String; fn is_camel_case(&self) -> bool; @@ -148,13 +153,11 @@ pub trait Inflector { fn to_singular(&self) -> String; } - #[allow(missing_docs)] pub trait InflectorNumbers { fn ordinalize(&self) -> String; } - macro_rules! define_implementations { ( $slf:ident; $($imp_trait:ident => $typ:ident), *) => { $( @@ -256,7 +259,7 @@ implement_number_for![ mod benchmarks { extern crate test; use self::test::Bencher; - use ::Inflector; + use Inflector; macro_rules! benchmarks { ( $($test_name:ident => $imp_trait:ident => $to_cast:expr), *) => { diff --git a/src/numbers/deordinalize/mod.rs b/src/numbers/deordinalize/mod.rs index f64cce11..6400511a 100644 --- a/src/numbers/deordinalize/mod.rs +++ b/src/numbers/deordinalize/mod.rs @@ -108,7 +108,8 @@ pub fn deordinalize(non_ordinalized_string: &str) -> String { if non_ordinalized_string.contains('.') { non_ordinalized_string.to_owned() } else { - non_ordinalized_string.trim_end_matches("st") + non_ordinalized_string + .trim_end_matches("st") .trim_end_matches("nd") .trim_end_matches("rd") .trim_end_matches("th") diff --git a/src/numbers/mod.rs b/src/numbers/mod.rs index f28704f5..fc3645a2 100644 --- a/src/numbers/mod.rs +++ b/src/numbers/mod.rs @@ -1,9 +1,9 @@ #![deny(warnings)] -/// Provides ordinalization of a string. -/// -/// Example string "1" becomes "1st" -pub mod ordinalize; /// Provides deordinalization of a string. /// /// Example string "1st" becomes "1" pub mod deordinalize; +/// Provides ordinalization of a string. +/// +/// Example string "1" becomes "1st" +pub mod ordinalize; diff --git a/src/numbers/ordinalize/mod.rs b/src/numbers/ordinalize/mod.rs index 82b0d2d8..f1b011de 100644 --- a/src/numbers/ordinalize/mod.rs +++ b/src/numbers/ordinalize/mod.rs @@ -113,7 +113,7 @@ /// /// ``` pub fn ordinalize(non_ordinalized_string: &str) -> String { - let chars: Vec = non_ordinalized_string.clone().chars().collect(); + let chars: Vec = non_ordinalized_string.chars().collect(); let last_number: char = chars[chars.len() - 1]; if is_ordinalizable(last_number) { return non_ordinalized_string.to_owned(); diff --git a/src/string/constants/mod.rs b/src/string/constants/mod.rs index 0ba4e429..02122a48 100644 --- a/src/string/constants/mod.rs +++ b/src/string/constants/mod.rs @@ -1,225 +1,204 @@ -pub const UNACCONTABLE_WORDS: [&'static str; 202] = ["accommodation", - "adulthood", - "advertising", - "advice", - "aggression", - "aid", - "air", - "aircraft", - "alcohol", - "anger", - "applause", - "arithmetic", - "assistance", - "athletics", - - "bacon", - "baggage", - "beef", - "biology", - "blood", - "botany", - "bread", - "butter", - - "carbon", - "cardboard", - "cash", - "chalk", - "chaos", - "chess", - "crossroads", - "countryside", - - "dancing", - "deer", - "dignity", - "dirt", - "dust", - - "economics", - "education", - "electricity", - "engineering", - "enjoyment", - "envy", - "equipment", - "ethics", - "evidence", - "evolution", - - "fame", - "fiction", - "flour", - "flu", - "food", - "fuel", - "fun", - "furniture", - - "gallows", - "garbage", - "garlic", - "genetics", - "gold", - "golf", - "gossip", - "grammar", - "gratitude", - "grief", - "guilt", - "gymnastics", - - "happiness", - "hardware", - "harm", - "hate", - "hatred", - "health", - "heat", - "help", - "homework", - "honesty", - "honey", - "hospitality", - "housework", - "humour", - "hunger", - "hydrogen", - - "ice", - "importance", - "inflation", - "information", - "innocence", - "iron", - "irony", - - "jam", - "jewelry", - "judo", - - "karate", - "knowledge", - - "lack", - "laughter", - "lava", - "leather", - "leisure", - "lightning", - "linguine", - "linguini", - "linguistics", - "literature", - "litter", - "livestock", - "logic", - "loneliness", - "luck", - "luggage", - - "macaroni", - "machinery", - "magic", - "management", - "mankind", - "marble", - "mathematics", - "mayonnaise", - "measles", - "methane", - "milk", - "money", - "mud", - "music", - "mumps", - - "nature", - "news", - "nitrogen", - "nonsense", - "nurture", - "nutrition", - - "obedience", - "obesity", - "oxygen", - - "pasta", - "patience", - "physics", - "poetry", - "pollution", - "poverty", - "pride", - "psychology", - "publicity", - "punctuation", - - "quartz", - - "racism", - "relaxation", - "reliability", - "research", - "respect", - "revenge", - "rice", - "rubbish", - "rum", - - "safety", - "scenery", - "seafood", - "seaside", - "series", - "shame", - "sheep", - "shopping", - "sleep", - "smoke", - "smoking", - "snow", - "soap", - "software", - "soil", - "spaghetti", - "species", - "steam", - "stuff", - "stupidity", - "sunshine", - "symmetry", - - "tennis", - "thirst", - "thunder", - "timber", - "traffic", - "transportation", - "trust", - - "underwear", - "unemployment", - "unity", - - "validity", - "veal", - "vegetation", - "vegetarianism", - "vengeance", - "violence", - "vitality", - - "warmth", - "wealth", - "weather", - "welfare", - "wheat", - "wildlife", - "wisdom", - "yoga", - - "zinc", - "zoology"]; +pub const UNACCONTABLE_WORDS: [&str; 202] = [ + "accommodation", + "adulthood", + "advertising", + "advice", + "aggression", + "aid", + "air", + "aircraft", + "alcohol", + "anger", + "applause", + "arithmetic", + "assistance", + "athletics", + "bacon", + "baggage", + "beef", + "biology", + "blood", + "botany", + "bread", + "butter", + "carbon", + "cardboard", + "cash", + "chalk", + "chaos", + "chess", + "crossroads", + "countryside", + "dancing", + "deer", + "dignity", + "dirt", + "dust", + "economics", + "education", + "electricity", + "engineering", + "enjoyment", + "envy", + "equipment", + "ethics", + "evidence", + "evolution", + "fame", + "fiction", + "flour", + "flu", + "food", + "fuel", + "fun", + "furniture", + "gallows", + "garbage", + "garlic", + "genetics", + "gold", + "golf", + "gossip", + "grammar", + "gratitude", + "grief", + "guilt", + "gymnastics", + "happiness", + "hardware", + "harm", + "hate", + "hatred", + "health", + "heat", + "help", + "homework", + "honesty", + "honey", + "hospitality", + "housework", + "humour", + "hunger", + "hydrogen", + "ice", + "importance", + "inflation", + "information", + "innocence", + "iron", + "irony", + "jam", + "jewelry", + "judo", + "karate", + "knowledge", + "lack", + "laughter", + "lava", + "leather", + "leisure", + "lightning", + "linguine", + "linguini", + "linguistics", + "literature", + "litter", + "livestock", + "logic", + "loneliness", + "luck", + "luggage", + "macaroni", + "machinery", + "magic", + "management", + "mankind", + "marble", + "mathematics", + "mayonnaise", + "measles", + "methane", + "milk", + "money", + "mud", + "music", + "mumps", + "nature", + "news", + "nitrogen", + "nonsense", + "nurture", + "nutrition", + "obedience", + "obesity", + "oxygen", + "pasta", + "patience", + "physics", + "poetry", + "pollution", + "poverty", + "pride", + "psychology", + "publicity", + "punctuation", + "quartz", + "racism", + "relaxation", + "reliability", + "research", + "respect", + "revenge", + "rice", + "rubbish", + "rum", + "safety", + "scenery", + "seafood", + "seaside", + "series", + "shame", + "sheep", + "shopping", + "sleep", + "smoke", + "smoking", + "snow", + "soap", + "software", + "soil", + "spaghetti", + "species", + "steam", + "stuff", + "stupidity", + "sunshine", + "symmetry", + "tennis", + "thirst", + "thunder", + "timber", + "traffic", + "transportation", + "trust", + "underwear", + "unemployment", + "unity", + "validity", + "veal", + "vegetation", + "vegetarianism", + "vengeance", + "violence", + "vitality", + "warmth", + "wealth", + "weather", + "welfare", + "wheat", + "wildlife", + "wisdom", + "yoga", + "zinc", + "zoology", +]; diff --git a/src/string/deconstantize/mod.rs b/src/string/deconstantize/mod.rs index 5b106e56..51f91ef6 100644 --- a/src/string/deconstantize/mod.rs +++ b/src/string/deconstantize/mod.rs @@ -1,5 +1,5 @@ #[cfg(feature = "heavyweight")] -use cases::classcase::to_class_case; +use crate::cases::classcase::to_class_case; #[cfg(feature = "heavyweight")] /// Deconstantizes a `&str` diff --git a/src/string/demodulize/mod.rs b/src/string/demodulize/mod.rs index 5d4bd771..0c591be9 100644 --- a/src/string/demodulize/mod.rs +++ b/src/string/demodulize/mod.rs @@ -1,5 +1,5 @@ #[cfg(feature = "heavyweight")] -use cases::classcase::to_class_case; +use crate::cases::classcase::to_class_case; #[cfg(feature = "heavyweight")] /// Demodulize a `&str` diff --git a/src/string/mod.rs b/src/string/mod.rs index 72702881..038ade82 100644 --- a/src/string/mod.rs +++ b/src/string/mod.rs @@ -1,14 +1,14 @@ #![deny(warnings)] -/// Provides demodulize a string. -/// -/// Example string `Foo::Bar` becomes `Bar` -#[cfg(feature = "heavyweight")] -pub mod demodulize; /// Provides deconstantizea string. /// /// Example string `Foo::Bar` becomes `Foo` #[cfg(feature = "heavyweight")] pub mod deconstantize; +/// Provides demodulize a string. +/// +/// Example string `Foo::Bar` becomes `Bar` +#[cfg(feature = "heavyweight")] +pub mod demodulize; /// Provides conversion to plural strings. /// /// Example string `FooBar` -> `FooBars` diff --git a/src/string/pluralize/mod.rs b/src/string/pluralize/mod.rs index 1caeea52..354ecdc8 100644 --- a/src/string/pluralize/mod.rs +++ b/src/string/pluralize/mod.rs @@ -1,11 +1,11 @@ #![deny(warnings)] +use crate::string::constants::UNACCONTABLE_WORDS; use regex::Regex; -use string::constants::UNACCONTABLE_WORDS; -macro_rules! add_rule{ +macro_rules! add_rule { ($r:ident, $rule:expr => $replace:expr) => { $r.push((Regex::new($rule).unwrap(), $replace)); - } + }; } macro_rules! rules{ @@ -16,8 +16,7 @@ macro_rules! rules{ } } - -lazy_static!{ +lazy_static! { static ref RULES: Vec<(Regex, &'static str)> = { let mut r = Vec::with_capacity(24); rules![r; @@ -63,7 +62,6 @@ macro_rules! special_cases{ } } - /// Converts a `&str` to pluralized `String` /// /// ``` @@ -124,7 +122,7 @@ macro_rules! special_cases{ /// ``` /// pub fn to_plural(non_plural_string: &str) -> String { - if UNACCONTABLE_WORDS.contains(&non_plural_string.as_ref()) { + if UNACCONTABLE_WORDS.contains(&non_plural_string) { non_plural_string.to_owned() } else { special_cases![non_plural_string, @@ -140,7 +138,7 @@ pub fn to_plural(non_plural_string: &str) -> String { "quiz" => "quizzes" ]; for &(ref rule, replace) in RULES.iter().rev() { - if let Some(c) = rule.captures(&non_plural_string) { + if let Some(c) = rule.captures(non_plural_string) { if let Some(c) = c.get(1) { return format!("{}{}", c.as_str(), replace); } @@ -151,12 +149,13 @@ pub fn to_plural(non_plural_string: &str) -> String { } } - #[cfg(test)] mod tests { macro_rules! as_item { - ($i:item) => { $i }; + ($i:item) => { + $i + }; } macro_rules! make_tests{ @@ -180,7 +179,7 @@ mod tests { assert_eq!("boxes", super::to_plural("box")); } - make_tests!{ + make_tests! { geometry => geometries; ox => oxen; woman => women; diff --git a/src/string/singularize/mod.rs b/src/string/singularize/mod.rs index 60e0b51c..3d5a7b7b 100644 --- a/src/string/singularize/mod.rs +++ b/src/string/singularize/mod.rs @@ -1,5 +1,5 @@ +use crate::string::constants::UNACCONTABLE_WORDS; use regex::Regex; -use string::constants::UNACCONTABLE_WORDS; macro_rules! special_cases{ ($s:ident, $($singular: expr => $plural:expr), *) => { @@ -14,7 +14,6 @@ macro_rules! special_cases{ } } - /// Converts a `&str` to singularized `String` /// /// ``` @@ -75,7 +74,7 @@ macro_rules! special_cases{ /// ``` /// pub fn to_singular(non_singular_string: &str) -> String { - if UNACCONTABLE_WORDS.contains(&non_singular_string.as_ref()) { + if UNACCONTABLE_WORDS.contains(&non_singular_string) { non_singular_string.to_owned() } else { special_cases![non_singular_string, @@ -92,7 +91,7 @@ pub fn to_singular(non_singular_string: &str) -> String { "quizzes" => "quiz" ]; for &(ref rule, replace) in RULES.iter().rev() { - if let Some(captures) = rule.captures(&non_singular_string) { + if let Some(captures) = rule.captures(non_singular_string) { if let Some(c) = captures.get(1) { let mut buf = String::new(); captures.expand(&format!("{}{}", c.as_str(), replace), &mut buf); @@ -101,14 +100,14 @@ pub fn to_singular(non_singular_string: &str) -> String { } } - format!("{}", non_singular_string) + non_singular_string.to_string() } } -macro_rules! add_rule{ +macro_rules! add_rule { ($r:ident, $rule:expr => $replace:expr) => { $r.push((Regex::new($rule).unwrap(), $replace)); - } + }; } macro_rules! rules{ @@ -119,40 +118,39 @@ macro_rules! rules{ } } - -lazy_static!{ +lazy_static! { static ref RULES: Vec<(Regex, &'static str)> = { - let mut r = Vec::with_capacity(27); - rules![r; - r"(\w*)s$" => "", - r"(\w*)(ss)$" => "$2", - r"(n)ews$" => "ews", - r"(\w*)(o)es$" => "", - r"(\w*)([ti])a$" => "um", - r"((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)(sis|ses)$" => "sis", - r"(^analy)(sis|ses)$" => "sis", - r"(\w*)([^f])ves$" => "fe", - r"(\w*)(hive)s$" => "", - r"(\w*)(tive)s$" => "", - r"(\w*)([lr])ves$" => "f", - r"(\w*([^aeiouy]|qu))ies$" => "y", - r"(s)eries$" => "eries", - r"(m)ovies$" => "ovie", - r"(\w*)(x|ch|ss|sh)es$" => "$2", - r"(m|l)ice$" => "ouse", - r"(bus)(es)?$" => "", - r"(shoe)s$" => "", - r"(cris|test)(is|es)$" => "is", - r"^(a)x[ie]s$" => "xis", - r"(octop|vir)(us|i)$" => "us", - r"(alias|status)(es)?$" => "", - r"^(ox)en" => "", - r"(vert|ind)ices$" => "ex", - r"(matr)ices$" => "ix", - r"(quiz)zes$" => "", - r"(database)s$" => "" - ]; - r + let mut r = Vec::with_capacity(27); + rules![r; + r"(\w*)s$" => "", + r"(\w*)(ss)$" => "$2", + r"(n)ews$" => "ews", + r"(\w*)(o)es$" => "", + r"(\w*)([ti])a$" => "um", + r"((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)(sis|ses)$" => "sis", + r"(^analy)(sis|ses)$" => "sis", + r"(\w*)([^f])ves$" => "fe", + r"(\w*)(hive)s$" => "", + r"(\w*)(tive)s$" => "", + r"(\w*)([lr])ves$" => "f", + r"(\w*([^aeiouy]|qu))ies$" => "y", + r"(s)eries$" => "eries", + r"(m)ovies$" => "ovie", + r"(\w*)(x|ch|ss|sh)es$" => "$2", + r"(m|l)ice$" => "ouse", + r"(bus)(es)?$" => "", + r"(shoe)s$" => "", + r"(cris|test)(is|es)$" => "is", + r"^(a)x[ie]s$" => "xis", + r"(octop|vir)(us|i)$" => "us", + r"(alias|status)(es)?$" => "", + r"^(ox)en" => "", + r"(vert|ind)ices$" => "ex", + r"(matr)ices$" => "ix", + r"(quiz)zes$" => "", + r"(database)s$" => "" + ]; + r }; } @@ -177,7 +175,6 @@ fn singularize_string_if_a_regex_will_match() { let expected_string: String = "ox".to_owned(); let asserted_string: String = to_singular("oxen"); assert!(expected_string == asserted_string); - } #[test] diff --git a/src/suffix/foreignkey/mod.rs b/src/suffix/foreignkey/mod.rs index e46696d4..53cf5d5d 100644 --- a/src/suffix/foreignkey/mod.rs +++ b/src/suffix/foreignkey/mod.rs @@ -1,4 +1,4 @@ -use cases::snakecase::to_snake_case; +use crate::cases::snakecase::to_snake_case; /// Converts a `&str` to a `foreign_key` /// @@ -135,5 +135,5 @@ fn safe_convert(safe_string: &str) -> String { /// /// ``` pub fn is_foreign_key(test_string: &str) -> bool { - to_foreign_key(test_string.clone()) == test_string + to_foreign_key(test_string) == test_string }