Skip to main content

icydb/base/validator/text/
mod.rs

1pub mod case;
2pub mod color;
3
4use crate::{design::prelude::*, traits::Validator};
5
6///
7/// AlphaUscore
8/// this doesn't force ASCII; it uses Unicode `is_alphabetic`
9/// ASCII is handled by a separate validator
10///
11
12#[validator]
13pub struct AlphaUscore;
14
15impl Validator<str> for AlphaUscore {
16    fn validate(&self, s: &str, ctx: &mut dyn VisitorContext) {
17        if !s.chars().all(|c| c.is_alphabetic() || c == '_') {
18            ctx.issue(format!("'{s}' is not alphabetic with underscores"));
19        }
20    }
21}
22
23///
24/// AlphanumUscore
25///
26
27#[validator]
28pub struct AlphanumUscore;
29
30impl Validator<str> for AlphanumUscore {
31    fn validate(&self, s: &str, ctx: &mut dyn VisitorContext) {
32        if !s.chars().all(|c| c.is_alphanumeric() || c == '_') {
33            ctx.issue(format!("'{s}' is not alphanumeric with underscores"));
34        }
35    }
36}
37
38///
39/// Ascii
40///
41
42#[validator]
43pub struct Ascii;
44
45impl Validator<str> for Ascii {
46    fn validate(&self, s: &str, ctx: &mut dyn VisitorContext) {
47        if !s.is_ascii() {
48            ctx.issue("string contains non-ascii characters".to_string());
49        }
50    }
51}