1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
use std::borrow::Borrow;
use std::hash::Hash;
use std::iter::FusedIterator;
use std::ops::{Index, IndexMut};

use crate::Value;

/// A map type with [`String`] keys and [`Value`] values.
#[derive(Clone, PartialEq, Default, Debug)]
pub struct Compound {
    map: Map,
}

#[cfg(not(feature = "preserve_order"))]
type Map = std::collections::HashMap<String, Value>;

#[cfg(feature = "preserve_order")]
type Map = indexmap::IndexMap<String, Value>;

impl Compound {
    pub fn new() -> Self {
        Self { map: Map::new() }
    }

    pub fn with_capacity(cap: usize) -> Self {
        Self {
            map: Map::with_capacity(cap),
        }
    }

    pub fn clear(&mut self) {
        self.map.clear();
    }

    pub fn get<Q>(&self, k: &Q) -> Option<&Value>
    where
        String: Borrow<Q>,
        Q: ?Sized + Eq + Ord + Hash,
    {
        self.map.get(k)
    }

    pub fn contains_key<Q>(&self, k: &Q) -> bool
    where
        String: Borrow<Q>,
        Q: ?Sized + Eq + Ord + Hash,
    {
        self.map.contains_key(k)
    }

    pub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut Value>
    where
        String: Borrow<Q>,
        Q: ?Sized + Eq + Ord + Hash,
    {
        self.map.get_mut(k)
    }

    pub fn get_key_value<Q>(&self, k: &Q) -> Option<(&String, &Value)>
    where
        String: Borrow<Q>,
        Q: ?Sized + Eq + Ord + Hash,
    {
        self.map.get_key_value(k)
    }

    pub fn insert<K, V>(&mut self, k: K, v: V) -> Option<Value>
    where
        K: Into<String>,
        V: Into<Value>,
    {
        self.map.insert(k.into(), v.into())
    }

    pub fn remove<Q>(&mut self, k: &Q) -> Option<Value>
    where
        String: Borrow<Q>,
        Q: ?Sized + Eq + Ord + Hash,
    {
        self.map.remove(k)
    }

    pub fn remove_entry<Q>(&mut self, k: &Q) -> Option<(String, Value)>
    where
        String: Borrow<Q>,
        Q: ?Sized + Eq + Ord + Hash,
    {
        self.map.remove_entry(k)
    }

    // TODO: append function for potential BTreeMap usage?

    pub fn entry<K>(&mut self, k: K) -> Entry
    where
        K: Into<String>,
    {
        #[cfg(not(feature = "preserve_order"))]
        use std::collections::hash_map::Entry as EntryImpl;

        #[cfg(feature = "preserve_order")]
        use indexmap::map::Entry as EntryImpl;

        match self.map.entry(k.into()) {
            EntryImpl::Vacant(ve) => Entry::Vacant(VacantEntry { ve }),
            EntryImpl::Occupied(oe) => Entry::Occupied(OccupiedEntry { oe }),
        }
    }

    pub fn len(&self) -> usize {
        self.map.len()
    }

    pub fn is_empty(&self) -> bool {
        self.map.is_empty()
    }

    pub fn iter(&self) -> Iter {
        Iter {
            iter: self.map.iter(),
        }
    }

    pub fn iter_mut(&mut self) -> IterMut {
        IterMut {
            iter: self.map.iter_mut(),
        }
    }

    pub fn keys(&self) -> Keys {
        Keys {
            iter: self.map.keys(),
        }
    }

    pub fn values(&self) -> Values {
        Values {
            iter: self.map.values(),
        }
    }

    pub fn values_mut(&mut self) -> ValuesMut {
        ValuesMut {
            iter: self.map.values_mut(),
        }
    }

    pub fn retain<F>(&mut self, f: F)
    where
        F: FnMut(&String, &mut Value) -> bool,
    {
        self.map.retain(f)
    }
}

impl Extend<(String, Value)> for Compound {
    fn extend<T>(&mut self, iter: T)
    where
        T: IntoIterator<Item = (String, Value)>,
    {
        self.map.extend(iter)
    }
}

impl FromIterator<(String, Value)> for Compound {
    fn from_iter<T>(iter: T) -> Self
    where
        T: IntoIterator<Item = (String, Value)>,
    {
        Self {
            map: Map::from_iter(iter),
        }
    }
}

pub enum Entry<'a> {
    Vacant(VacantEntry<'a>),
    Occupied(OccupiedEntry<'a>),
}

impl<'a> Entry<'a> {
    pub fn key(&self) -> &String {
        match self {
            Entry::Vacant(ve) => ve.key(),
            Entry::Occupied(oe) => oe.key(),
        }
    }

    pub fn or_insert(self, default: impl Into<Value>) -> &'a mut Value {
        match self {
            Entry::Vacant(ve) => ve.insert(default),
            Entry::Occupied(oe) => oe.into_mut(),
        }
    }

    pub fn or_insert_with<F, V>(self, default: F) -> &'a mut Value
    where
        F: FnOnce() -> V,
        V: Into<Value>,
    {
        match self {
            Entry::Vacant(ve) => ve.insert(default()),
            Entry::Occupied(oe) => oe.into_mut(),
        }
    }

    pub fn and_modify<F>(self, f: F) -> Self
    where
        F: FnOnce(&mut Value),
    {
        match self {
            Entry::Vacant(ve) => Entry::Vacant(ve),
            Entry::Occupied(mut oe) => {
                f(oe.get_mut());
                Entry::Occupied(oe)
            }
        }
    }
}

