toasty_core/stmt/
value_cmp.rs

1//! [`PartialEq`] implementations for [`Value`] and [`Expr`] with Rust
2//! primitive types.
3//!
4//! This module enables direct comparison between `Value` / `Expr` enum
5//! variants and their corresponding Rust types (e.g., `bool`, `String`,
6//! `&str`), making assertions more ergonomic.
7//!
8//! # Examples
9//!
10//! ```
11//! use toasty_core::stmt::Value;
12//!
13//! let v = Value::from("hello");
14//! assert_eq!(v, "hello");
15//!
16//! let v = Value::from(true);
17//! assert_eq!(v, true);
18//! ```
19
20use super::{Expr, Value};
21
22/// Macro to implement PartialEq for numeric and simple types
23macro_rules! impl_value_eq {
24    ($($ty:ty => $variant:ident),* $(,)?) => {
25        $(
26            /// PartialEq implementation for Value and primitive type
27            impl PartialEq<$ty> for Value {
28                fn eq(&self, other: &$ty) -> bool {
29                    matches!(self, Value::$variant(val) if val == other)
30                }
31            }
32
33            /// PartialEq implementation for Expr and primitive type
34            impl PartialEq<$ty> for Expr {
35                fn eq(&self, other: &$ty) -> bool {
36                    matches!(self, Expr::Value(Value::$variant(val)) if val == other)
37                }
38            }
39
40            /// Reverse PartialEq implementation for convenience
41            impl PartialEq<Value> for $ty {
42                fn eq(&self, other: &Value) -> bool {
43                    other.eq(self)
44                }
45            }
46        )*
47    };
48}
49
50// Implement PartialEq for all numeric and boolean types
51impl_value_eq! {
52    bool => Bool,
53}
54
55// String types need special handling since they all map to Value::String
56
57/// PartialEq<String> for Value
58impl PartialEq<String> for Value {
59    fn eq(&self, other: &String) -> bool {
60        matches!(self, Value::String(val) if val == other)
61    }
62}
63
64/// PartialEq<String> for Expr
65impl PartialEq<String> for Expr {
66    fn eq(&self, other: &String) -> bool {
67        matches!(self, Expr::Value(Value::String(val)) if val == other)
68    }
69}
70
71/// PartialEq<&str> for Value
72impl PartialEq<&str> for Value {
73    fn eq(&self, other: &&str) -> bool {
74        matches!(self, Value::String(val) if val == other)
75    }
76}
77
78/// PartialEq<&str> for Expr
79impl PartialEq<&str> for Expr {
80    fn eq(&self, other: &&str) -> bool {
81        matches!(self, Expr::Value(Value::String(val)) if val == other)
82    }
83}
84
85/// PartialEq<str> for Value
86impl PartialEq<str> for Value {
87    fn eq(&self, other: &str) -> bool {
88        matches!(self, Value::String(val) if val == other)
89    }
90}
91
92/// PartialEq<str> for Expr
93impl PartialEq<str> for Expr {
94    fn eq(&self, other: &str) -> bool {
95        matches!(self, Expr::Value(Value::String(val)) if val == other)
96    }
97}
98
99// Reverse implementations for string types
100
101/// PartialEq<Value> for String
102impl PartialEq<Value> for String {
103    fn eq(&self, other: &Value) -> bool {
104        other.eq(self)
105    }
106}
107
108/// PartialEq<Value> for &str
109impl PartialEq<Value> for &str {
110    fn eq(&self, other: &Value) -> bool {
111        other.eq(self)
112    }
113}
114
115/// PartialEq<Value> for str
116impl PartialEq<Value> for str {
117    fn eq(&self, other: &Value) -> bool {
118        other.eq(self)
119    }
120}