Skip to main content

icydb/base/validator/text/
case.rs

1use crate::{design::prelude::*, traits::Validator};
2use icydb_utils::{Case, Casing};
3
4///
5/// Kebab
6///
7
8#[validator]
9pub struct Kebab;
10
11impl Validator<str> for Kebab {
12    fn validate(&self, s: &str, ctx: &mut dyn VisitorContext) {
13        if !s.is_case(Case::Kebab) {
14            ctx.issue(format!("'{s}' is not kebab-case"));
15        }
16    }
17}
18
19///
20/// Lower
21///
22
23#[validator]
24pub struct Lower;
25
26impl Validator<str> for Lower {
27    fn validate(&self, s: &str, ctx: &mut dyn VisitorContext) {
28        if !s.is_case(Case::Lower) {
29            ctx.issue(format!("'{s}' is not lower case"));
30        }
31    }
32}
33
34///
35/// LowerUscore
36///
37
38#[validator]
39pub struct LowerUscore;
40
41impl Validator<str> for LowerUscore {
42    fn validate(&self, s: &str, ctx: &mut dyn VisitorContext) {
43        if !s.chars().all(|c| c.is_lowercase() || c == '_') {
44            ctx.issue(format!("'{s}' is not lower case with underscores"));
45        }
46    }
47}
48
49///
50/// Snake
51///
52
53#[validator]
54pub struct Snake;
55
56impl Validator<str> for Snake {
57    fn validate(&self, s: &str, ctx: &mut dyn VisitorContext) {
58        if !s.is_case(Case::Snake) {
59            ctx.issue(format!("'{s}' is not snake_case"));
60        }
61    }
62}
63
64///
65/// Title
66///
67
68#[validator]
69pub struct Title;
70
71impl Validator<str> for Title {
72    fn validate(&self, s: &str, ctx: &mut dyn VisitorContext) {
73        if !s.is_case(Case::Title) {
74            ctx.issue(format!("'{s}' is not Title Case"));
75        }
76    }
77}
78
79///
80/// Upper
81///
82
83#[validator]
84pub struct Upper;
85
86impl Validator<str> for Upper {
87    fn validate(&self, s: &str, ctx: &mut dyn VisitorContext) {
88        if !s.is_case(Case::Upper) {
89            ctx.issue(format!("'{s}' is not UPPER CASE"));
90        }
91    }
92}
93
94///
95/// UpperCamel
96///
97
98#[validator]
99pub struct UpperCamel;
100
101impl Validator<str> for UpperCamel {
102    fn validate(&self, s: &str, ctx: &mut dyn VisitorContext) {
103        if !s.is_case(Case::UpperCamel) {
104            ctx.issue(format!("'{s}' is not UpperCamelCase"));
105        }
106    }
107}
108
109///
110/// UpperSnake
111///
112
113#[validator]
114pub struct UpperSnake;
115
116impl Validator<str> for UpperSnake {
117    fn validate(&self, s: &str, ctx: &mut dyn VisitorContext) {
118        if !s.is_case(Case::UpperSnake) {
119            ctx.issue(format!("'{s}' is not UPPER_SNAKE_CASE"));
120        }
121    }
122}