1 //! A YAML mapping and its iterator types.
4 use linked_hash_map
::LinkedHashMap
;
5 use serde
::{Deserialize, Deserializer, Serialize}
;
7 use std
::iter
::FromIterator
;
8 use std
::ops
::{Index, IndexMut}
;
10 /// A YAML mapping in which the keys and values are both `serde_yaml::Value`.
11 #[derive(Clone, Debug, Default, Eq, Hash, PartialEq, PartialOrd)]
13 map
: LinkedHashMap
<Value
, Value
>,
17 /// Creates an empty YAML map.
19 pub fn new() -> Self {
23 /// Creates an empty YAML map with the given initial capacity.
25 pub fn with_capacity(capacity
: usize) -> Self {
27 map
: LinkedHashMap
::with_capacity(capacity
),
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
37 /// Panics if the new allocation size overflows `usize`.
39 pub fn reserve(&mut self, additional
: usize) {
40 self.map
.reserve(additional
)
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.
47 pub fn shrink_to_fit(&mut self) {
48 self.map
.shrink_to_fit()
51 /// Inserts a key-value pair into the map. If the key already existed, the
52 /// old value is returned.
54 pub fn insert(&mut self, k
: Value
, v
: Value
) -> Option
<Value
> {
58 /// Checks if the map contains the given key.
60 pub fn contains_key(&self, k
: &Value
) -> bool
{
61 self.map
.contains_key(k
)
64 /// Returns the value corresponding to the key in the map.
66 pub fn get(&self, k
: &Value
) -> Option
<&Value
> {
70 /// Returns the mutable reference corresponding to the key in the map.
72 pub fn get_mut(&mut self, k
: &Value
) -> Option
<&mut Value
> {
76 /// Removes and returns the value corresponding to the key from the map.
78 pub fn remove(&mut self, k
: &Value
) -> Option
<Value
> {
82 /// Returns the maximum number of key-value pairs the map can hold without
85 pub fn capacity(&self) -> usize {
89 /// Returns the number of key-value pairs in the map.
91 pub fn len(&self) -> usize {
95 /// Returns whether the map is currently empty.
97 pub fn is_empty(&self) -> bool
{
101 /// Clears the map of all key-value pairs.
103 pub fn clear(&mut self) {
107 /// Returns a double-ended iterator visiting all key-value pairs in order of
108 /// insertion. Iterator element type is `(&'a Value, &'a Value)`.
110 pub fn iter(&self) -> Iter
{
112 iter
: self.map
.iter(),
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)`.
119 pub fn iter_mut(&mut self) -> IterMut
{
121 iter
: self.map
.iter_mut(),
126 impl<'a
> Index
<&'a Value
> for Mapping
{
129 fn index(&self, index
: &'a Value
) -> &Value
{
130 self.map
.index(index
)
134 impl<'a
> IndexMut
<&'a Value
> for Mapping
{
136 fn index_mut(&mut self, index
: &'a Value
) -> &mut Value
{
137 self.map
.index_mut(index
)
141 impl Extend
<(Value
, Value
)> for Mapping
{
143 fn extend
<I
: IntoIterator
<Item
= (Value
, Value
)>>(&mut self, iter
: I
) {
144 self.map
.extend(iter
);
148 impl FromIterator
<(Value
, Value
)> for Mapping
{
150 fn from_iter
<I
: IntoIterator
<Item
= (Value
, Value
)>>(iter
: I
) -> Self {
152 map
: LinkedHashMap
::from_iter(iter
),
157 macro_rules
! delegate_iterator
{
158 (($name
:ident $
($generics
:tt
)*) => $item
:ty
) => {
159 impl $
($generics
)* Iterator
for $name $
($generics
)* {
162 fn next(&mut self) -> Option
<Self::Item
> {
166 fn size_hint(&self) -> (usize, Option
<usize>) {
167 self.iter
.size_hint()
171 impl $
($generics
)* ExactSizeIterator
for $name $
($generics
)* {
173 fn len(&self) -> usize {
180 /// Iterator over `&serde_yaml::Mapping`.
181 pub struct Iter
<'a
> {
182 iter
: linked_hash_map
::Iter
<'a
, Value
, Value
>,
185 delegate_iterator
!((Iter
<'a
>) => (&'a Value
, &'a Value
));
187 impl<'a
> IntoIterator
for &'a Mapping
{
188 type Item
= (&'a Value
, &'a Value
);
189 type IntoIter
= Iter
<'a
>;
191 fn into_iter(self) -> Self::IntoIter
{
193 iter
: self.map
.iter(),
198 /// Iterator over `&mut serde_yaml::Mapping`.
199 pub struct IterMut
<'a
> {
200 iter
: linked_hash_map
::IterMut
<'a
, Value
, Value
>,
203 delegate_iterator
!((IterMut
<'a
>) => (&'a Value
, &'a
mut Value
));
205 impl<'a
> IntoIterator
for &'a
mut Mapping
{
206 type Item
= (&'a Value
, &'a
mut Value
);
207 type IntoIter
= IterMut
<'a
>;
209 fn into_iter(self) -> Self::IntoIter
{
211 iter
: self.map
.iter_mut(),
216 /// Iterator over `serde_yaml::Mapping` by value.
217 pub struct IntoIter
{
218 iter
: linked_hash_map
::IntoIter
<Value
, Value
>,
221 delegate_iterator
!((IntoIter
) => (Value
, Value
));
223 impl IntoIterator
for Mapping
{
224 type Item
= (Value
, Value
);
225 type IntoIter
= IntoIter
;
227 fn into_iter(self) -> Self::IntoIter
{
229 iter
: self.map
.into_iter(),
234 impl Serialize
for Mapping
{
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()))?
;
240 map_serializer
.serialize_entry(k
, v
)?
;
246 impl<'de
> Deserialize
<'de
> for Mapping
{
247 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
249 D
: Deserializer
<'de
>,
253 impl<'de
> serde
::de
::Visitor
<'de
> for Visitor
{
254 type Value
= Mapping
;
256 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
257 formatter
.write_str("a YAML mapping")
261 fn visit_unit
<E
>(self) -> Result
<Self::Value
, E
>
269 fn visit_map
<V
>(self, mut visitor
: V
) -> Result
<Self::Value
, V
::Error
>
271 V
: serde
::de
::MapAccess
<'de
>,
273 let mut values
= Mapping
::new();
274 while let Some((k
, v
)) = visitor
.next_entry()?
{
281 deserializer
.deserialize_map(Visitor
)