protocheck_core/validators/
containing.rs1use std::collections::HashSet;
2
3use paste::paste;
4use proto_types::{Any, Duration};
5
6use crate::{
7 field_data::FieldContext,
8 protovalidate::Violation,
9 validators::static_data::{
10 base_violations::create_violation, in_violations::*, not_in_violations::*,
11 },
12};
13
14macro_rules! in_slice_list_validator {
15 (
16 $name_ty:ident,
17 $value_ty:ty,
18 $target_ty:ty
19 $(, $value_override:expr)?
20 ) => {
21 in_list_validator!(slice, $name_ty, $value_ty, [$target_ty] $(, $value_override)?);
22 };
23}
24
25macro_rules! in_hashset_list_validator {
26 (
27 $name_ty:ident,
28 $value_ty:ty,
29 $target_ty:ty
30 $(, $value_override:expr)?
31 ) => {
32 in_list_validator!(hashset, $name_ty, $value_ty, HashSet<$target_ty> $(, $value_override)? );
33 };
34}
35
36macro_rules! in_list_validator {
37 (
38 $list_ty:ident,
39 $name_ty:ident,
40 $value_ty:ty,
41 $target_ty:ty
42 $(, $value_override:expr)?
43 ) => {
44 macro_rules! _get_value_for_contains {
45 ($override:expr, $value:ident, $target:ident) => { ($override)($value, $target) };
46 (, $value:ident, $target:ident) => { $target.contains(&$value) };
47 }
48 paste! {
49 pub fn [<$name_ty _in_ $list_ty _list>](field_context: &FieldContext, value: $value_ty, target: &'static $target_ty, error_message: &'static str) -> Result<(), Violation> {
50 let check = _get_value_for_contains!($($value_override)?, value, target);
51
52 if check {
53 Ok(())
54 } else {
55 Err(create_violation(
56 field_context,
57 &[< $name_ty:upper _ IN_VIOLATION >],
58 concat!(stringify!($name_ty), ".in"),
59 error_message
60 ))
61 }
62 }
63
64 pub fn [<$name_ty _not_in_ $list_ty _list>](field_context: &FieldContext, value: $value_ty, target: &'static $target_ty, error_message: &'static str) -> Result<(), Violation> {
65 let check = !_get_value_for_contains!($($value_override)?, value, target);
66
67 if check {
68 Ok(())
69 } else {
70 Err(create_violation(
71 field_context,
72 &[< $name_ty:upper _ NOT_IN_VIOLATION >],
73 concat!(stringify!($name_ty), ".not_in"),
74 error_message
75 ))
76 }
77 }
78 }
79 };
80
81}
82
83in_hashset_list_validator!(string, &str, &'static str);
84in_slice_list_validator!(string, &str, &'static str);
85
86#[cfg(feature = "bytes")]
87in_hashset_list_validator!(
88 bytes,
89 &bytes::Bytes,
90 bytes::Bytes,
91 |value: &bytes::Bytes, target: &'static HashSet<::bytes::Bytes>| target.contains(value)
92);
93#[cfg(feature = "bytes")]
94in_slice_list_validator!(
95 bytes,
96 &bytes::Bytes,
97 &'static [u8],
98 |value: &bytes::Bytes, target: &'static [&'static [u8]]| target.iter().any(|by| by == value)
99);
100
101in_hashset_list_validator!(any, &Any, &'static str, |value: &Any,
102 target: &'static HashSet<
103 &'static str,
104>| {
105 target.contains(value.type_url.as_str())
106});
107in_slice_list_validator!(any, &Any, &'static str, |value: &Any, target: &[&str]| {
108 target.contains(&value.type_url.as_str())
109});
110
111in_hashset_list_validator!(duration, Duration, Duration);
112in_slice_list_validator!(duration, Duration, Duration);
113
114in_hashset_list_validator!(enum, i32, i32);
115in_slice_list_validator!(enum, i32, i32);
116
117in_hashset_list_validator!(
118 float,
119 f32,
120 u32,
121 |value: f32, target: &'static HashSet<u32>| target.contains(&value.to_bits())
122);
123in_slice_list_validator!(float, f32, f32);
124in_hashset_list_validator!(
125 double,
126 f64,
127 u64,
128 |value: f64, target: &'static HashSet<u64>| target.contains(&value.to_bits())
129);
130in_slice_list_validator!(double, f64, f64);
131
132in_hashset_list_validator!(int64, i64, i64);
133in_slice_list_validator!(int64, i64, i64);
134in_hashset_list_validator!(int32, i32, i32);
135in_slice_list_validator!(int32, i32, i32);
136in_hashset_list_validator!(sint64, i64, i64);
137in_slice_list_validator!(sint64, i64, i64);
138in_hashset_list_validator!(sint32, i32, i32);
139in_slice_list_validator!(sint32, i32, i32);
140in_hashset_list_validator!(sfixed64, i64, i64);
141in_slice_list_validator!(sfixed64, i64, i64);
142in_hashset_list_validator!(sfixed32, i32, i32);
143in_slice_list_validator!(sfixed32, i32, i32);
144
145in_hashset_list_validator!(fixed64, u64, u64);
146in_slice_list_validator!(fixed64, u64, u64);
147in_hashset_list_validator!(fixed32, u32, u32);
148in_slice_list_validator!(fixed32, u32, u32);
149in_hashset_list_validator!(uint64, u64, u64);
150in_slice_list_validator!(uint64, u64, u64);
151in_hashset_list_validator!(uint32, u32, u32);
152in_slice_list_validator!(uint32, u32, u32);