Skip to main content

deser_value/
seq.rs

1use std::fmt;
2use std::hash::{Hash, Hasher};
3use std::ops::{Deref, DerefMut};
4
5use deser_core::Order;
6
7use crate::tree;
8use crate::value::{Kind, Value};
9
10/// A sequence of values.
11///
12/// A sequence dereferences to a [`Vec`] of its values.  Additionally it
13/// holds the [`Order`] of the values, which is passed on when the sequence
14/// is serialized, and if it holds the values of a repeated key of a
15/// multimap (see [`Map::is_multimap`](crate::Map::is_multimap)).  They are
16/// not considered when sequences are compared.
17///
18/// ```
19/// use deser::Order;
20/// use deser_value::{Seq, Value};
21///
22/// let mut seq = Seq::new().with_order(Order::Sorted);
23/// seq.push(Value::from(1));
24/// seq.push(Value::from(2));
25/// assert_eq!(seq.len(), 2);
26/// assert_eq!(seq.order(), Order::Sorted);
27/// ```
28#[derive(Default)]
29pub struct Seq {
30    pub(crate) items: Vec<Value>,
31    pub(crate) order: Order,
32    pub(crate) repeated: bool,
33}
34
35impl Seq {
36    /// Creates an empty sequence.
37    pub const fn new() -> Seq {
38        Seq {
39            items: Vec::new(),
40            order: Order::Natural,
41            repeated: false,
42        }
43    }
44
45    /// Creates an empty sequence with a capacity.
46    pub fn with_capacity(capacity: usize) -> Seq {
47        Seq {
48            items: Vec::with_capacity(capacity),
49            order: Order::Natural,
50            repeated: false,
51        }
52    }
53
54    /// Returns the order of the values.
55    pub fn order(&self) -> Order {
56        self.order
57    }
58
59    /// Sets the order of the values.
60    pub fn set_order(&mut self, order: Order) {
61        self.order = order;
62    }
63
64    /// Sets the order of the values and returns the sequence.
65    pub fn with_order(mut self, order: Order) -> Seq {
66        self.order = order;
67        self
68    }
69
70    /// Returns `true` if the sequence holds the values of a repeated key.
71    ///
72    /// Within a multimap (see [`Map::is_multimap`](crate::Map::is_multimap))
73    /// the values are passed on as the values of a key that is given more
74    /// than once.  Elsewhere this has no effect.
75    pub fn is_repeated(&self) -> bool {
76        self.repeated
77    }
78
79    /// Sets if the sequence holds the values of a repeated key.
80    pub fn set_repeated(&mut self, yes: bool) {
81        self.repeated = yes;
82    }
83
84    /// Sets if the sequence holds the values of a repeated key and returns
85    /// the sequence.
86    pub fn with_repeated(mut self, yes: bool) -> Seq {
87        self.repeated = yes;
88        self
89    }
90
91    /// Creates an empty sequence with the order and the repeated flag of
92    /// this one.
93    pub(crate) fn empty_like(&self, capacity: usize) -> Seq {
94        Seq::with_capacity(capacity)
95            .with_order(self.order)
96            .with_repeated(self.repeated)
97    }
98
99    /// Converts the sequence into a vector.
100    pub fn into_vec(mut self) -> Vec<Value> {
101        std::mem::take(&mut self.items)
102    }
103}
104
105impl Deref for Seq {
106    type Target = Vec<Value>;
107
108    fn deref(&self) -> &Vec<Value> {
109        &self.items
110    }
111}
112
113impl DerefMut for Seq {
114    fn deref_mut(&mut self) -> &mut Vec<Value> {
115        &mut self.items
116    }
117}
118
119impl Drop for Seq {
120    fn drop(&mut self) {
121        if self.items.iter().any(|item| item.has_children()) {
122            tree::drop_values(std::mem::take(&mut self.items));
123        }
124    }
125}
126
127impl Clone for Seq {
128    fn clone(&self) -> Seq {
129        match tree::clone_seq(self) {
130            Kind::Seq(seq) => seq,
131            _ => unreachable!(),
132        }
133    }
134}
135
136impl PartialEq for Seq {
137    fn eq(&self, other: &Seq) -> bool {
138        tree::eq_seq(self, other)
139    }
140}
141
142impl Eq for Seq {}
143
144impl Hash for Seq {
145    fn hash<H: Hasher>(&self, state: &mut H) {
146        tree::hash_seq(self, state);
147    }
148}
149
150impl fmt::Debug for Seq {
151    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
152        tree::fmt_seq(self, f)
153    }
154}
155
156impl From<Vec<Value>> for Seq {
157    fn from(items: Vec<Value>) -> Seq {
158        Seq {
159            items,
160            order: Order::Natural,
161            repeated: false,
162        }
163    }
164}
165
166impl From<Seq> for Vec<Value> {
167    fn from(seq: Seq) -> Vec<Value> {
168        seq.into_vec()
169    }
170}
171
172impl<T: Into<Value>> FromIterator<T> for Seq {
173    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Seq {
174        Seq::from(iter.into_iter().map(Into::into).collect::<Vec<_>>())
175    }
176}
177
178impl<T: Into<Value>> Extend<T> for Seq {
179    fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
180        self.items.extend(iter.into_iter().map(Into::into));
181    }
182}
183
184impl IntoIterator for Seq {
185    type Item = Value;
186    type IntoIter = std::vec::IntoIter<Value>;
187
188    fn into_iter(self) -> Self::IntoIter {
189        self.into_vec().into_iter()
190    }
191}
192
193impl<'a> IntoIterator for &'a Seq {
194    type Item = &'a Value;
195    type IntoIter = std::slice::Iter<'a, Value>;
196
197    fn into_iter(self) -> Self::IntoIter {
198        self.items.iter()
199    }
200}
201
202impl<'a> IntoIterator for &'a mut Seq {
203    type Item = &'a mut Value;
204    type IntoIter = std::slice::IterMut<'a, Value>;
205
206    fn into_iter(self) -> Self::IntoIter {
207        self.items.iter_mut()
208    }
209}