1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use super::*;
use crate::element::*;
use crate::error::{Error, Result};
use serde::{Deserialize, Serialize};
#[inline]
fn is_false(v: &bool) -> bool {
!v
}
#[inline]
fn is_nan(v: &f64) -> bool {
v.is_nan()
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields, default)]
pub struct F64Validator {
#[serde(skip_serializing_if = "String::is_empty")]
pub comment: String,
#[serde(skip_serializing_if = "is_nan")]
pub max: f64,
#[serde(skip_serializing_if = "is_nan")]
pub min: f64,
#[serde(skip_serializing_if = "is_false")]
pub ex_max: bool,
#[serde(skip_serializing_if = "is_false")]
pub ex_min: bool,
#[serde(rename = "in", skip_serializing_if = "Vec::is_empty")]
pub in_list: Vec<f64>,
#[serde(rename = "nin", skip_serializing_if = "Vec::is_empty")]
pub nin_list: Vec<f64>,
#[serde(skip_serializing_if = "is_false")]
pub query: bool,
#[serde(skip_serializing_if = "is_false")]
pub ord: bool,
}
impl std::default::Default for F64Validator {
fn default() -> Self {
Self {
comment: String::new(),
max: f64::NAN,
min: f64::NAN,
ex_max: false,
ex_min: false,
in_list: Vec::new(),
nin_list: Vec::new(),
query: false,
ord: false,
}
}
}
impl F64Validator {
pub fn new() -> Self {
Self::default()
}
pub fn comment(mut self, comment: impl Into<String>) -> Self {
self.comment = comment.into();
self
}
pub fn max(mut self, max: f64) -> Self {
self.max = max;
self
}
pub fn min(mut self, min: f64) -> Self {
self.min = min;
self
}
pub fn ex_max(mut self, ex_max: bool) -> Self {
self.ex_max = ex_max;
self
}
pub fn ex_min(mut self, ex_min: bool) -> Self {
self.ex_min = ex_min;
self
}
pub fn in_add(mut self, add: f64) -> Self {
self.in_list.push(add);
self
}
pub fn nin_add(mut self, add: f64) -> Self {
self.nin_list.push(add);
self
}
pub fn query(mut self, query: bool) -> Self {
self.query = query;
self
}
pub fn ord(mut self, ord: bool) -> Self {
self.ord = ord;
self
}
pub(crate) fn validate(&self, parser: &mut Parser) -> Result<()> {
let elem = parser
.next()
.ok_or_else(|| Error::FailValidate("Expected a f64".to_string()))??;
let elem = if let Element::F64(v) = elem {
v
} else {
return Err(Error::FailValidate(format!(
"Expected F64, got {}",
elem.name()
)));
};
let bytes = elem.to_ne_bytes();
if !self.in_list.is_empty() && !self.in_list.iter().any(|v| v.to_ne_bytes() == bytes) {
return Err(Error::FailValidate("F64 is not on `in` list".to_string()));
}
if self.nin_list.iter().any(|v| v.to_ne_bytes() == bytes) {
return Err(Error::FailValidate("F64 is on `nin` list".to_string()));
}
if !self.max.is_nan() && ((self.ex_max && elem >= self.max) || (elem > self.max)) {
return Err(Error::FailValidate(
"F64 greater than maximum allowed".to_string(),
));
}
if !self.min.is_nan() && ((self.ex_min && elem <= self.min) || (elem < self.min)) {
return Err(Error::FailValidate(
"F64 less than maximum allowed".to_string(),
));
}
Ok(())
}
fn query_check_f64(&self, other: &Self) -> bool {
(self.query || (other.in_list.is_empty() && other.nin_list.is_empty()))
&& (self.ord
|| (!other.ex_min && !other.ex_max && other.min.is_nan() && other.max.is_nan()))
}
pub(crate) fn query_check(&self, other: &Validator) -> bool {
match other {
Validator::F64(other) => self.query_check_f64(other),
Validator::Multi(list) => list.iter().all(|other| match other {
Validator::F64(other) => self.query_check_f64(other),
_ => false,
}),
Validator::Any => true,
_ => false,
}
}
}