platform_value/btreemap_extensions/
equal_underlying_data.rs

1use crate::Value;
2use std::collections::BTreeMap;
3/* ========================================================= *
4 *   Trait: EqualUnderlyingData                              *
5 * ========================================================= */
6
7/// Compare two structures by the data they ultimately represent,
8/// not necessarily by their concrete variants.
9pub trait EqualUnderlyingData {
10    fn equal_underlying_data(&self, other: &Self) -> bool;
11}
12
13/* ========================================================= *
14 *   Impl for `BTreeMap<String, Value>`                      *
15 * ========================================================= */
16
17impl EqualUnderlyingData for &BTreeMap<String, Value> {
18    fn equal_underlying_data(&self, other: &Self) -> bool {
19        // quick size check
20        if self.len() != other.len() {
21            return false;
22        }
23        // every key must exist in both and values must match by underlying data
24        self.iter().all(|(k, v_self)| {
25            other
26                .get(k)
27                .map(|v_other| v_self.equal_underlying_data(v_other))
28                .unwrap_or(false)
29        })
30    }
31}
32
33impl EqualUnderlyingData for BTreeMap<String, Value> {
34    fn equal_underlying_data(&self, other: &Self) -> bool {
35        // quick size check
36        if self.len() != other.len() {
37            return false;
38        }
39        // every key must exist in both and values must match by underlying data
40        self.iter().all(|(k, v_self)| {
41            other
42                .get(k)
43                .map(|v_other| v_self.equal_underlying_data(v_other))
44                .unwrap_or(false)
45        })
46    }
47}
48
49impl EqualUnderlyingData for BTreeMap<&String, Value> {
50    fn equal_underlying_data(&self, other: &Self) -> bool {
51        // quick size check
52        if self.len() != other.len() {
53            return false;
54        }
55        // every key must exist in both and values must match by underlying data
56        self.iter().all(|(k, v_self)| {
57            other
58                .get(k)
59                .map(|v_other| v_self.equal_underlying_data(v_other))
60                .unwrap_or(false)
61        })
62    }
63}
64
65impl EqualUnderlyingData for BTreeMap<&String, &Value> {
66    fn equal_underlying_data(&self, other: &Self) -> bool {
67        // quick size check
68        if self.len() != other.len() {
69            return false;
70        }
71        // every key must exist in both and values must match by underlying data
72        self.iter().all(|(k, v_self)| {
73            other
74                .get(k)
75                .map(|v_other| v_self.equal_underlying_data(v_other))
76                .unwrap_or(false)
77        })
78    }
79}
80
81impl EqualUnderlyingData for BTreeMap<String, &Value> {
82    fn equal_underlying_data(&self, other: &Self) -> bool {
83        // quick size check
84        if self.len() != other.len() {
85            return false;
86        }
87        // every key must exist in both and values must match by underlying data
88        self.iter().all(|(k, v_self)| {
89            other
90                .get(k)
91                .map(|v_other| v_self.equal_underlying_data(v_other))
92                .unwrap_or(false)
93        })
94    }
95}