Derive Macro sanitizer_macros::Sanitize [−][src]
#[derive(Sanitize)]
{
// Attributes available to this derive:
#[sanitize]
}
Expand description
The Sanitize derive macro implements the Sanitize trait for you.
The trait only has a single associated function called sanitize
which edits the fields based on the sanitizer you specified in
the helper attributes
Example
use sanitizer::prelude::*;
#[derive(Sanitize)]
struct User {
#[sanitize(trim)]
name: String,
#[sanitize(custom(eight))]
acc_no: u8
}
fn eight(mut acc_no: u8) -> u8 {
if acc_no != 8 {
acc_no = 8;
}
acc_no
}
fn main() {
let mut instance = User {
name: String::from("John, Doe "),
acc_no: 10
};
instance.sanitize();
assert_eq!(instance.name, "John, Doe");
assert_eq!(instance.acc_no, 8);
}
Available sanitizers
- trim: Trims the string.
- numeric: Remove numeric items from the string.
- alphanumeric: Remove alphanumeric items from the string.
- lower_case: Convert input to lower case.
- upper_case: Convert input to upper case.
- camel_case: Convert input to camel case.
- snake_case: Convert input to snake case.
- kebab_case: Convert input to kebab case.
- screaming_kebab_case: Convert input to shouty kebab case.
- e164: Convert a valid phone number to the e164 international standard, panic if invalid phone number.
- clamp(min, max): Limit an integer input to this region of min to max.
- clamp(max): Cut the string if it exceeds max.
- screaming_snake_case: Convert input to screaming snake case.
- custom(function): A custom function that is called to sanitize a field according to any other way.