fog_pack/validator/
bool.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
11#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
26#[serde(deny_unknown_fields, default)]
27pub struct BoolValidator {
28 #[serde(skip_serializing_if = "String::is_empty")]
30 pub comment: String,
31 #[serde(rename = "in", skip_serializing_if = "Vec::is_empty")]
33 pub in_list: Vec<bool>,
34 #[serde(rename = "nin", skip_serializing_if = "Vec::is_empty")]
36 pub nin_list: Vec<bool>,
37 #[serde(skip_serializing_if = "is_false")]
39 pub query: bool,
40}
41
42impl BoolValidator {
43 pub fn new() -> Self {
45 Self::default()
46 }
47
48 pub fn comment(mut self, comment: impl Into<String>) -> Self {
50 self.comment = comment.into();
51 self
52 }
53
54 pub fn in_add(mut self, add: bool) -> Self {
56 self.in_list.push(add);
57 self
58 }
59
60 pub fn nin_add(mut self, add: bool) -> Self {
62 self.nin_list.push(add);
63 self
64 }
65
66 pub fn query(mut self, query: bool) -> Self {
68 self.query = query;
69 self
70 }
71
72 pub fn build(self) -> Validator {
74 Validator::Bool(Box::new(self))
75 }
76
77 pub(crate) fn validate(&self, parser: &mut Parser) -> Result<()> {
78 let elem = parser
79 .next()
80 .ok_or_else(|| Error::FailValidate("Expected a boolean".to_string()))??;
81 let elem = if let Element::Bool(v) = elem {
82 v
83 } else {
84 return Err(Error::FailValidate(format!(
85 "Expected Bool, got {}",
86 elem.name()
87 )));
88 };
89 if !self.in_list.is_empty() && !self.in_list.iter().any(|v| *v == elem) {
90 return Err(Error::FailValidate(
91 "Boolean is not on `in` list".to_string(),
92 ));
93 }
94 if self.nin_list.iter().any(|v| *v == elem) {
95 return Err(Error::FailValidate("Boolean is on `nin` list".to_string()));
96 }
97 Ok(())
98 }
99
100 fn query_check_bool(&self, other: &Self) -> bool {
101 self.query || (other.in_list.is_empty() && other.nin_list.is_empty())
102 }
103
104 pub(crate) fn query_check(&self, other: &Validator) -> bool {
105 match other {
106 Validator::Bool(other) => self.query_check_bool(other),
107 Validator::Multi(list) => list.iter().all(|other| match other {
108 Validator::Bool(other) => self.query_check_bool(other),
109 _ => false,
110 }),
111 Validator::Any => true,
112 _ => false,
113 }
114 }
115}