Skip to content

Commit

Permalink
Release 0.1.2 (dashxhq#6)
Browse files Browse the repository at this point in the history
* Add readme for sanitizer-macros and update cargo.toml

* Update cargo.toml

* minor parameter fixes for publish

* Update docs and links

* Update readme to add info about structs and methods

* Add custom function macro, docs for it and tests

* Update readme with the new sanitizer

* Update readme with the new sanitizer

* Bump to 0.1.1

* Edit workflows

* Add from impls for int types for int sanitizer

* Move to heck 3.2

* Release 0.1.2

* Release 0.1.2 for sanitizer macros
  • Loading branch information
Daksh14 authored Feb 24, 2021
1 parent e4671b0 commit 5273d4c
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 97 deletions.
31 changes: 18 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>"]
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"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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());
```
Expand Down
2 changes: 1 addition & 1 deletion sanitizer-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sanitizer_macros"
version = "0.1.1"
version = "0.1.2"
edition = "2018"
authors = ["weegee <[email protected]>"]
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion sanitizer-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {
Expand Down
4 changes: 2 additions & 2 deletions sanitizer-macros/src/sanitizers/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ pub fn get_string_sanitizers(sanitizer: &PathOrList) -> Result<TokenStream2, San
"alphanumeric" => 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" => {
Expand Down
2 changes: 1 addition & 1 deletion sanitizer-macros/tests/custom_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
2 changes: 1 addition & 1 deletion sanitizer-macros/tests/macro_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
30 changes: 26 additions & 4 deletions src/int_sanitizer.rs
Original file line number Diff line number Diff line change
@@ -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`
///
Expand All @@ -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);
Expand All @@ -17,7 +28,7 @@ pub struct IntSanitizer<T: PartialOrd + Copy>(T);
// TODO: Remove Copy since its restrictive
impl<T: PartialOrd + Copy> IntSanitizer<T> {
/// 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
Expand All @@ -40,22 +51,33 @@ impl<T: PartialOrd + Copy> IntSanitizer<T> {
}
}

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::*;

#[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());
}

#[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());
}
Expand Down
106 changes: 35 additions & 71 deletions src/string_sanitizer.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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::<String>() + 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
Expand Down Expand Up @@ -108,69 +111,37 @@ impl From<String> for StringSanitizer {

impl From<&str> for StringSanitizer {
fn from(content: &str) -> Self {
Self::new(content.to_string())
Self::new(content.to_owned())
}
}

#[cfg(test)]
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() {
Expand All @@ -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() {
Expand Down

0 comments on commit 5273d4c

Please sign in to comment.