1 use crate::Value;
2 
3 impl PartialEq for Value {
eq(&self, other: &Value) -> bool4     fn eq(&self, other: &Value) -> bool {
5         match (self, other) {
6             (Value::Null, Value::Null) => true,
7             (Value::Bool(a), Value::Bool(b)) => a == b,
8             (Value::Number(a), Value::Number(b)) => a == b,
9             (Value::String(a), Value::String(b)) => a == b,
10             (Value::Sequence(a), Value::Sequence(b)) => a == b,
11             (Value::Mapping(a), Value::Mapping(b)) => a == b,
12             _ => false,
13         }
14     }
15 }
16 
17 impl PartialEq<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     /// ```
eq(&self, other: &str) -> bool26     fn eq(&self, other: &str) -> bool {
27         self.as_str().map_or(false, |s| s == other)
28     }
29 }
30 
31 impl<'a> PartialEq<&'a str> for Value {
32     /// Compare `&str` with YAML value
33     ///
34     /// # Examples
35     ///
36     /// ```
37     /// # use serde_yaml::Value;
38     /// assert!(Value::String("lorem".into()) == "lorem");
39     /// ```
eq(&self, other: &&str) -> bool40     fn eq(&self, other: &&str) -> bool {
41         self.as_str().map_or(false, |s| s == *other)
42     }
43 }
44 
45 impl PartialEq<Value> for str {
46     /// Compare YAML value with `str`
47     ///
48     /// # Examples
49     ///
50     /// ```
51     /// # use serde_yaml::Value;
52     /// assert!(*"lorem" == Value::String("lorem".into()));
53     /// ```
eq(&self, other: &Value) -> bool54     fn eq(&self, other: &Value) -> bool {
55         other.as_str().map_or(false, |s| s == self)
56     }
57 }
58 
59 impl<'a> PartialEq<Value> for &'a str {
60     /// Compare `&str` with YAML value
61     ///
62     /// # Examples
63     ///
64     /// ```
65     /// # use serde_yaml::Value;
66     /// assert!("lorem" == Value::String("lorem".into()));
67     /// ```
eq(&self, other: &Value) -> bool68     fn eq(&self, other: &Value) -> bool {
69         other.as_str().map_or(false, |s| s == *self)
70     }
71 }
72 
73 impl PartialEq<String> for Value {
74     /// Compare YAML value with String
75     ///
76     /// # Examples
77     ///
78     /// ```
79     /// # use serde_yaml::Value;
80     /// assert!(Value::String("lorem".into()) == "lorem".to_string());
81     /// ```
eq(&self, other: &String) -> bool82     fn eq(&self, other: &String) -> bool {
83         self.as_str().map_or(false, |s| s == other)
84     }
85 }
86 
87 impl PartialEq<Value> for String {
88     /// Compare `String` with YAML value
89     ///
90     /// # Examples
91     ///
92     /// ```
93     /// # use serde_yaml::Value;
94     /// assert!("lorem".to_string() == Value::String("lorem".into()));
95     /// ```
eq(&self, other: &Value) -> bool96     fn eq(&self, other: &Value) -> bool {
97         other.as_str().map_or(false, |s| s == self)
98     }
99 }
100 
101 impl PartialEq<bool> for Value {
102     /// Compare YAML value with bool
103     ///
104     /// # Examples
105     ///
106     /// ```
107     /// # use serde_yaml::Value;
108     /// assert!(Value::Bool(true) == true);
109     /// ```
eq(&self, other: &bool) -> bool110     fn eq(&self, other: &bool) -> bool {
111         self.as_bool().map_or(false, |b| b == *other)
112     }
113 }
114 
115 macro_rules! partialeq_numeric {
116     ($([$($ty:ty)*], $conversion:ident, $base:ty)*) => {
117         $($(
118             impl PartialEq<$ty> for Value {
119                 fn eq(&self, other: &$ty) -> bool {
120                     self.$conversion().map_or(false, |i| i == (*other as $base))
121                 }
122             }
123 
124             impl PartialEq<Value> for $ty {
125                 fn eq(&self, other: &Value) -> bool {
126                     other.$conversion().map_or(false, |i| i == (*self as $base))
127                 }
128             }
129 
130             impl<'a> PartialEq<$ty> for &'a Value {
131                 fn eq(&self, other: &$ty) -> bool {
132                     self.$conversion().map_or(false, |i| i == (*other as $base))
133                 }
134             }
135 
136             impl<'a> PartialEq<$ty> for &'a mut Value {
137                 fn eq(&self, other: &$ty) -> bool {
138                     self.$conversion().map_or(false, |i| i == (*other as $base))
139                 }
140             }
141         )*)*
142     }
143 }
144 
145 partialeq_numeric! {
146     [i8 i16 i32 i64 isize], as_i64, i64
147     [u8 u16 u32 u64 usize], as_u64, u64
148     [f32 f64], as_f64, f64
149 }
150