icydb_base/validator/text/
mod.rs

1pub mod case;
2pub mod color;
3
4use crate::{core::traits::Validator, prelude::*};
5
6///
7/// AlphaUscore
8/// this doesn't force ASCII, instead we're using the unicode is_alphabetic
9/// and ASCII is handled in a separate validator
10///
11
12#[validator]
13pub struct AlphaUscore {}
14
15impl Validator<str> for AlphaUscore {
16    fn validate(&self, s: &str) -> Result<(), String> {
17        if s.chars().all(|c| c.is_alphabetic() || c == '_') {
18            Ok(())
19        } else {
20            Err(format!("'{s}' is not alphabetic with underscores"))
21        }
22    }
23}
24
25///
26/// AlphanumUscore
27///
28
29#[validator]
30pub struct AlphanumUscore {}
31
32impl Validator<str> for AlphanumUscore {
33    fn validate(&self, s: &str) -> Result<(), String> {
34        if s.chars().all(|c| c.is_alphanumeric() || c == '_') {
35            Ok(())
36        } else {
37            Err(format!("'{s}' is not alphanumeric with underscores"))
38        }
39    }
40}
41
42///
43/// Ascii
44///
45
46#[validator]
47pub struct Ascii {}
48
49impl Validator<str> for Ascii {
50    fn validate(&self, s: &str) -> Result<(), String> {
51        if s.is_ascii() {
52            Ok(())
53        } else {
54            Err("string contains non-ascii characters".to_string())
55        }
56    }
57}