1use crate::{settings, Spanned};
2
3use serde::de;
4
5use std::borrow::Borrow;
6use std::cmp::Ord;
7use std::fmt::{self, Debug, Formatter};
8use std::hash::Hash;
9use std::marker::PhantomData;
10
11
12
13#[cfg(not(feature = "indexmap"))] type MapImpl<K, V> = std::collections::BTreeMap<K, V>;
14#[cfg( feature = "indexmap" )] type MapImpl<K, V> = indexmap::IndexMap<K, V>;
15
16#[cfg(not(feature = "indexmap"))] type Entry<'s, K, V> = std::collections::btree_map::Entry<'s, K, V>;
17#[cfg( feature = "indexmap" )] type Entry<'s, K, V> = indexmap::map::Entry<'s, K, V>;
18
19#[derive(Clone, Debug)]
20pub struct Map<K: Hash + Ord, V> {
24 map: MapImpl<K, V>
25}
26
27impl<K: Hash + Ord, V> Map<K, V> {
28 #[doc = "Makes a new empty Map." ] pub fn new() -> Self { Map { map: MapImpl::new() } }
32 #[doc = "Returns the number of elements in the map." ] pub fn len(&self) -> usize { self.map.len() }
33 #[doc = "`true` if the map contains no elements." ] pub fn is_empty(&self) -> bool { self.map.is_empty() }
34 #[doc = "Clears the map, removing all elements." ] pub fn clear(&mut self) { self.map.clear() }
35 #[doc = "Inserts a key-value pair into the map, returning the replaced value, if any." ] pub fn insert(&mut self, k: K, v: V) -> Option<V> { self.map.insert(k, v) }
36 #[doc = "Gets the given key's corresponding entry in the map for in-place manipulation."] pub fn entry(&mut self, key: K) -> Entry<K, V> { self.map.entry(key) }
37
38 #[doc = "`true` if the map contains the given key." ] pub fn contains_key <Q>(&self, key: &Q) -> bool where Q: Eq + Ord + Hash + ?Sized, K: Borrow<Q>{ self.map.contains_key(key) }
39 #[doc = "Return a reference to the value stored for key, if any." ] pub fn get <Q>(&self, key: &Q) -> Option<&V> where Q: Eq + Ord + Hash + ?Sized, K: Borrow<Q>{ self.map.get(key) }
40 #[doc = "Returns the key-value pair corresponding to the supplied key, if any." ] pub fn get_key_value <Q>(&self, key: &Q) -> Option<(&K, &V)> where Q: Eq + Ord + Hash + ?Sized, K: Borrow<Q>{ self.map.get_key_value(key) }
41 #[doc = "Returns a mutable reference to the value stored for key, if any." ] pub fn get_mut <Q>(&mut self, key: &Q) -> Option<&mut V> where Q: Eq + Ord + Hash + ?Sized, K: Borrow<Q>{ self.map.get_mut(key) }
42 #[doc = "Removes a key from the map, returning the previous value, if any." ] pub fn remove <Q>(&mut self, key: &Q) -> Option<V> where Q: Eq + Ord + Hash + ?Sized, K: Borrow<Q>{ self.map.remove(key) }
43 #[doc = "Removes a key from the map, returning the previous key/value pair, if any." ] pub fn remove_entry <Q>(&mut self, key: &Q) -> Option<(K, V)> where Q: Eq + Ord + Hash + ?Sized, K: Borrow<Q>{ self.map.remove_entry(key) }
44
45 #[doc = "Gets an iterator over the keys of the map." ] pub fn keys (&self) -> impl Iterator<Item = &K> { self.map.keys().into_iter() }
46 #[doc = "Gets an iterator over the values of the map." ] pub fn values (&self) -> impl Iterator<Item = &V> { self.map.values().into_iter() }
47 #[doc = "Gets a mutable iterator over the values of the map." ] pub fn values_mut (&mut self) -> impl Iterator<Item = &mut V> { self.map.values_mut().into_iter() }
48 #[doc = "Gets an iterator over the entries of the map." ] pub fn iter (&self) -> impl Iterator<Item = (&K, &V)> { self.map.iter() }
49 #[doc = "Gets a mutable iterator over the entries of the map." ] pub fn iter_mut (&mut self) -> impl Iterator<Item = (&K, &mut V)> { self.map.iter_mut() }
50}
51
52impl<'a, K: Hash + Ord + 'a, V: 'a> IntoIterator for &'a Map<K, V> {
53 type Item = (&'a K, &'a V);
54 type IntoIter = <&'a MapImpl<K, V> as IntoIterator>::IntoIter;
55 fn into_iter(self) -> Self::IntoIter { (&self.map).into_iter() }
56}
57
58impl<'a, K: Hash + Ord + 'a, V: 'a> IntoIterator for &'a mut Map<K, V> {
59 type Item = (&'a K, &'a mut V);
60 type IntoIter = <&'a mut MapImpl<K, V> as IntoIterator>::IntoIter;
61 fn into_iter(self) -> Self::IntoIter { (&mut self.map).into_iter() }
62}
63
64impl<K: Hash + Ord, V> IntoIterator for Map<K, V> {
65 type Item = (K, V);
66 type IntoIter = <MapImpl<K, V> as IntoIterator>::IntoIter;
67 fn into_iter(self) -> Self::IntoIter { self.map.into_iter() }
68}
69
70impl<'a, K: Hash + Ord + 'a, V: 'a> IntoIterator for &'a Spanned<Map<K, V>> {
71 type Item = (&'a K, &'a V);
72 type IntoIter = <&'a MapImpl<K, V> as IntoIterator>::IntoIter;
73 fn into_iter(self) -> Self::IntoIter { (&self.get_ref().map).into_iter() }
74}
75
76impl<'a, K: Hash + Ord + 'a, V: 'a> IntoIterator for &'a mut Spanned<Map<K, V>> {
77 type Item = (&'a K, &'a mut V);
78 type IntoIter = <&'a mut MapImpl<K, V> as IntoIterator>::IntoIter;
79 fn into_iter(self) -> Self::IntoIter { (&mut self.get_mut().map).into_iter() }
80}
81
82impl<K: Hash + Ord, V> IntoIterator for Spanned<Map<K, V>> {
83 type Item = (K, V);
84 type IntoIter = <MapImpl<K, V> as IntoIterator>::IntoIter;
85 fn into_iter(self) -> Self::IntoIter { self.into_inner().map.into_iter() }
86}
87
88impl<'de, K: Debug + Hash + Ord + de::Deserialize<'de>, V: de::Deserialize<'de>> de::Deserialize<'de> for Map<K, V> {
89 fn deserialize<D: de::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
90 struct MapVisitor<'de, K: Ord + de::Deserialize<'de>, V: de::Deserialize<'de>>(PhantomData<(&'de (), K, V)>);
91 impl<'de, K: Debug + Hash + Ord + de::Deserialize<'de>, V: de::Deserialize<'de>> de::Visitor<'de> for MapVisitor<'de, K, V> {
92 type Value = Map<K, V>;
93 fn expecting(&self, formatter: &mut Formatter) -> fmt::Result { formatter.write_str("a JSON object") }
94 fn visit_map<MA: de::MapAccess<'de>>(self, mut visitor: MA) -> Result<Self::Value, MA::Error> {
95 let mut values = Map::new();
96 while let Some(key) = visitor.next_key()? {
97 if !settings().map_or(false, |s| s.allow_duplicate_keys) && values.contains_key(&key) {
98 return Err(de::Error::custom(format!("Duplicate field: {:?}", key)));
99 }
100 let value = visitor.next_value()?;
101 values.insert(key, value);
102 }
103 Ok(values)
104 }
105 }
106
107 deserializer.deserialize_any(MapVisitor::<'de, K, V>(PhantomData))
108 }
109}