proto_types/protovalidate/
comparable_rules.rs1use std::fmt::Debug;
2
3use quote::ToTokens;
4
5use crate::Timestamp;
6
7pub struct TimestampComparableRules {
8 pub comparable_rules: ComparableRules<Timestamp>,
9 pub lt_now: bool,
10 pub gt_now: bool,
11}
12
13pub enum ComparableLessThan<T> {
14 Lt { val: T, error_message: String },
15 Lte { val: T, error_message: String },
16}
17
18impl<T> ComparableLessThan<T>
19where
20 T: Copy,
21{
22 pub fn value(&self) -> T {
23 match self {
24 Self::Lte { val, .. } => *val,
25 Self::Lt { val, .. } => *val,
26 }
27 }
28}
29
30impl<T> ComparableGreaterThan<T>
31where
32 T: Copy,
33{
34 pub fn value(&self) -> T {
35 match self {
36 Self::Gte { val, .. } => *val,
37 Self::Gt { val, .. } => *val,
38 }
39 }
40}
41
42pub enum ComparableGreaterThan<T> {
43 Gt { val: T, error_message: String },
44 Gte { val: T, error_message: String },
45}
46
47pub struct ComparableRules<T>
48where
49 T: PartialOrd + PartialEq + Debug + ToTokens,
50{
51 pub less_than: Option<ComparableLessThan<T>>,
52 pub greater_than: Option<ComparableGreaterThan<T>>,
53}
54
55impl ComparableRules<Timestamp> {}
56
57impl<T> ComparableRules<T>
58where
59 T: PartialOrd + PartialEq + Debug + ToTokens,
60{
61 pub fn validate(self) -> Result<Self, &'static str> {
62 if let Some(ref gt_rule) = self.greater_than
63 && let Some(ref lt_rule) = self.less_than {
64 match gt_rule {
65 ComparableGreaterThan::Gte { val: gte_val,.. } => {
66 match lt_rule {
67 ComparableLessThan::Lte { val:lte_val,.. } => {
68 if lte_val < gte_val {
69 return Err("Lte cannot be smaller than Gte");
70 }
71 }
72 ComparableLessThan::Lt { val:lt_val,.. } => {
73 if lt_val <= gte_val {
74 return Err("Lt cannot be smaller than Gte");
75 }
76 }
77 };
78 }
79 ComparableGreaterThan::Gt { val: gt_val, .. } => {
80 match lt_rule {
81 ComparableLessThan::Lte { val: lte_val, .. } => {
82 if lte_val <= gt_val {
83 return Err("Lte cannot be smaller than or equal to Gt");
84 }
85 }
86 ComparableLessThan::Lt { val: lt_val, .. } => {
87 if lt_val <= gt_val {
88 return Err("Lt cannot be smaller than or equal to Gt");
89 }
90 }
91 };
92 }
93 };
94 }
95 Ok(self)
96 }
97}