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
//! The Sanitizer crate helps in sanitizing structured data
//! by providing [macros](https://docs.rs/sanitizer_macros/1.0.0/sanitizer_macros/derive.Sanitizer.html) and data structures to perform sanitization on
//! fields.
//!
//! Sanitizer uses [heck](https://docs.rs/heck/) to allow case conversions
//!
//! # Example
//!
//! If you want your incoming data which is serialised to a
//! structure to be sanitized then first of all, you make a struct
//! and derive the Sanitizer trait on it.
//! The macro will implement the trait for you all you have to do
//! now is to call the sanitize method on the trait
//!
//! ```
//! use sanitizer::prelude::*;
//!
//! #[derive(Sanitizer)]
//! struct User {
//! #[sanitizer(trim)]
//! name: String,
//! #[sanitizer(trim, lower_case)]
//! email: String
//! }
//!
//! fn main() {
//! let mut instance = User {
//! name: String::from(" John Doe123 "),
//! email: String::from(" JohnDoe123@email.com")
//! };
//! instance.sanitize();
//! assert_eq!(instance.name, "John Doe123");
//! assert_eq!(instance.email, "johndoe123@email.com");
//! }
//! ```
//! To see a list of available sanitizers, check the [sanitizer-macros crate](https://docs.rs/sanitizer_macros/0.1.0/sanitizer_macros/derive.Sanitize.html)
/// Bring all the sanitizers, the derive macro, and the Sanitizer trait in scope
/// Sanitizer methods for ints
pub use crateIntSanitizer;
/// Sanitizer methods for strings
pub use crateStringSanitizer;
/// The Sanitizer trait generalises types that are to be sanitized.
/// Generic `impl` for sanitizing values wrapped in an [Option]:
///
/// ```rust
/// use sanitizer::Sanitizer;
///
/// #[derive(Debug, PartialEq)]
/// struct MyValue(i32);
///
/// impl Sanitizer for MyValue {
/// /// If the inner value is `0`, change it to `1`.
/// fn sanitize(&mut self) {
/// if self.0 == 0 {
/// self.0 = 1;
/// }
/// }
/// }
///
/// let mut wrapped_value = Some(MyValue(0));
/// wrapped_value.sanitize();
/// assert_eq!(wrapped_value, Some(MyValue(1)));
/// ```
/// Generic `impl` for sanitizing values in a [Vec]:
///
/// ```rust
/// use sanitizer::Sanitizer;
///
/// #[derive(Debug, PartialEq)]
/// struct MyValue(i32);
///
/// impl Sanitizer for MyValue {
/// /// If the inner value is `0`, change it to `1`.
/// fn sanitize(&mut self) {
/// if self.0 == 0 {
/// self.0 = 1;
/// }
/// }
/// }
///
/// let mut values = vec![MyValue(0), MyValue(2)];
/// values.sanitize();
/// assert_eq!(values, vec![MyValue(1), MyValue(2)]);
/// ```