]> git.proxmox.com Git - rustc.git/blob - vendor/serde_yaml/src/mapping.rs
New upstream version 1.46.0~beta.2+dfsg1
[rustc.git] / vendor / serde_yaml / src / mapping.rs
1 //! A YAML mapping and its iterator types.
2
3 use crate::Value;
4 use linked_hash_map::LinkedHashMap;
5 use serde::{Deserialize, Deserializer, Serialize};
6 use std::fmt;
7 use std::iter::FromIterator;
8 use std::ops::{Index, IndexMut};
9
10 /// A YAML mapping in which the keys and values are both `serde_yaml::Value`.
11 #[derive(Clone, Debug, Default, Eq, Hash, PartialEq, PartialOrd)]
12 pub struct Mapping {
13 map: LinkedHashMap<Value, Value>,
14 }
15
16 impl Mapping {
17 /// Creates an empty YAML map.
18 #[inline]
19 pub fn new() -> Self {
20 Self::default()
21 }
22
23 /// Creates an empty YAML map with the given initial capacity.
24 #[inline]
25 pub fn with_capacity(capacity: usize) -> Self {
26 Mapping {
27 map: LinkedHashMap::with_capacity(capacity),
28 }
29 }
30
31 /// Reserves capacity for at least `additional` more elements to be inserted
32 /// into the map. The map may reserve more space to avoid frequent
33 /// allocations.
34 ///
35 /// # Panics
36 ///
37 /// Panics if the new allocation size overflows `usize`.
38 #[inline]
39 pub fn reserve(&mut self, additional: usize) {
40 self.map.reserve(additional)
41 }
42
43 /// Shrinks the capacity of the map as much as possible. It will drop down
44 /// as much as possible while maintaining the internal rules and possibly
45 /// leaving some space in accordance with the resize policy.
46 #[inline]
47 pub fn shrink_to_fit(&mut self) {
48 self.map.shrink_to_fit()
49 }
50
51 /// Inserts a key-value pair into the map. If the key already existed, the
52 /// old value is returned.
53 #[inline]
54 pub fn insert(&mut self, k: Value, v: Value) -> Option<Value> {
55 self.map.insert(k, v)
56 }
57
58 /// Checks if the map contains the given key.
59 #[inline]
60 pub fn contains_key(&self, k: &Value) -> bool {
61 self.map.contains_key(k)
62 }
63
64 /// Returns the value corresponding to the key in the map.
65 #[inline]
66 pub fn get(&self, k: &Value) -> Option<&Value> {
67 self.map.get(k)
68 }
69
70 /// Returns the mutable reference corresponding to the key in the map.
71 #[inline]
72 pub fn get_mut(&mut self, k: &Value) -> Option<&mut Value> {
73 self.map.get_mut(k)
74 }
75
76 /// Removes and returns the value corresponding to the key from the map.
77 #[inline]
78 pub fn remove(&mut self, k: &Value) -> Option<Value> {
79 self.map.remove(k)
80 }
81
82 /// Returns the maximum number of key-value pairs the map can hold without
83 /// reallocating.
84 #[inline]
85 pub fn capacity(&self) -> usize {
86 self.map.capacity()
87 }
88
89 /// Returns the number of key-value pairs in the map.
90 #[inline]
91 pub fn len(&self) -> usize {
92 self.map.len()
93 }
94
95 /// Returns whether the map is currently empty.
96 #[inline]
97 pub fn is_empty(&self) -> bool {
98 self.map.is_empty()
99 }
100
101 /// Clears the map of all key-value pairs.
102 #[inline]
103 pub fn clear(&mut self) {
104 self.map.clear()
105 }
106
107 /// Returns a double-ended iterator visiting all key-value pairs in order of
108 /// insertion. Iterator element type is `(&'a Value, &'a Value)`.
109 #[inline]
110 pub fn iter(&self) -> Iter {
111 Iter {
112 iter: self.map.iter(),
113 }
114 }
115
116 /// Returns a double-ended iterator visiting all key-value pairs in order of
117 /// insertion. Iterator element type is `(&'a Value, &'a mut ValuE)`.
118 #[inline]
119 pub fn iter_mut(&mut self) -> IterMut {
120 IterMut {
121 iter: self.map.iter_mut(),
122 }
123 }
124 }
125
126 impl<'a> Index<&'a Value> for Mapping {
127 type Output = Value;
128 #[inline]
129 fn index(&self, index: &'a Value) -> &Value {
130 self.map.index(index)
131 }
132 }
133
134 impl<'a> IndexMut<&'a Value> for Mapping {
135 #[inline]
136 fn index_mut(&mut self, index: &'a Value) -> &mut Value {
137 self.map.index_mut(index)
138 }
139 }
140
141 impl Extend<(Value, Value)> for Mapping {
142 #[inline]
143 fn extend<I: IntoIterator<Item = (Value, Value)>>(&mut self, iter: I) {
144 self.map.extend(iter);
145 }
146 }
147
148 impl FromIterator<(Value, Value)> for Mapping {
149 #[inline]
150 fn from_iter<I: IntoIterator<Item = (Value, Value)>>(iter: I) -> Self {
151 Mapping {
152 map: LinkedHashMap::from_iter(iter),
153 }
154 }
155 }
156
157 macro_rules! delegate_iterator {
158 (($name:ident $($generics:tt)*) => $item:ty) => {
159 impl $($generics)* Iterator for $name $($generics)* {
160 type Item = $item;
161 #[inline]
162 fn next(&mut self) -> Option<Self::Item> {
163 self.iter.next()
164 }
165 #[inline]
166 fn size_hint(&self) -> (usize, Option<usize>) {
167 self.iter.size_hint()
168 }
169 }
170
171 impl $($generics)* ExactSizeIterator for $name $($generics)* {
172 #[inline]
173 fn len(&self) -> usize {
174 self.iter.len()
175 }
176 }
177 }
178 }
179
180 /// Iterator over `&serde_yaml::Mapping`.
181 pub struct Iter<'a> {
182 iter: linked_hash_map::Iter<'a, Value, Value>,
183 }
184
185 delegate_iterator!((Iter<'a>) => (&'a Value, &'a Value));
186
187 impl<'a> IntoIterator for &'a Mapping {
188 type Item = (&'a Value, &'a Value);
189 type IntoIter = Iter<'a>;
190 #[inline]
191 fn into_iter(self) -> Self::IntoIter {
192 Iter {
193 iter: self.map.iter(),
194 }
195 }
196 }
197
198 /// Iterator over `&mut serde_yaml::Mapping`.
199 pub struct IterMut<'a> {
200 iter: linked_hash_map::IterMut<'a, Value, Value>,
201 }
202
203 delegate_iterator!((IterMut<'a>) => (&'a Value, &'a mut Value));
204
205 impl<'a> IntoIterator for &'a mut Mapping {
206 type Item = (&'a Value, &'a mut Value);
207 type IntoIter = IterMut<'a>;
208 #[inline]
209 fn into_iter(self) -> Self::IntoIter {
210 IterMut {
211 iter: self.map.iter_mut(),
212 }
213 }
214 }
215
216 /// Iterator over `serde_yaml::Mapping` by value.
217 pub struct IntoIter {
218 iter: linked_hash_map::IntoIter<Value, Value>,
219 }
220
221 delegate_iterator!((IntoIter) => (Value, Value));
222
223 impl IntoIterator for Mapping {
224 type Item = (Value, Value);
225 type IntoIter = IntoIter;
226 #[inline]
227 fn into_iter(self) -> Self::IntoIter {
228 IntoIter {
229 iter: self.map.into_iter(),
230 }
231 }
232 }
233
234 impl Serialize for Mapping {
235 #[inline]
236 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
237 use serde::ser::SerializeMap;
238 let mut map_serializer = serializer.serialize_map(Some(self.len()))?;
239 for (k, v) in self {
240 map_serializer.serialize_entry(k, v)?;
241 }
242 map_serializer.end()
243 }
244 }
245
246 impl<'de> Deserialize<'de> for Mapping {
247 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
248 where
249 D: Deserializer<'de>,
250 {
251 struct Visitor;
252
253 impl<'de> serde::de::Visitor<'de> for Visitor {
254 type Value = Mapping;
255
256 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
257 formatter.write_str("a YAML mapping")
258 }
259
260 #[inline]
261 fn visit_unit<E>(self) -> Result<Self::Value, E>
262 where
263 E: serde::de::Error,
264 {
265 Ok(Mapping::new())
266 }
267
268 #[inline]
269 fn visit_map<V>(self, mut visitor: V) -> Result<Self::Value, V::Error>
270 where
271 V: serde::de::MapAccess<'de>,
272 {
273 let mut values = Mapping::new();
274 while let Some((k, v)) = visitor.next_entry()? {
275 values.insert(k, v);
276 }
277 Ok(values)
278 }
279 }
280
281 deserializer.deserialize_map(Visitor)
282 }
283 }