serde_yaml/value/
partial_eq.rs

1use crate::Value;
2
3impl PartialEq<str> for Value {
4    /// Compare `str` with YAML value
5    ///
6    /// # Examples
7    ///
8    /// ```
9    /// # use serde_yaml::Value;
10    /// assert!(Value::String("lorem".into()) == *"lorem");
11    /// ```
12    fn eq(&self, other: &str) -> bool {
13        self.as_str().map_or(false, |s| s == other)
14    }
15}
16
17impl<'a> PartialEq<&'a str> for Value {
18    /// Compare `&str` with YAML value
19    ///
20    /// # Examples
21    ///
22    /// ```
23    /// # use serde_yaml::Value;
24    /// assert!(Value::String("lorem".into()) == "lorem");
25    /// ```
26    fn eq(&self, other: &&str) -> bool {
27        self.as_str().map_or(false, |s| s == *other)
28    }
29}
30
31impl PartialEq<String> for Value {
32    /// Compare YAML value with String
33    ///
34    /// # Examples
35    ///
36    /// ```
37    /// # use serde_yaml::Value;
38    /// assert!(Value::String("lorem".into()) == "lorem".to_string());
39    /// ```
40    fn eq(&self, other: &String) -> bool {
41        self.as_str().map_or(false, |s| s == other)
42    }
43}
44
45impl PartialEq<bool> for Value {
46    /// Compare YAML value with bool
47    ///
48    /// # Examples
49    ///
50    /// ```
51    /// # use serde_yaml::Value;
52    /// assert!(Value::Bool(true) == true);
53    /// ```
54    fn eq(&self, other: &bool) -> bool {
55        self.as_bool().map_or(false, |b| b == *other)
56    }
57}
58
59macro_rules! partialeq_numeric {
60    ($([$($ty:ty)*], $conversion:ident, $base:ty)*) => {
61        $($(
62            impl PartialEq<$ty> for Value {
63                fn eq(&self, other: &$ty) -> bool {
64                    self.$conversion().map_or(false, |i| i == (*other as $base))
65                }
66            }
67
68            impl<'a> PartialEq<$ty> for &'a Value {
69                fn eq(&self, other: &$ty) -> bool {
70                    self.$conversion().map_or(false, |i| i == (*other as $base))
71                }
72            }
73
74            impl<'a> PartialEq<$ty> for &'a mut Value {
75                fn eq(&self, other: &$ty) -> bool {
76                    self.$conversion().map_or(false, |i| i == (*other as $base))
77                }
78            }
79        )*)*
80    }
81}
82
83partialeq_numeric! {
84    [i8 i16 i32 i64 isize], as_i64, i64
85    [u8 u16 u32 u64 usize], as_u64, u64
86    [f32 f64], as_f64, f64
87}