1use serde::{Deserialize, Serialize};
2use std::borrow::Borrow;
3use std::collections::btree_map;
4use std::collections::BTreeMap;
5use std::fmt;
6use std::iter::FromIterator;
7use std::ops::{Deref, DerefMut};
8
9pub struct Map<K, V> {
11 values: BTreeMap<K, V>,
12}
13
14impl<K: Ord, V> Map<K, V> {
15 pub fn insert(&mut self, key: K, value: V) -> Option<V> {
16 self.values.insert(key, value)
17 }
18
19 #[must_use]
20 pub fn get<Q: Ord + ?Sized>(&self, key: &Q) -> Option<&V>
21 where
22 K: Borrow<Q>,
23 {
24 self.values.get(key)
25 }
26
27 pub fn get_mut<Q: Ord + ?Sized>(&mut self, key: &Q) -> Option<&mut V>
28 where
29 K: Borrow<Q>,
30 {
31 self.values.get_mut(key)
32 }
33
34 pub fn remove<Q: Ord + ?Sized>(&mut self, key: &Q) -> Option<V>
35 where
36 K: Borrow<Q>,
37 {
38 self.values.remove(key)
39 }
40}
41
42impl Map<String, crate::Value> {
43 #[must_use]
44 pub const fn new() -> Self {
45 Self {
46 values: BTreeMap::new(),
47 }
48 }
49
50 pub fn entry<S: Into<String>>(&mut self, key: S) -> btree_map::Entry<'_, String, crate::Value> {
51 self.values.entry(key.into())
52 }
53
54 #[must_use]
55 pub fn is_empty(&self) -> bool {
56 self.values.is_empty()
57 }
58
59 #[must_use]
60 pub fn len(&self) -> usize {
61 self.values.len()
62 }
63}
64
65impl<K: Ord, V> Default for Map<K, V> {
66 fn default() -> Self {
67 Self {
68 values: BTreeMap::new(),
69 }
70 }
71}
72
73impl<K: Ord + Clone, V: Clone> Clone for Map<K, V> {
74 fn clone(&self) -> Self {
75 Self {
76 values: self.values.clone(),
77 }
78 }
79}
80
81impl<K: Ord + fmt::Debug, V: fmt::Debug> fmt::Debug for Map<K, V> {
82 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
83 self.values.fmt(formatter)
84 }
85}
86
87impl<K: Ord + PartialEq, V: PartialEq> PartialEq for Map<K, V> {
88 fn eq(&self, other: &Self) -> bool {
89 self.values == other.values
90 }
91}
92
93impl<K: Ord + Eq, V: Eq> Eq for Map<K, V> {}
94
95impl<K: Ord, V> Deref for Map<K, V> {
96 type Target = BTreeMap<K, V>;
97
98 fn deref(&self) -> &Self::Target {
99 &self.values
100 }
101}
102
103impl<K: Ord, V> DerefMut for Map<K, V> {
104 fn deref_mut(&mut self) -> &mut Self::Target {
105 &mut self.values
106 }
107}
108
109impl<K: Ord, V> FromIterator<(K, V)> for Map<K, V> {
110 fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self {
111 Self {
112 values: BTreeMap::from_iter(iter),
113 }
114 }
115}
116
117impl<K: Ord, V> Extend<(K, V)> for Map<K, V> {
118 fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T) {
119 self.values.extend(iter);
120 }
121}
122
123impl<K: Ord, V, const N: usize> From<[(K, V); N]> for Map<K, V> {
124 fn from(entries: [(K, V); N]) -> Self {
125 entries.into_iter().collect()
126 }
127}
128
129impl<K: Ord, V> From<BTreeMap<K, V>> for Map<K, V> {
130 fn from(values: BTreeMap<K, V>) -> Self {
131 Self { values }
132 }
133}
134
135impl<K: Ord, V> From<Map<K, V>> for BTreeMap<K, V> {
136 fn from(map: Map<K, V>) -> Self {
137 map.values
138 }
139}
140
141impl<K: Ord, V> IntoIterator for Map<K, V> {
142 type Item = (K, V);
143 type IntoIter = btree_map::IntoIter<K, V>;
144
145 fn into_iter(self) -> Self::IntoIter {
146 self.values.into_iter()
147 }
148}
149
150impl<'a, K: Ord, V> IntoIterator for &'a Map<K, V> {
151 type Item = (&'a K, &'a V);
152 type IntoIter = btree_map::Iter<'a, K, V>;
153
154 fn into_iter(self) -> Self::IntoIter {
155 self.values.iter()
156 }
157}
158
159impl<'a, K: Ord, V> IntoIterator for &'a mut Map<K, V> {
160 type Item = (&'a K, &'a mut V);
161 type IntoIter = btree_map::IterMut<'a, K, V>;
162
163 fn into_iter(self) -> Self::IntoIter {
164 self.values.iter_mut()
165 }
166}
167
168impl<K: Ord + Serialize, V: Serialize> Serialize for Map<K, V> {
169 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
170 self.values.serialize(serializer)
171 }
172}
173
174impl<'de, K: Ord + Deserialize<'de>, V: Deserialize<'de>> Deserialize<'de> for Map<K, V> {
175 fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
176 BTreeMap::deserialize(deserializer).map(Self::from)
177 }
178}