diff --git a/Cargo.lock b/Cargo.lock index 18668c1..25a4cd8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,15 +1,5 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -[[package]] -name = "Inflector" -version = "0.11.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fe438c63458706e03479442743baae6c88256498e6431708f6dfc520a26515d3" -dependencies = [ - "lazy_static", - "regex", -] - [[package]] name = "aho-corasick" version = "0.7.15" @@ -71,6 +61,15 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "heck" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87cbf45460356b7deeb5e3415b5563308c0a9b057c85e12b06ad551f98d0a6ac" +dependencies = [ + "unicode-segmentation", +] + [[package]] name = "itertools" version = "0.9.0" @@ -237,9 +236,9 @@ checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" [[package]] name = "sanitizer" -version = "0.1.0" +version = "0.1.1" dependencies = [ - "Inflector", + "heck", "num-traits", "paste", "phonenumber", @@ -248,7 +247,7 @@ dependencies = [ [[package]] name = "sanitizer_macros" -version = "0.1.0" +version = "0.1.1" dependencies = [ "quote", "sanitizer", @@ -318,6 +317,12 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "unicode-segmentation" +version = "1.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb0d2e7be6ae3a5fa87eed5fb451aff96f2573d2694942e40543ae0bbe19c796" + [[package]] name = "unicode-xid" version = "0.2.1" diff --git a/Cargo.toml b/Cargo.toml index 947c6cf..f7a85f2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,13 +3,13 @@ name = "sanitizer" description = "A collection of methods and macros to sanitize struct fields" keywords = ["sanitizer", "validate", "trim", "e164", "case"] categories = ["text-processing", "value-formatting"] -version = "0.1.1" +version = "0.1.2" authors = ["weegee "] license = "MIT" edition = "2018" [dependencies] -Inflector = "0.11.4" +heck = "0.3.2" phonenumber = "0.3.1+8.12.9" paste = "1.0" num-traits = "0.2" diff --git a/README.md b/README.md index ecc0301..83c06b5 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ ints ```rust let int: u8 = 50; -let mut instance = IntSanitizer::new(int); +let mut instance = IntSanitizer::from(int); instance.clamp(99, 101); assert_eq!(99, instance.get()); ``` diff --git a/sanitizer-macros/Cargo.toml b/sanitizer-macros/Cargo.toml index a36d188..5a7c595 100644 --- a/sanitizer-macros/Cargo.toml +++ b/sanitizer-macros/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sanitizer_macros" -version = "0.1.1" +version = "0.1.2" edition = "2018" authors = ["weegee "] license = "MIT" diff --git a/sanitizer-macros/src/lib.rs b/sanitizer-macros/src/lib.rs index 7f578e4..3ada3d2 100644 --- a/sanitizer-macros/src/lib.rs +++ b/sanitizer-macros/src/lib.rs @@ -94,7 +94,7 @@ pub fn sanitize(input: TokenStream) -> TokenStream { }) } else { init.append_all(quote! { - let mut instance = IntSanitizer::new(self.#x); + let mut instance = IntSanitizer::from(self.#x); }) } call.append_all(quote! { diff --git a/sanitizer-macros/src/sanitizers/string.rs b/sanitizer-macros/src/sanitizers/string.rs index 63db6e8..85b0f7d 100644 --- a/sanitizer-macros/src/sanitizers/string.rs +++ b/sanitizer-macros/src/sanitizers/string.rs @@ -22,8 +22,8 @@ pub fn get_string_sanitizers(sanitizer: &PathOrList) -> Result Ok(quote! { alphanumeric() }), "lower_case" => Ok(quote! { to_lowercase() }), "upper_case" => Ok(quote! { to_uppercase() }), - "camel_case" => Ok(quote! { to_camelcase() }), - "snake_case" => Ok(quote! { to_snakecase() }), + "camel_case" => Ok(quote! { to_camel_case() }), + "snake_case" => Ok(quote! { to_snake_case() }), "screaming_snake_case" => Ok(quote! { to_screaming_snakecase() }), "e164" => Ok(quote! { e164() }), "clamp" => { diff --git a/sanitizer-macros/tests/custom_functions.rs b/sanitizer-macros/tests/custom_functions.rs index 4a4d8c7..2fe83ac 100644 --- a/sanitizer-macros/tests/custom_functions.rs +++ b/sanitizer-macros/tests/custom_functions.rs @@ -9,7 +9,7 @@ struct SanitizerTest { } fn func_int(field: u8) -> u8 { - let mut sanitizer = IntSanitizer::new(field); + let mut sanitizer = IntSanitizer::from(field); sanitizer.clamp(0, 5); sanitizer.get() } diff --git a/sanitizer-macros/tests/macro_test.rs b/sanitizer-macros/tests/macro_test.rs index 76609a9..f8a11ac 100644 --- a/sanitizer-macros/tests/macro_test.rs +++ b/sanitizer-macros/tests/macro_test.rs @@ -56,5 +56,5 @@ fn sanitizer_check() { assert_eq!(instance.clamp_str, "Hello, Wor"); assert_eq!(instance.clamp_int, 10); assert_eq!(instance.phone_number, "+1454"); - assert_eq!(instance.multiple_sanitizers, "HELLO_WORLD_123"); + assert_eq!(instance.multiple_sanitizers, "HELLO_WORLD123"); } diff --git a/src/int_sanitizer.rs b/src/int_sanitizer.rs index 6726c30..c344d03 100644 --- a/src/int_sanitizer.rs +++ b/src/int_sanitizer.rs @@ -1,3 +1,14 @@ +use std::convert::From; + +macro_rules! impl_from { + ( $type : tt ) => { + impl From<$type> for IntSanitizer<$type> { + fn from(content: $type) -> Self { + Self::new(content) + } + } + }; +} /// The IntSanitizer structure is a wrapper over a type T which is to /// be sanitized, T can be anything that's `PartialOrd` /// @@ -6,7 +17,7 @@ /// ``` /// use sanitizer::prelude::*; /// -/// let mut instance = IntSanitizer::new(5); +/// let mut instance = IntSanitizer::from(5); /// instance /// .clamp(9, 15); /// assert_eq!(instance.get(), 9); @@ -17,7 +28,7 @@ pub struct IntSanitizer(T); // TODO: Remove Copy since its restrictive impl IntSanitizer { /// Make a new instance of the struct from the given T - pub fn new(int: T) -> Self { + pub(crate) fn new(int: T) -> Self { Self(int) } /// Consume the struct and return T @@ -40,6 +51,17 @@ impl IntSanitizer { } } +impl_from!(u8); +impl_from!(u16); +impl_from!(u32); +impl_from!(u64); +impl_from!(usize); +impl_from!(isize); +impl_from!(i64); +impl_from!(i32); +impl_from!(i16); +impl_from!(i8); + #[cfg(test)] mod test { use super::*; @@ -47,7 +69,7 @@ mod test { #[test] fn basic_cap_min() { let int: u8 = 50; - let mut instance = IntSanitizer::new(int); + let mut instance = IntSanitizer::from(int); instance.clamp(99, 101); assert_eq!(99, instance.get()); } @@ -55,7 +77,7 @@ mod test { #[test] fn basic_cap_max() { let int: u8 = 200; - let mut instance = IntSanitizer::new(int); + let mut instance = IntSanitizer::from(int); instance.clamp(99, 101); assert_eq!(101, instance.get()); } diff --git a/src/string_sanitizer.rs b/src/string_sanitizer.rs index e9511a6..fc73bd9 100644 --- a/src/string_sanitizer.rs +++ b/src/string_sanitizer.rs @@ -1,6 +1,4 @@ -use inflector::cases::{ - camelcase::to_camel_case, screamingsnakecase::to_screaming_snake_case, snakecase::to_snake_case, -}; +use heck::*; use phonenumber::{parse, Mode}; use std::convert::From; @@ -56,18 +54,23 @@ impl StringSanitizer { self } /// Convert string to camel case - pub fn to_camelcase(&mut self) -> &mut Self { - self.0 = to_camel_case(&self.0); + pub fn to_camel_case(&mut self) -> &mut Self { + let s = self.0.to_camel_case(); + let mut c = s.chars(); + self.0 = match c.next() { + None => String::new(), + Some(f) => f.to_lowercase().collect::() + c.as_str(), + }; self } /// Convert string to snake case - pub fn to_snakecase(&mut self) -> &mut Self { - self.0 = to_snake_case(&self.0); + pub fn to_snake_case(&mut self) -> &mut Self { + self.0 = self.0.to_snake_case(); self } /// Convert string to screaming snake case pub fn to_screaming_snakecase(&mut self) -> &mut Self { - self.0 = to_screaming_snake_case(&self.0); + self.0 = self.0.to_shouty_snake_case(); self } /// Set the maximum lenght of the content @@ -108,7 +111,7 @@ impl From for StringSanitizer { impl From<&str> for StringSanitizer { fn from(content: &str) -> Self { - Self::new(content.to_string()) + Self::new(content.to_owned()) } } @@ -116,61 +119,29 @@ impl From<&str> for StringSanitizer { mod test { use super::*; - #[test] - fn trim() { - let mut sanitize = StringSanitizer::from(" Test "); - sanitize.trim(); - assert_eq!("Test", sanitize.get()); - } - - #[test] - fn numeric() { - let mut sanitize = StringSanitizer::from("Test123445Test"); - sanitize.numeric(); - assert_eq!("123445", sanitize.get()); - } - - #[test] - fn alphanumeric() { - let mut sanitize = StringSanitizer::from("Hello,藏World&&"); - sanitize.alphanumeric(); - assert_eq!("Hello藏World", sanitize.get()); - } - - #[test] - fn lowercase() { - let mut sanitize = StringSanitizer::from("HELLO"); - sanitize.to_lowercase(); - assert_eq!("hello", sanitize.get()); - } - - #[test] - fn uppercase() { - let mut sanitize = StringSanitizer::from("hello"); - sanitize.to_uppercase(); - assert_eq!("HELLO", sanitize.get()); - } - - #[test] - fn camelcase() { - let mut sanitize = StringSanitizer::from("some_string"); - sanitize.to_camelcase(); - assert_eq!("someString", sanitize.get()); - } - - #[test] - fn snakecase() { - let mut sanitize = StringSanitizer::from("someString"); - sanitize.to_snakecase(); - assert_eq!("some_string", sanitize.get()); - } - - #[test] - fn screaming_snakecase() { - let mut sanitize = StringSanitizer::from("someString"); - sanitize.to_screaming_snakecase(); - assert_eq!("SOME_STRING", sanitize.get()); - } + #[macro_use] + macro_rules! string_test { + ( $sanitizer : ident, $from : expr => $to : expr ) => { + paste::paste! { + #[test] + fn [<$sanitizer>]() { + let mut sanitize = StringSanitizer::from($from); + sanitize.$sanitizer(); + assert_eq!($to, sanitize.get()); + } + } + }; + } + + string_test!(trim, " Test " => "Test"); + string_test!(numeric, "Test123445Test" => "123445"); + string_test!(alphanumeric, "Hello,藏World&&" => "Hello藏World"); + string_test!(to_lowercase, "HELLO" => "hello"); + string_test!(to_uppercase, "hello" => "HELLO"); + string_test!(to_camel_case, "some_string" => "someString"); + string_test!(to_snake_case, "someString" => "some_string"); + string_test!(to_screaming_snakecase, "someString" => "SOME_STRING"); + string_test!(e164, "+1 (555) 555-1234" => "+15555551234"); #[test] fn clamp_max() { @@ -179,13 +150,6 @@ mod test { assert_eq!("someStrin", sanitize.get()); } - #[test] - fn phone_number() { - let mut sanitize = StringSanitizer::from("+1 (555) 555-1234"); - sanitize.e164(); - assert_eq!("+15555551234", sanitize.get()); - } - #[test] #[should_panic] fn wrong_phone_number() {