Skip to content

Commit 9e245f9

Browse files
committed
feat: added rule builder to simplify creating rules
1 parent 8abe840 commit 9e245f9

File tree

1 file changed

+238
-0
lines changed
  • build/rust/src/shared/helper

1 file changed

+238
-0
lines changed

build/rust/src/shared/helper/rule.rs

+238
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
use crate::shared::data_type_input_types_rule_config::DataTypeInputType;
2+
use crate::shared::{
3+
DataTypeContainsKeyRuleConfig, DataTypeContainsTypeRuleConfig, DataTypeInputTypesRuleConfig,
4+
DataTypeItemOfCollectionRuleConfig, DataTypeNumberRangeRuleConfig, DataTypeRegexRuleConfig,
5+
DataTypeReturnTypeRuleConfig, DataTypeRule, Value, data_type_rule::Config,
6+
};
7+
8+
pub struct RuleBuilder {
9+
rules: Vec<DataTypeRule>,
10+
}
11+
12+
impl RuleBuilder {
13+
pub fn new() -> Self {
14+
Self { rules: Vec::new() }
15+
}
16+
17+
pub fn add_contains_key(mut self, key: String, data_type_identifier: String) -> Self {
18+
self.rules.push(DataTypeRule {
19+
config: Some(Config::ContainsKey(DataTypeContainsKeyRuleConfig {
20+
key,
21+
data_type_identifier,
22+
})),
23+
});
24+
self
25+
}
26+
27+
pub fn add_contains_type(mut self, data_type_identifier: String) -> Self {
28+
self.rules.push(DataTypeRule {
29+
config: Some(Config::ContainsType(DataTypeContainsTypeRuleConfig {
30+
data_type_identifier,
31+
})),
32+
});
33+
self
34+
}
35+
36+
pub fn add_item_of_collection(mut self, items: Vec<Value>) -> Self {
37+
self.rules.push(DataTypeRule {
38+
config: Some(Config::ItemOfCollection(
39+
DataTypeItemOfCollectionRuleConfig { items },
40+
)),
41+
});
42+
self
43+
}
44+
45+
pub fn add_number_range(mut self, from: i64, to: i64, steps: Option<i64>) -> Self {
46+
self.rules.push(DataTypeRule {
47+
config: Some(Config::NumberRange(DataTypeNumberRangeRuleConfig {
48+
from,
49+
to,
50+
steps,
51+
})),
52+
});
53+
self
54+
}
55+
56+
pub fn add_regex(mut self, pattern: String) -> Self {
57+
self.rules.push(DataTypeRule {
58+
config: Some(Config::Regex(DataTypeRegexRuleConfig { pattern })),
59+
});
60+
self
61+
}
62+
63+
pub fn add_input_types(mut self, input_types: Vec<DataTypeInputType>) -> Self {
64+
self.rules.push(DataTypeRule {
65+
config: Some(Config::InputTypes(DataTypeInputTypesRuleConfig {
66+
input_types,
67+
})),
68+
});
69+
self
70+
}
71+
72+
pub fn add_return_type(mut self, data_type_identifier: String) -> Self {
73+
self.rules.push(DataTypeRule {
74+
config: Some(Config::ReturnType(DataTypeReturnTypeRuleConfig {
75+
data_type_identifier,
76+
})),
77+
});
78+
self
79+
}
80+
81+
pub fn build(self) -> Vec<DataTypeRule> {
82+
self.rules
83+
}
84+
}
85+
86+
#[cfg(test)]
87+
mod tests {
88+
use super::*;
89+
use crate::shared::{
90+
data_type_input_types_rule_config::DataTypeInputType, data_type_rule::Config,
91+
helper::value::ToValue,
92+
};
93+
94+
#[test]
95+
fn test_add_contains_key() {
96+
let rules = RuleBuilder::new()
97+
.add_contains_key("id".into(), "User".into())
98+
.build();
99+
100+
match &rules[0].config {
101+
Some(Config::ContainsKey(cfg)) => {
102+
assert_eq!(cfg.key, "id");
103+
assert_eq!(cfg.data_type_identifier, "User");
104+
}
105+
_ => panic!("Expected ContainsKey config"),
106+
}
107+
}
108+
109+
#[test]
110+
fn test_add_contains_type() {
111+
let rules = RuleBuilder::new().add_contains_type("User".into()).build();
112+
113+
match &rules[0].config {
114+
Some(Config::ContainsType(cfg)) => {
115+
assert_eq!(cfg.data_type_identifier, "User");
116+
}
117+
_ => panic!("Expected ContainsType config"),
118+
}
119+
}
120+
121+
#[test]
122+
fn test_add_item_of_collection() {
123+
let items = vec!["a".to_value(), 42.to_value()];
124+
let rules = RuleBuilder::new()
125+
.add_item_of_collection(items.clone())
126+
.build();
127+
128+
match &rules[0].config {
129+
Some(Config::ItemOfCollection(cfg)) => {
130+
assert_eq!(cfg.items, items);
131+
}
132+
_ => panic!("Expected ItemOfCollection config"),
133+
}
134+
}
135+
136+
#[test]
137+
fn test_add_number_range() {
138+
let rules = RuleBuilder::new().add_number_range(1, 10, Some(2)).build();
139+
140+
match &rules[0].config {
141+
Some(Config::NumberRange(cfg)) => {
142+
assert_eq!(cfg.from, 1);
143+
assert_eq!(cfg.to, 10);
144+
assert_eq!(cfg.steps, Some(2));
145+
}
146+
_ => panic!("Expected NumberRange config"),
147+
}
148+
}
149+
150+
#[test]
151+
fn test_add_regex() {
152+
let rules = RuleBuilder::new().add_regex(r"^\d+$".into()).build();
153+
154+
match &rules[0].config {
155+
Some(Config::Regex(cfg)) => {
156+
assert_eq!(cfg.pattern, r"^\d+$");
157+
}
158+
_ => panic!("Expected Regex config"),
159+
}
160+
}
161+
162+
#[test]
163+
fn test_add_input_types() {
164+
let input_types = vec![
165+
DataTypeInputType {
166+
data_type_identifier: "Type1".into(),
167+
input_identifier: "input1".into(),
168+
},
169+
DataTypeInputType {
170+
data_type_identifier: "Type2".into(),
171+
input_identifier: "input2".into(),
172+
},
173+
];
174+
175+
let rules = RuleBuilder::new()
176+
.add_input_types(input_types.clone())
177+
.build();
178+
179+
match &rules[0].config {
180+
Some(Config::InputTypes(cfg)) => {
181+
assert_eq!(cfg.input_types, input_types);
182+
}
183+
_ => panic!("Expected InputTypes config"),
184+
}
185+
}
186+
187+
#[test]
188+
fn test_add_return_type() {
189+
let rules = RuleBuilder::new().add_return_type("Result".into()).build();
190+
191+
match &rules[0].config {
192+
Some(Config::ReturnType(cfg)) => {
193+
assert_eq!(cfg.data_type_identifier, "Result");
194+
}
195+
_ => panic!("Expected ReturnType config"),
196+
}
197+
}
198+
199+
#[test]
200+
fn test_add_many_rules() {
201+
let rules = RuleBuilder::new()
202+
.add_contains_key("id".into(), "User".into())
203+
.add_return_type("Result".into())
204+
.add_regex(r"^\d+$".into())
205+
.add_contains_key("id".into(), "User".into())
206+
.build();
207+
208+
match &rules[0].config {
209+
Some(Config::ContainsKey(cfg)) => {
210+
assert_eq!(cfg.key, "id");
211+
assert_eq!(cfg.data_type_identifier, "User");
212+
}
213+
_ => panic!("Expected ContainsKey config"),
214+
}
215+
216+
match &rules[1].config {
217+
Some(Config::ReturnType(cfg)) => {
218+
assert_eq!(cfg.data_type_identifier, "Result");
219+
}
220+
_ => panic!("Expected ReturnType config"),
221+
}
222+
223+
match &rules[2].config {
224+
Some(Config::Regex(cfg)) => {
225+
assert_eq!(cfg.pattern, r"^\d+$");
226+
}
227+
_ => panic!("Expected Regex config"),
228+
}
229+
230+
match &rules[3].config {
231+
Some(Config::ContainsKey(cfg)) => {
232+
assert_eq!(cfg.key, "id");
233+
assert_eq!(cfg.data_type_identifier, "User");
234+
}
235+
_ => panic!("Expected ContainsKey config"),
236+
}
237+
}
238+
}

0 commit comments

Comments
 (0)