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