fog_pack/validator/
float32.rs1use super::*;
2use crate::element::*;
3use crate::error::{Error, Result};
4use serde::{Deserialize, Serialize};
5
6#[inline]
7fn is_false(v: &bool) -> bool {
8 !v
9}
10#[inline]
11fn is_nan(v: &f32) -> bool {
12 v.is_nan()
13}
14
15#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
44#[serde(deny_unknown_fields, default)]
45pub struct F32Validator {
46 #[serde(skip_serializing_if = "String::is_empty")]
48 pub comment: String,
49 #[serde(skip_serializing_if = "is_nan")]
51 pub max: f32,
52 #[serde(skip_serializing_if = "is_nan")]
54 pub min: f32,
55 #[serde(skip_serializing_if = "is_false")]
57 pub ex_max: bool,
58 #[serde(skip_serializing_if = "is_false")]
60 pub ex_min: bool,
61 #[serde(rename = "in", skip_serializing_if = "Vec::is_empty")]
63 pub in_list: Vec<f32>,
64 #[serde(rename = "nin", skip_serializing_if = "Vec::is_empty")]
65 pub nin_list: Vec<f32>,
67 #[serde(skip_serializing_if = "is_false")]
69 pub query: bool,
70 #[serde(skip_serializing_if = "is_false")]
73 pub ord: bool,
74}
75
76impl std::default::Default for F32Validator {
77 fn default() -> Self {
78 Self {
79 comment: String::new(),
80 max: f32::NAN,
81 min: f32::NAN,
82 ex_max: false,
83 ex_min: false,
84 in_list: Vec::new(),
85 nin_list: Vec::new(),
86 query: false,
87 ord: false,
88 }
89 }
90}
91
92impl F32Validator {
93 pub fn new() -> Self {
95 Self::default()
96 }
97
98 pub fn comment(mut self, comment: impl Into<String>) -> Self {
100 self.comment = comment.into();
101 self
102 }
103
104 pub fn max(mut self, max: f32) -> Self {
106 self.max = max;
107 self
108 }
109
110 pub fn min(mut self, min: f32) -> Self {
112 self.min = min;
113 self
114 }
115
116 pub fn ex_max(mut self, ex_max: bool) -> Self {
118 self.ex_max = ex_max;
119 self
120 }
121
122 pub fn ex_min(mut self, ex_min: bool) -> Self {
124 self.ex_min = ex_min;
125 self
126 }
127
128 pub fn in_add(mut self, add: f32) -> Self {
130 self.in_list.push(add);
131 self
132 }
133
134 pub fn nin_add(mut self, add: f32) -> Self {
136 self.nin_list.push(add);
137 self
138 }
139
140 pub fn query(mut self, query: bool) -> Self {
142 self.query = query;
143 self
144 }
145
146 pub fn ord(mut self, ord: bool) -> Self {
148 self.ord = ord;
149 self
150 }
151
152 pub fn build(self) -> Validator {
154 Validator::F32(Box::new(self))
155 }
156
157 pub(crate) fn validate(&self, parser: &mut Parser) -> Result<()> {
158 let elem = parser
159 .next()
160 .ok_or_else(|| Error::FailValidate("Expected a f32".to_string()))??;
161 let elem = if let Element::F32(v) = elem {
162 v
163 } else {
164 return Err(Error::FailValidate(format!(
165 "Expected F32, got {}",
166 elem.name()
167 )));
168 };
169 let bytes = elem.to_ne_bytes();
170 if !self.in_list.is_empty() && !self.in_list.iter().any(|v| v.to_ne_bytes() == bytes) {
171 return Err(Error::FailValidate("F32 is not on `in` list".to_string()));
172 }
173 if self.nin_list.iter().any(|v| v.to_ne_bytes() == bytes) {
174 return Err(Error::FailValidate("F32 is on `nin` list".to_string()));
175 }
176 if !self.max.is_nan() && ((self.ex_max && elem >= self.max) || (elem > self.max)) {
177 return Err(Error::FailValidate(
178 "F32 greater than maximum allowed".to_string(),
179 ));
180 }
181 if !self.min.is_nan() && ((self.ex_min && elem <= self.min) || (elem < self.min)) {
182 return Err(Error::FailValidate(
183 "F32 less than maximum allowed".to_string(),
184 ));
185 }
186 Ok(())
187 }
188
189 fn query_check_f32(&self, other: &Self) -> bool {
190 (self.query || (other.in_list.is_empty() && other.nin_list.is_empty()))
191 && (self.ord
192 || (!other.ex_min && !other.ex_max && other.min.is_nan() && other.max.is_nan()))
193 }
194
195 pub(crate) fn query_check(&self, other: &Validator) -> bool {
196 match other {
197 Validator::F32(other) => self.query_check_f32(other),
198 Validator::Multi(list) => list.iter().all(|other| match other {
199 Validator::F32(other) => self.query_check_f32(other),
200 _ => false,
201 }),
202 Validator::Any => true,
203 _ => false,
204 }
205 }
206}