toasty_core/stmt/
value_cmp.rs

1//! PartialEq implementations for Value with Rust primitive types
2//!
3//! This module enables direct comparison between Value enum variants and their
4//! corresponding Rust primitive types, making test assertions cleaner and more readable.
5
6use super::{Expr, Value};
7
8/// Macro to implement PartialEq for numeric and simple types
9macro_rules! impl_value_eq {
10    ($($ty:ty => $variant:ident),* $(,)?) => {
11        $(
12            /// PartialEq implementation for Value and primitive type
13            impl PartialEq<$ty> for Value {
14                fn eq(&self, other: &$ty) -> bool {
15                    matches!(self, Value::$variant(val) if val == other)
16                }
17            }
18
19            /// PartialEq implementation for Expr and primitive type
20            impl PartialEq<$ty> for Expr {
21                fn eq(&self, other: &$ty) -> bool {
22                    matches!(self, Expr::Value(Value::$variant(val)) if val == other)
23                }
24            }
25
26            /// Reverse PartialEq implementation for convenience
27            impl PartialEq<Value> for $ty {
28                fn eq(&self, other: &Value) -> bool {
29                    other.eq(self)
30                }
31            }
32        )*
33    };
34}
35
36// Implement PartialEq for all numeric and boolean types
37impl_value_eq! {
38    bool => Bool,
39}
40
41// String types need special handling since they all map to Value::String
42
43/// PartialEq<String> for Value
44impl PartialEq<String> for Value {
45    fn eq(&self, other: &String) -> bool {
46        matches!(self, Value::String(val) if val == other)
47    }
48}
49
50/// PartialEq<String> for Expr
51impl PartialEq<String> for Expr {
52    fn eq(&self, other: &String) -> bool {
53        matches!(self, Expr::Value(Value::String(val)) if val == other)
54    }
55}
56
57/// PartialEq<&str> for Value
58impl PartialEq<&str> for Value {
59    fn eq(&self, other: &&str) -> bool {
60        matches!(self, Value::String(val) if val == other)
61    }
62}
63
64/// PartialEq<&str> for Expr
65impl PartialEq<&str> for Expr {
66    fn eq(&self, other: &&str) -> 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// Reverse implementations for string types
86
87/// PartialEq<Value> for String
88impl PartialEq<Value> for String {
89    fn eq(&self, other: &Value) -> bool {
90        other.eq(self)
91    }
92}
93
94/// PartialEq<Value> for &str
95impl PartialEq<Value> for &str {
96    fn eq(&self, other: &Value) -> bool {
97        other.eq(self)
98    }
99}
100
101/// PartialEq<Value> for str
102impl PartialEq<Value> for str {
103    fn eq(&self, other: &Value) -> bool {
104        other.eq(self)
105    }
106}