Skip to main content

datex_core/values/core_value/
equality.rs

1use crate::{
2    traits::{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            _ => false,
67        }
68    }
69}
70
71/// Value equality corresponds to partial equality for all values,
72/// except for decimals, where partial equality is also given for NaN values and +0.0 and -0.0.
73/// Therefore, we ValueEq is used instead for decimals
74impl ValueEq for CoreValue {
75    fn value_eq(&self, other: &Self) -> bool {
76        match (self, other) {
77            (CoreValue::Decimal(a), CoreValue::Decimal(b)) => a.value_eq(b),
78            (CoreValue::TypedDecimal(a), CoreValue::TypedDecimal(b)) => {
79                a.value_eq(b)
80            }
81            _ => self == other,
82        }
83    }
84}