Skip to main content

protocheck_core/validators/
comparables.rs

1use paste::paste;
2use proto_types::{Duration, Timestamp};
3
4use crate::{
5  field_data::FieldContext,
6  protovalidate::Violation,
7  validators::static_data::{
8    base_violations::create_violation, gt_violations::*, gte_violations::*, lt_violations::*,
9    lte_violations::*,
10  },
11};
12
13macro_rules! comparable_validator {
14  ($proto_type:ident, $rust_type:ty) => {
15    paste! {
16      pub fn [< $proto_type _lt >](field_context: &FieldContext, value: $rust_type, target: $rust_type, error_message: &'static str) -> Result<(), Violation> {
17        let check = value < target;
18
19        create_violation!($proto_type, check, field_context, lt, error_message)
20      }
21
22      pub fn [< $proto_type _lte >](field_context: &FieldContext, value: $rust_type, target: $rust_type, error_message: &'static str) -> Result<(), Violation> {
23        let check = value <= target;
24
25        create_violation!($proto_type, check, field_context, lte, error_message)
26      }
27
28      pub fn [< $proto_type _gt >](field_context: &FieldContext, value: $rust_type, target: $rust_type, error_message: &'static str) -> Result<(), Violation> {
29        let check = value > target;
30
31        create_violation!($proto_type, check, field_context, gt, error_message)
32      }
33
34      pub fn [< $proto_type _gte >](field_context: &FieldContext, value: $rust_type, target: $rust_type, error_message: &'static str) -> Result<(), Violation> {
35        let check = value >= target;
36
37        create_violation!($proto_type, check, field_context, gte, error_message)
38      }
39    }
40  };
41}
42
43comparable_validator!(float, f32);
44comparable_validator!(double, f64);
45
46comparable_validator!(int64, i64);
47comparable_validator!(int32, i32);
48comparable_validator!(sint64, i64);
49comparable_validator!(sint32, i32);
50comparable_validator!(sfixed64, i64);
51comparable_validator!(sfixed32, i32);
52
53comparable_validator!(uint64, u64);
54comparable_validator!(uint32, u32);
55comparable_validator!(fixed64, u64);
56comparable_validator!(fixed32, u32);
57
58comparable_validator!(duration, Duration);
59comparable_validator!(timestamp, Timestamp);