doido_controller/
constraints.rs1pub fn numeric(s: &str) -> bool {
9 !s.is_empty() && s.bytes().all(|b| b.is_ascii_digit())
10}
11
12pub fn alpha(s: &str) -> bool {
14 !s.is_empty() && s.bytes().all(|b| b.is_ascii_alphabetic())
15}
16
17pub fn alphanumeric(s: &str) -> bool {
19 !s.is_empty() && s.bytes().all(|b| b.is_ascii_alphanumeric())
20}
21
22pub fn uuid_like(s: &str) -> bool {
24 let groups = [8, 4, 4, 4, 12];
25 let parts: Vec<&str> = s.split('-').collect();
26 parts.len() == groups.len()
27 && parts
28 .iter()
29 .zip(groups)
30 .all(|(p, n)| p.len() == n && p.bytes().all(|b| b.is_ascii_hexdigit()))
31}
32
33pub type Validator = fn(&str) -> bool;
35
36#[derive(Default)]
38pub struct Constraints {
39 rules: Vec<(String, Validator)>,
40}
41
42impl Constraints {
43 pub fn new() -> Self {
44 Self::default()
45 }
46
47 pub fn param(mut self, name: &str, validator: Validator) -> Self {
49 self.rules.push((name.to_string(), validator));
50 self
51 }
52
53 pub fn matches(&self, params: &[(&str, &str)]) -> bool {
56 self.rules.iter().all(|(name, validator)| {
57 params
58 .iter()
59 .find(|(k, _)| k == name)
60 .map(|(_, v)| validator(v))
61 .unwrap_or(false)
62 })
63 }
64}