pub struct VacantEntry<'a> {
    #[cfg(not(feature = "preserve_order"))]
    ve: std::collections::hash_map::VacantEntry<'a, String, Value>,
    #[cfg(feature = "preserve_order")]
    ve: indexmap::map::VacantEntry<'a, String, Value>,
}

impl<'a> VacantEntry<'a> {
    pub fn key(&self) -> &String {
        self.ve.key()
    }

    pub fn insert(self, v: impl Into<Value>) -> &'a mut Value {
        self.ve.insert(v.into())
    }
}

pub struct OccupiedEntry<'a> {
    #[cfg(not(feature = "preserve_order"))]
    oe: std::collections::hash_map::OccupiedEntry<'a, String, Value>,
    #[cfg(feature = "preserve_order")]
    oe: indexmap::map::OccupiedEntry<'a, String, Value>,
}

impl<'a> OccupiedEntry<'a> {
    pub fn key(&self) -> &String {
        self.oe.key()
    }

    pub fn get(&self) -> &Value {
        self.oe.get()
    }

    pub fn get_mut(&mut self) -> &mut Value {
        self.oe.get_mut()
    }

    pub fn into_mut(self) -> &'a mut Value {
        self.oe.into_mut()
    }

    pub fn insert(&mut self, v: impl Into<Value>) -> Value {
        self.oe.insert(v.into())
    }

    pub fn remove(self) -> Value {
        self.oe.remove()
    }
}

impl<Q> Index<&'_ Q> for Compound
where
    String: Borrow<Q>,
    Q: ?Sized + Eq + Ord + Hash,
{
    type Output = Value;

    fn index(&self, index: &Q) -> &Self::Output {
        self.map.index(index)
    }
}

impl<Q> IndexMut<&'_ Q> for Compound
where
    String: Borrow<Q>,
    Q: ?Sized + Eq + Ord + Hash,
{
    fn index_mut(&mut self, index: &Q) -> &mut Self::Output {
        self.map.get_mut(index).expect("no entry found for key")
    }
}

macro_rules! impl_iterator_traits {
    (($name:ident $($generics:tt)*) => $item:ty) => {
        impl $($generics)* Iterator for $name $($generics)* {
            type Item = $item;
            #[inline]
            fn next(&mut self) -> Option<Self::Item> {
                self.iter.next()
            }
            #[inline]
            fn size_hint(&self) -> (usize, Option<usize>) {
                self.iter.size_hint()
            }
        }

        #[cfg(feature = "preserve_order")]
        impl $($generics)* DoubleEndedIterator for $name $($generics)* {
            #[inline]
            fn next_back(&mut self) -> Option<Self::Item> {
                self.iter.next_back()
            }
        }

        impl $($generics)* ExactSizeIterator for $name $($generics)* {
            #[inline]
            fn len(&self) -> usize {
                self.iter.len()
            }
        }

        impl $($generics)* FusedIterator for $name $($generics)* {}
    }
}

impl<'a> IntoIterator for &'a Compound {
    type Item = (&'a String, &'a Value);
    type IntoIter = Iter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        Iter {
            iter: self.map.iter(),
        }
    }
}

#[derive(Clone)]
pub struct Iter<'a> {
    #[cfg(not(feature = "preserve_order"))]
    iter: std::collections::hash_map::Iter<'a, String, Value>,
    #[cfg(feature = "preserve_order")]
    iter: indexmap::map::Iter<'a, String, Value>,
}

impl_iterator_traits!((Iter<'a>) => (&'a String, &'a Value));

impl<'a> IntoIterator for &'a mut Compound {
    type Item = (&'a String, &'a mut Value);
    type IntoIter = IterMut<'a>;

    fn into_iter(self) -> Self::IntoIter {
        IterMut {
            iter: self.map.iter_mut(),
        }
    }
}

pub struct IterMut<'a> {
    #[cfg(not(feature = "preserve_order"))]
    iter: std::collections::hash_map::IterMut<'a, String, Value>,
    #[cfg(feature = "preserve_order")]
    iter: indexmap::map::IterMut<'a, String, Value>,
}

impl_iterator_traits!((IterMut<'a>) => (&'a String, &'a mut Value));

impl IntoIterator for Compound {
    type Item = (String, Value);
    type IntoIter = IntoIter;

    fn into_iter(self) -> Self::IntoIter {
        IntoIter {
            iter: self.map.into_iter(),
        }
    }
}

pub struct IntoIter {
    #[cfg(not(feature = "preserve_order"))]
    iter: std::collections::hash_map::IntoIter<String, Value>,
    #[cfg(feature = "preserve_order")]
    iter: indexmap::map::IntoIter<String, Value>,
}

impl_iterator_traits!((IntoIter) => (String, Value));

#[derive(Clone)]
pub struct Keys<'a> {
    #[cfg(not(feature = "preserve_order"))]
    iter: std::collections::hash_map::Keys<'a, String, Value>,
    #[cfg(feature = "preserve_order")]
    iter: indexmap::map::Keys<'a, String, Value>,
}

impl_iterator_traits!((Keys<'a>) => &'a String);

#[derive(Clone)]
pub struct Values<'a> {
    #[cfg(not(feature = "preserve_order"))]
    iter: std::collections::hash_map::Values<'a, String, Value>,
    #[cfg(feature = "preserve_order")]
    iter: indexmap::map::Values<'a, String, Value>,
}

impl_iterator_traits!((Values<'a>) => &'a Value);

pub struct ValuesMut<'a> {
    #[cfg(not(feature = "preserve_order"))]
    iter: std::collections::hash_map::ValuesMut<'a, String, Value>,
    #[cfg(feature = "preserve_order")]
    iter: indexmap::map::ValuesMut<'a, String, Value>,
}

impl_iterator_traits!((ValuesMut<'a>) => &'a mut Value);