icydb_base/validator/text/
case.rs1use crate::{core::traits::Validator, prelude::*};
2use canic_core::utils::case::{Case, Casing};
3
4#[validator]
9pub struct Kebab {}
10
11impl Validator<str> for Kebab {
12 fn validate(&self, s: &str) -> Result<(), String> {
13 if s.is_case(Case::Kebab) {
14 Ok(())
15 } else {
16 Err(format!("'{s}' is not kebab-case"))
17 }
18 }
19}
20
21#[validator]
26pub struct Lower {}
27
28impl Validator<str> for Lower {
29 fn validate(&self, s: &str) -> Result<(), String> {
30 if s.is_case(Case::Lower) {
31 Ok(())
32 } else {
33 Err(format!("'{s}' is not lower case"))
34 }
35 }
36}
37
38#[validator]
43pub struct LowerUscore {}
44
45impl Validator<str> for LowerUscore {
46 fn validate(&self, s: &str) -> Result<(), String> {
47 if s.chars().all(|c| c.is_lowercase() || c == '_') {
48 Ok(())
49 } else {
50 Err(format!("'{s}' is not lower case with_underscores"))
51 }
52 }
53}
54
55#[validator]
60pub struct Snake {}
61
62impl Validator<str> for Snake {
63 fn validate(&self, s: &str) -> Result<(), String> {
64 if s.is_case(Case::Snake) {
65 Ok(())
66 } else {
67 Err(format!("'{s}' is not snake_case"))
68 }
69 }
70}
71
72#[validator]
77pub struct Title {}
78
79impl Validator<str> for Title {
80 fn validate(&self, s: &str) -> Result<(), String> {
81 if s.is_case(Case::Title) {
82 Ok(())
83 } else {
84 Err(format!("'{s}' Is Not Title Case"))
85 }
86 }
87}
88
89#[validator]
94pub struct Upper {}
95
96impl Validator<str> for Upper {
97 fn validate(&self, s: &str) -> Result<(), String> {
98 if s.is_case(Case::Upper) {
99 Ok(())
100 } else {
101 Err(format!("'{s}' is not UPPER CASE"))
102 }
103 }
104}
105
106#[validator]
111pub struct UpperCamel {}
112
113impl Validator<str> for UpperCamel {
114 fn validate(&self, s: &str) -> Result<(), String> {
115 if s.is_case(Case::UpperCamel) {
116 Ok(())
117 } else {
118 Err(format!("'{s}' is not UpperCamelCase"))
119 }
120 }
121}
122
123#[validator]
128pub struct UpperSnake {}
129
130impl Validator<str> for UpperSnake {
131 fn validate(&self, s: &str) -> Result<(), String> {
132 if s.is_case(Case::UpperSnake) {
133 Ok(())
134 } else {
135 Err(format!("'{s}' is not UPPER_SNAKE_CASE"))
136 }
137 }
138}