Skip to main content

datex_core/values/core_value/
equality.rs

1use crate::{
2    traits::{dyn_eq::DynEq, structural_eq::StructuralEq, value_eq::ValueEq},
3    values::{
4        core_value::CoreValue,
5        core_values::{
6            decimal::typed_decimal::TypedDecimal,
7            integer::typed_integer::TypedInteger,
8        },
9    },
10};
11
12impl StructuralEq for CoreValue {
13    fn structural_eq(&self, other: &Self) -> bool {
14        match (self, other) {
15            (CoreValue::Boolean(a), CoreValue::Boolean(b)) => {
16                a.structural_eq(b)
17            }
18
19            // Integers
20            (CoreValue::Integer(a), CoreValue::Integer(b)) => {
21                a.structural_eq(b)
22            }
23
24            // TypedIntegers
25            (CoreValue::TypedInteger(a), CoreValue::TypedInteger(b)) => {
26                a.structural_eq(b)
27            }
28
29            // Integers + TypedIntegers
30            (CoreValue::Integer(a), CoreValue::TypedInteger(b))
31            | (CoreValue::TypedInteger(b), CoreValue::Integer(a)) => {
32                TypedInteger::IBig(a.clone()).structural_eq(b)
33            }
34
35            // Decimals
36            (CoreValue::Decimal(a), CoreValue::Decimal(b)) => {
37                a.structural_eq(b)
38            }
39
40            // TypedDecimals
41            (CoreValue::TypedDecimal(a), CoreValue::TypedDecimal(b)) => {
42                a.structural_eq(b)
43            }
44
45            // Decimal + TypedDecimal
46            (CoreValue::Decimal(a), CoreValue::TypedDecimal(b))
47            | (CoreValue::TypedDecimal(b), CoreValue::Decimal(a)) => {
48                TypedDecimal::Decimal(a.clone()).structural_eq(b)
49            }
50
51            (CoreValue::Text(a), CoreValue::Text(b)) => a.structural_eq(b),
52            (CoreValue::Null, CoreValue::Null) => true,
53            (CoreValue::Endpoint(a), CoreValue::Endpoint(b)) => {
54                a.structural_eq(b)
55            }
56            (CoreValue::List(a), CoreValue::List(b)) => a.structural_eq(b),
57            (CoreValue::Map(a), CoreValue::Map(b)) => a.structural_eq(b),
58            (CoreValue::Type(a), CoreValue::Type(b)) => a.structural_eq(b),
59            (CoreValue::Callable(a), CoreValue::Callable(b)) => {
60                a.structural_eq(b)
61            }
62
63            (CoreValue::Range(a), CoreValue::Range(b)) => {
64                a.start.structural_eq(&b.start) && a.end.structural_eq(&b.end)
65            }
66
67            (CoreValue::Sender(s), CoreValue::Sender(t)) => s.structural_eq(t),
68            (CoreValue::Receiver(r), CoreValue::Receiver(t)) => {
69                r.structural_eq(t)
70            }
71            _ => false,
72        }
73    }
74}
75
76/// Value equality corresponds to partial equality for all values,
77/// except for decimals, where partial equality is also given for NaN values and +0.0 and -0.0.
78/// Therefore, we ValueEq is used instead for decimals
79impl ValueEq for CoreValue {
80    fn value_eq(&self, other: &Self) -> bool {
81        match (self, other) {
82            (CoreValue::Decimal(a), CoreValue::Decimal(b)) => a.value_eq(b),
83            (CoreValue::TypedDecimal(a), CoreValue::TypedDecimal(b)) => {
84                a.value_eq(b)
85            }
86            _ => self == other,
87        }
88    }
89}
90
91impl PartialEq for CoreValue {
92    fn eq(&self, other: &Self) -> bool {
93        match (self, other) {
94            (CoreValue::Uninitialized, CoreValue::Uninitialized) => true,
95            (CoreValue::Null, CoreValue::Null) => true,
96            (CoreValue::Instant(i1), CoreValue::Instant(i2)) => i1 == i2,
97            (CoreValue::Boolean(b1), CoreValue::Boolean(b2)) => b1 == b2,
98            (CoreValue::Integer(i1), CoreValue::Integer(i2)) => i1 == i2,
99            (CoreValue::TypedInteger(ti1), CoreValue::TypedInteger(ti2)) => {
100                ti1 == ti2
101            }
102            (CoreValue::Decimal(d1), CoreValue::Decimal(d2)) => d1 == d2,
103            (CoreValue::TypedDecimal(td1), CoreValue::TypedDecimal(td2)) => {
104                td1 == td2
105            }
106            (CoreValue::Text(t1), CoreValue::Text(t2)) => t1 == t2,
107            (CoreValue::Endpoint(e1), CoreValue::Endpoint(e2)) => e1 == e2,
108            (CoreValue::List(l1), CoreValue::List(l2)) => l1 == l2,
109            (CoreValue::Map(m1), CoreValue::Map(m2)) => m1 == m2,
110            (CoreValue::Type(t1), CoreValue::Type(t2)) => t1 == t2,
111            (
112                CoreValue::EntityTypeDefinition(etd1),
113                CoreValue::EntityTypeDefinition(etd2),
114            ) => etd1 == etd2,
115            (CoreValue::Callable(c1), CoreValue::Callable(c2)) => c1 == c2,
116            (CoreValue::Range(r1), CoreValue::Range(r2)) => r1 == r2,
117            (CoreValue::Box(b1), CoreValue::Box(b2)) => *b1 == *b2,
118            _ => false,
119        }
120    }
121}
122
123impl Eq for CoreValue {}
124impl CoreValue {
125    #[allow(unused)]
126    fn dyn_eq_native(&self, native: &dyn DynEq) -> bool {
127        match self {
128            CoreValue::Boolean(v) => native.dyn_eq(v),
129            CoreValue::Integer(v) => native.dyn_eq(v),
130            CoreValue::TypedInteger(v) => native.dyn_eq(v),
131            CoreValue::Decimal(v) => native.dyn_eq(v),
132            CoreValue::TypedDecimal(v) => native.dyn_eq(v),
133            CoreValue::Text(v) => native.dyn_eq(v),
134            CoreValue::Endpoint(v) => native.dyn_eq(v),
135            CoreValue::List(v) => native.dyn_eq(v),
136            CoreValue::Map(v) => native.dyn_eq(v),
137            CoreValue::Type(v) => native.dyn_eq(v),
138            CoreValue::Callable(v) => native.dyn_eq(v),
139            CoreValue::Range(v) => native.dyn_eq(v),
140            CoreValue::Null => native.dyn_eq(&()), //FIXME or false?
141            CoreValue::Instant(v) => native.dyn_eq(v),
142            CoreValue::Uninitialized => todo!(),
143            CoreValue::EntityTypeDefinition(_entity_type_definition) => todo!(),
144            CoreValue::Box(value_container) => value_container.dyn_eq(native), // FIXME
145            CoreValue::Sender(sender) => sender.dyn_eq(native),
146            CoreValue::Receiver(receiver) => receiver.dyn_eq(native),
147        }
148    }
149}
150
151#[cfg(test)]
152mod tests {
153
154    // TODO: on Value level
155    // #[test]
156    // fn native_eq() {
157    //     let native = CoreValue::Native(NativeCoreValue::new(Boolean(true)));
158    //     let non_native = CoreValue::Boolean(Boolean(true));
159    //     assert_eq!(native, non_native);
160    //
161    //     let native =
162    //         CoreValue::Native(NativeCoreValue::new(Endpoint::new("@jonas")));
163    //     let non_native = CoreValue::Endpoint(Endpoint::new("@jonas"));
164    //     assert_eq!(native, non_native);
165    //
166    //     let native = CoreValue::native(TypedInteger::I8(42));
167    //     let non_native = CoreValue::TypedInteger(TypedInteger::I8(42));
168    //     assert_eq!(native, non_native);
169    // }
170    //
171    // // FIXME
172    // #[test]
173    // fn native_eq_rust() {
174    //     let native = CoreValue::native(42);
175    //     let non_native = CoreValue::TypedInteger(TypedInteger::I8(42));
176    //     assert_eq!(native, non_native);
177    // }
178    //
179    // #[test]
180    // fn native_ne() {
181    //     let native = CoreValue::Native(NativeCoreValue::new(Boolean(true)));
182    //     let value = CoreValue::Boolean(Boolean(false));
183    //
184    //     assert_ne!(native, value);
185    //     assert_ne!(value, native);
186    // }
187}