Skip to main content

icydb_model/base/validator/text/
case.rs

1//! Module: base::validator::text::case
2//!
3//! Responsibility: base validator definitions.
4//! Does not own: normalization policy, persistence, or schema mutation semantics.
5//! Boundary: reports typed visitor issues for facade schema values.
6
7use crate::{prelude::*, visitor::Validator};
8
9use convert_case::{Case, Casing};
10
11///
12/// Camel
13///
14
15#[validator]
16pub struct Camel;
17
18impl Validator<str> for Camel {
19    fn validate(&self, s: &str, ctx: &mut dyn VisitorContext) {
20        if s.to_case(Case::Camel) != s {
21            ctx.issue(format!("'{s}' is not camelCase"));
22        }
23    }
24}
25
26///
27/// Kebab
28///
29
30#[validator]
31pub struct Kebab;
32
33impl Validator<str> for Kebab {
34    fn validate(&self, s: &str, ctx: &mut dyn VisitorContext) {
35        if s.to_case(Case::Kebab) != s {
36            ctx.issue(format!("'{s}' is not kebab-case"));
37        }
38    }
39}
40
41///
42/// Lower
43///
44
45#[validator]
46pub struct Lower;
47
48impl Validator<str> for Lower {
49    fn validate(&self, s: &str, ctx: &mut dyn VisitorContext) {
50        if s.to_case(Case::Lower) != s {
51            ctx.issue(format!("'{s}' is not lower case"));
52        }
53    }
54}
55
56///
57/// LowerUscore
58///
59
60#[validator]
61pub struct LowerUscore;
62
63impl Validator<str> for LowerUscore {
64    fn validate(&self, s: &str, ctx: &mut dyn VisitorContext) {
65        if !s.chars().all(|c| c.is_lowercase() || c == '_') {
66            ctx.issue(format!("'{s}' is not lower case with underscores"));
67        }
68    }
69}
70
71///
72/// Sentence
73///
74
75#[validator]
76pub struct Sentence;
77
78impl Validator<str> for Sentence {
79    fn validate(&self, s: &str, ctx: &mut dyn VisitorContext) {
80        if s.to_case(Case::Sentence) != s {
81            ctx.issue(format!("'{s}' is not Sentence case"));
82        }
83    }
84}
85
86///
87/// Snake
88///
89
90#[validator]
91pub struct Snake;
92
93impl Validator<str> for Snake {
94    fn validate(&self, s: &str, ctx: &mut dyn VisitorContext) {
95        if s.to_case(Case::Snake) != s {
96            ctx.issue(format!("'{s}' is not snake_case"));
97        }
98    }
99}
100
101///
102/// Title
103///
104
105#[validator]
106pub struct Title;
107
108impl Validator<str> for Title {
109    fn validate(&self, s: &str, ctx: &mut dyn VisitorContext) {
110        if s.to_case(Case::Title) != s {
111            ctx.issue(format!("'{s}' is not Title Case"));
112        }
113    }
114}
115
116///
117/// Upper
118///
119
120#[validator]
121pub struct Upper;
122
123impl Validator<str> for Upper {
124    fn validate(&self, s: &str, ctx: &mut dyn VisitorContext) {
125        if s.to_case(Case::Upper) != s {
126            ctx.issue(format!("'{s}' is not UPPER CASE"));
127        }
128    }
129}
130
131///
132/// UpperCamel
133///
134
135#[validator]
136pub struct UpperCamel;
137
138impl Validator<str> for UpperCamel {
139    fn validate(&self, s: &str, ctx: &mut dyn VisitorContext) {
140        if s.to_case(Case::UpperCamel) != s {
141            ctx.issue(format!("'{s}' is not UpperCamelCase"));
142        }
143    }
144}
145
146///
147/// UpperKebab
148///
149
150#[validator]
151pub struct UpperKebab;
152
153impl Validator<str> for UpperKebab {
154    fn validate(&self, s: &str, ctx: &mut dyn VisitorContext) {
155        if s.to_case(Case::UpperKebab) != s {
156            ctx.issue(format!("'{s}' is not UPPER-KEBAB-CASE"));
157        }
158    }
159}
160
161///
162/// UpperSnake
163///
164
165#[validator]
166pub struct UpperSnake;
167
168impl Validator<str> for UpperSnake {
169    fn validate(&self, s: &str, ctx: &mut dyn VisitorContext) {
170        if s.to_case(Case::UpperSnake) != s {
171            ctx.issue(format!("'{s}' is not UPPER_SNAKE_CASE"));
172        }
173    }
174}