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
use heck::*;
use phonenumber::{parse, Mode};
use std::convert::From;
pub struct StringSanitizer(String);
impl StringSanitizer {
pub fn new(content: String) -> Self {
Self(content)
}
pub fn get(self) -> String {
self.0
}
pub fn trim(&mut self) -> &mut Self {
self.0 = self.0.trim().to_string();
self
}
pub fn numeric(&mut self) -> &mut Self {
self.0 = self.0.chars().filter(|b| b.is_numeric()).collect();
self
}
pub fn alphanumeric(&mut self) -> &mut Self {
self.0 = self.0.chars().filter(|b| b.is_alphanumeric()).collect();
self
}
pub fn to_lowercase(&mut self) -> &mut Self {
self.0 = self.0.to_lowercase();
self
}
pub fn to_uppercase(&mut self) -> &mut Self {
self.0 = self.0.to_uppercase();
self
}
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
}
pub fn to_snake_case(&mut self) -> &mut Self {
self.0 = self.0.to_snake_case();
self
}
pub fn to_screaming_snakecase(&mut self) -> &mut Self {
self.0 = self.0.to_shouty_snake_case();
self
}
pub fn clamp_max(&mut self, limit: usize) -> &mut Self {
self.0.truncate(limit);
self
}
pub fn e164(&mut self) -> &mut Self {
let phone_number = parse(None, &self.0);
if let Ok(x) = phone_number {
self.0 = x.format().mode(Mode::E164).to_string();
} else {
panic!("{:?}", "Not a valid phone number");
}
self
}
pub fn cut(&mut self, amount: usize) -> &mut Self {
self.0.truncate(amount);
self
}
pub fn call<F>(&mut self, func: F) -> &mut Self
where
F: FnOnce(&str) -> String,
{
self.0 = func(&self.0);
self
}
}
impl From<String> for StringSanitizer {
fn from(content: String) -> Self {
Self::new(content)
}
}
impl From<&str> for StringSanitizer {
fn from(content: &str) -> Self {
Self::new(content.to_owned())
}
}
#[cfg(test)]
mod test {
use super::*;
#[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() {
let mut sanitize = StringSanitizer::from("someString");
sanitize.clamp_max(9);
assert_eq!("someStrin", sanitize.get());
}
#[test]
#[should_panic]
fn wrong_phone_number() {
let mut sanitize = StringSanitizer::from("Not a Phone Number");
sanitize.e164();
}
#[test]
fn multiple_lints() {
let mut sanitize = StringSanitizer::from(" some_string12 ");
sanitize.trim().numeric();
assert_eq!("12", sanitize.get());
}
}