tanzim_validate/
boolean.rs1use crate::error::{Error, ErrorKind};
2use crate::{Meta, Validator};
3use tanzim_value::{Value, ValueType};
4
5#[derive(Debug, Clone, Default)]
7pub struct Bool {
8 meta: Meta,
9}
10
11impl Bool {
12 pub fn new() -> Self {
13 Self::default()
14 }
15
16 pub fn with_meta(mut self, meta: Meta) -> Self {
18 self.meta = meta;
19 self
20 }
21}
22
23crate::impl_meta_methods!(Bool);
24
25impl Validator for Bool {
26 fn meta(&self) -> &Meta {
27 &self.meta
28 }
29
30 fn meta_mut(&mut self) -> &mut Meta {
31 &mut self.meta
32 }
33
34 fn check(&self, value: &mut Value) -> Result<(), Error> {
35 if value.is_bool() {
36 Ok(())
37 } else {
38 Err(Error::new(ErrorKind::Type {
39 expected: ValueType::Bool,
40 found: value.type_name(),
41 }))
42 }
43 }
44}
45
46#[cfg(test)]
47mod tests {
48 use super::*;
49
50 #[test]
51 fn accepts_bool() {
52 let mut value = Value::Bool(true);
53 assert!(Bool::new().validate(&mut value).is_ok());
54 }
55
56 #[test]
57 fn rejects_non_bool() {
58 let mut value = Value::Int(1);
59 let error = Bool::new().validate(&mut value).unwrap_err();
60 assert!(matches!(error.kind, ErrorKind::Type { .. }));
61 }
62}