Skip to main content

datex_core/values/value/
equality.rs

1use core::hash::{Hash, Hasher};
2
3use crate::{
4    traits::{structural_eq::StructuralEq, value_eq::ValueEq},
5    values::value::Value,
6};
7
8/// Two values are structurally equal, if their inner values are structurally equal, regardless
9/// of the actual_type of the values
10impl StructuralEq for Value {
11    fn structural_eq(&self, other: &Self) -> bool {
12        match (self, other) {
13            (Value::Core(core_self), Value::Core(core_other)) => {
14                core_self.structural_eq(core_other)
15            }
16            (Value::Native(native_self), Value::Native(native_other)) => {
17                native_self.structural_eq(native_other)
18            }
19            _ => {
20                todo!("Structural equality not implemented for these variants")
21            }
22        }
23    }
24}
25
26/// Value equality corresponds to partial equality:
27/// Both type and inner value are the same
28impl ValueEq for Value {
29    fn value_eq(&self, other: &Self) -> bool {
30        match (self, other) {
31            // core vs core equality
32            (Value::Core(core_self), Value::Core(core_other)) => {
33                core_self.value_eq(core_other)
34            }
35
36            // native vs native equality
37            (Value::Native(native_self), Value::Native(native_other)) => {
38                native_self.value_eq(native_other)
39            }
40
41            // core vs native equality
42            (Value::Core(_core_self), Value::Native(_native_other))
43            | (Value::Native(_native_other), Value::Core(_core_self)) => {
44                todo!()
45                // native_other.value.try_into_map_parts(cache)
46            }
47        }
48    }
49}
50
51impl PartialEq for Value {
52    fn eq(&self, other: &Self) -> bool {
53        match (self, other) {
54            // core vs core equality
55            (Value::Core(core_self), Value::Core(core_other)) => {
56                core_self.eq(core_other)
57            }
58
59            // native vs native equality
60            (Value::Native(native_self), Value::Native(native_other)) => {
61                native_self.eq(native_other)
62            }
63
64            // core vs native equality
65            (Value::Core(_core_self), Value::Native(_native_other))
66            | (Value::Native(_native_other), Value::Core(_core_self)) => {
67                todo!()
68                // native_other.value.try_into_map_parts(cache)
69            }
70        }
71    }
72}
73
74impl Eq for Value {}
75impl Hash for Value {
76    fn hash<H: Hasher>(&self, state: &mut H) {
77        match self {
78            Value::Core(core_self) => {
79                core_self.hash(state);
80            }
81            Value::Native(native_self) => {
82                native_self.datex_hash(state);
83            }
84        }
85    }
86}