toml_edit/array.rs
1use std::iter::FromIterator;
2use std::mem;
3
4use crate::repr::Decor;
5use crate::value::{DEFAULT_LEADING_VALUE_DECOR, DEFAULT_VALUE_DECOR};
6use crate::{Item, RawString, Value};
7
8/// A TOML [`Value`] that contains a sequence of [`Value`]s
9#[derive(Debug, Default, Clone)]
10pub struct Array {
11 // `trailing` represents whitespaces, newlines
12 // and comments in an empty array or after the trailing comma
13 trailing: RawString,
14 trailing_comma: bool,
15 // prefix before `[` and suffix after `]`
16 decor: Decor,
17 pub(crate) span: Option<std::ops::Range<usize>>,
18 // always Vec<Item::Value>
19 pub(crate) values: Vec<Item>,
20}
21
22/// An owned iterator type over [`Array`]'s [`Value`]s
23pub type ArrayIntoIter = Box<dyn Iterator<Item = Value>>;
24/// An iterator type over [`Array`]'s [`Value`]s
25pub type ArrayIter<'a> = Box<dyn Iterator<Item = &'a Value> + 'a>;
26/// An iterator type over [`Array`]'s [`Value`]s
27pub type ArrayIterMut<'a> = Box<dyn Iterator<Item = &'a mut Value> + 'a>;
28
29/// Constructors
30///
31/// See also `FromIterator`
32impl Array {
33 /// Create an empty `Array`
34 ///
35 /// # Examples
36 ///
37 /// ```rust
38 /// let mut arr = toml_edit::Array::new();
39 /// ```
40 pub fn new() -> Self {
41 Default::default()
42 }
43
44 pub(crate) fn with_vec(values: Vec<Item>) -> Self {
45 Self {
46 values,
47 ..Default::default()
48 }
49 }
50}
51
52/// Formatting
53impl Array {
54 /// Auto formats the array.
55 pub fn fmt(&mut self) {
56 decorate_array(self);
57 }
58
59 /// Set whether the array will use a trailing comma
60 pub fn set_trailing_comma(&mut self, yes: bool) {
61 self.trailing_comma = yes;
62 }
63
64 /// Whether the array will use a trailing comma
65 pub fn trailing_comma(&self) -> bool {
66 self.trailing_comma
67 }
68
69 /// Set whitespace after last element
70 pub fn set_trailing(&mut self, trailing: impl Into<RawString>) {
71 self.trailing = trailing.into();
72 }
73
74 /// Whitespace after last element
75 pub fn trailing(&self) -> &RawString {
76 &self.trailing
77 }
78
79 /// Returns the surrounding whitespace
80 pub fn decor_mut(&mut self) -> &mut Decor {
81 &mut self.decor
82 }
83
84 /// Returns the surrounding whitespace
85 pub fn decor(&self) -> &Decor {
86 &self.decor
87 }
88
89 /// The location within the original document
90 ///
91 /// This generally requires a [`Document`][crate::Document].
92 pub fn span(&self) -> Option<std::ops::Range<usize>> {
93 self.span.clone()
94 }
95
96 pub(crate) fn despan(&mut self, input: &str) {
97 self.span = None;
98 self.decor.despan(input);
99 self.trailing.despan(input);
100 for value in &mut self.values {
101 value.despan(input);
102 }
103 }
104}
105
106impl Array {
107 /// Returns an iterator over all values.
108 pub fn iter(&self) -> ArrayIter<'_> {
109 Box::new(self.values.iter().filter_map(Item::as_value))
110 }
111
112 /// Returns an iterator over all values.
113 pub fn iter_mut(&mut self) -> ArrayIterMut<'_> {
114 Box::new(self.values.iter_mut().filter_map(Item::as_value_mut))
115 }
116
117 /// Returns the length of the underlying Vec.
118 ///
119 /// In some rare cases, placeholder elements will exist. For a more accurate count, call
120 /// `a.iter().count()`
121 ///
122 /// # Examples
123 ///
124 /// ```rust
125 /// let mut arr = toml_edit::Array::new();
126 /// arr.push(1);
127 /// arr.push("foo");
128 /// assert_eq!(arr.len(), 2);
129 /// ```
130 pub fn len(&self) -> usize {
131 self.values.len()
132 }
133
134 /// Return true if `self.len() == 0`.
135 ///
136 /// # Examples
137 ///
138 /// ```rust
139 /// let mut arr = toml_edit::Array::new();
140 /// assert!(arr.is_empty());
141 ///
142 /// arr.push(1);
143 /// arr.push("foo");
144 /// assert!(! arr.is_empty());
145 /// ```
146 pub fn is_empty(&self) -> bool {
147 self.len() == 0
148 }
149
150 /// Clears the array, removing all values. Keeps the allocated memory for reuse.
151 pub fn clear(&mut self) {
152 self.values.clear();
153 }
154
155 /// Returns a reference to the value at the given index, or `None` if the index is out of
156 /// bounds.
157 pub fn get(&self, index: usize) -> Option<&Value> {
158 self.values.get(index).and_then(Item::as_value)
159 }
160
161 /// Returns a reference to the value at the given index, or `None` if the index is out of
162 /// bounds.
163 pub fn get_mut(&mut self, index: usize) -> Option<&mut Value> {
164 self.values.get_mut(index).and_then(Item::as_value_mut)
165 }
166
167 /// Appends a new value to the end of the array, applying default formatting to it.
168 ///
169 /// # Examples
170 ///
171 /// ```rust
172 /// let mut arr = toml_edit::Array::new();
173 /// arr.push(1);
174 /// arr.push("foo");
175 /// ```
176 pub fn push<V: Into<Value>>(&mut self, v: V) {
177 self.values.push(Item::Value(v.into()));
178 }
179
180 /// Appends a new, already formatted value to the end of the array.
181 ///
182 /// # Examples
183 ///
184 /// ```rust
185 /// # #[cfg(feature = "parse")] {
186 /// let formatted_value = "'literal'".parse::<toml_edit::Value>().unwrap();
187 /// let mut arr = toml_edit::Array::new();
188 /// arr.push_formatted(formatted_value);
189 /// # }
190 /// ```
191 pub fn push_formatted(&mut self, v: Value) {
192 self.values.push(Item::Value(v));
193 }
194
195 /// Inserts an element at the given position within the array, applying default formatting to
196 /// it and shifting all values after it to the right.
197 ///
198 /// # Panics
199 ///
200 /// Panics if `index > len`.
201 ///
202 /// # Examples
203 ///
204 /// ```rust
205 /// let mut arr = toml_edit::Array::new();
206 /// arr.push(1);
207 /// arr.push("foo");
208 ///
209 /// arr.insert(0, "start");
210 /// ```
211 pub fn insert<V: Into<Value>>(&mut self, index: usize, v: V) {
212 self.values.insert(index, Item::Value(v.into()));
213 }
214
215 /// Inserts an already formatted value at the given position within the array, shifting all
216 /// values after it to the right.
217 ///
218 /// # Panics
219 ///
220 /// Panics if `index > len`.
221 ///
222 /// # Examples
223 ///
224 /// ```rust
225 /// # #[cfg(feature = "parse")] {
226 /// let mut arr = toml_edit::Array::new();
227 /// arr.push(1);
228 /// arr.push("foo");
229 ///
230 /// let formatted_value = "'start'".parse::<toml_edit::Value>().unwrap();
231 /// arr.insert_formatted(0, formatted_value);
232 /// # }
233 /// ```
234 pub fn insert_formatted(&mut self, index: usize, v: Value) {
235 self.values.insert(index, Item::Value(v));
236 }
237
238 /// Replaces the element at the given position within the array, preserving existing formatting.
239 ///
240 /// # Panics
241 ///
242 /// Panics if `index >= len`.
243 ///
244 /// # Examples
245 ///
246 /// ```rust
247 /// let mut arr = toml_edit::Array::new();
248 /// arr.push(1);
249 /// arr.push("foo");
250 ///
251 /// arr.replace(0, "start");
252 /// ```
253 pub fn replace<V: Into<Value>>(&mut self, index: usize, v: V) -> Value {
254 // Read the existing value's decor and preserve it.
255 let existing_decor = self
256 .get(index)
257 .unwrap_or_else(|| panic!("index {} out of bounds (len = {})", index, self.len()))
258 .decor();
259 let mut value = v.into();
260 *value.decor_mut() = existing_decor.clone();
261 self.replace_formatted(index, value)
262 }
263
264 /// Replaces the element at the given position within the array with an already formatted value.
265 ///
266 /// # Panics
267 ///
268 /// Panics if `index >= len`.
269 ///
270 /// # Examples
271 ///
272 /// ```rust
273 /// # #[cfg(feature = "parse")] {
274 /// let mut arr = toml_edit::Array::new();
275 /// arr.push(1);
276 /// arr.push("foo");
277 ///
278 /// let formatted_value = "'start'".parse::<toml_edit::Value>().unwrap();
279 /// arr.replace_formatted(0, formatted_value);
280 /// # }
281 /// ```
282 pub fn replace_formatted(&mut self, index: usize, v: Value) -> Value {
283 match mem::replace(&mut self.values[index], Item::Value(v)) {
284 Item::Value(old_value) => old_value,
285 x => panic!("non-value item {x:?} in an array"),
286 }
287 }
288
289 /// Removes the value at the given index.
290 ///
291 /// # Examples
292 ///
293 /// ```rust
294 /// let mut arr = toml_edit::Array::new();
295 /// arr.push(1);
296 /// arr.push("foo");
297 ///
298 /// arr.remove(0);
299 /// assert_eq!(arr.len(), 1);
300 /// ```
301 pub fn remove(&mut self, index: usize) -> Value {
302 let removed = self.values.remove(index);
303 match removed {
304 Item::Value(v) => v,
305 x => panic!("non-value item {x:?} in an array"),
306 }
307 }
308
309 /// Retains only the values specified by the `keep` predicate.
310 ///
311 /// In other words, remove all values for which `keep(&value)` returns `false`.
312 ///
313 /// This method operates in place, visiting each element exactly once in the
314 /// original order, and preserves the order of the retained elements.
315 pub fn retain<F>(&mut self, mut keep: F)
316 where
317 F: FnMut(&Value) -> bool,
318 {
319 self.values
320 .retain(|item| item.as_value().map(&mut keep).unwrap_or(false));
321 }
322
323 /// Sorts the slice with a comparator function.
324 ///
325 /// This sort is stable (i.e., does not reorder equal elements) and *O*(*n* \* log(*n*)) worst-case.
326 ///
327 /// The comparator function must define a total ordering for the elements in the slice. If
328 /// the ordering is not total, the order of the elements is unspecified. An order is a
329 /// total order if it is (for all `a`, `b` and `c`):
330 ///
331 /// * total and antisymmetric: exactly one of `a < b`, `a == b` or `a > b` is true, and
332 /// * transitive, `a < b` and `b < c` implies `a < c`. The same must hold for both `==` and `>`.
333 ///
334 /// For example, while [`f64`] doesn't implement [`Ord`] because `NaN != NaN`, we can use
335 /// `partial_cmp` as our sort function when we know the slice doesn't contain a `NaN`.
336 #[inline]
337 pub fn sort_by<F>(&mut self, mut compare: F)
338 where
339 F: FnMut(&Value, &Value) -> std::cmp::Ordering,
340 {
341 self.values.sort_by(move |lhs, rhs| {
342 let lhs = lhs.as_value();
343 let rhs = rhs.as_value();
344 match (lhs, rhs) {
345 (None, None) => std::cmp::Ordering::Equal,
346 (Some(_), None) => std::cmp::Ordering::Greater,
347 (None, Some(_)) => std::cmp::Ordering::Less,
348 (Some(lhs), Some(rhs)) => compare(lhs, rhs),
349 }
350 });
351 }
352
353 /// Sorts the array with a key extraction function.
354 ///
355 /// This sort is stable (i.e., does not reorder equal elements) and *O*(*m* \* *n* \* log(*n*))
356 /// worst-case, where the key function is *O*(*m*).
357 #[inline]
358 pub fn sort_by_key<K, F>(&mut self, mut f: F)
359 where
360 F: FnMut(&Value) -> K,
361 K: Ord,
362 {
363 #[allow(clippy::manual_map)] // needed for lifetimes
364 self.values.sort_by_key(move |item| {
365 if let Some(value) = item.as_value() {
366 Some(f(value))
367 } else {
368 None
369 }
370 });
371 }
372}
373
374#[cfg(feature = "display")]
375impl std::fmt::Display for Array {
376 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
377 crate::encode::encode_array(self, f, None, ("", ""))
378 }
379}
380
381impl<V: Into<Value>> Extend<V> for Array {
382 fn extend<T: IntoIterator<Item = V>>(&mut self, iter: T) {
383 for value in iter {
384 self.push_formatted(value.into());
385 }
386 }
387}
388
389impl<V: Into<Value>> FromIterator<V> for Array {
390 fn from_iter<I>(iter: I) -> Self
391 where
392 I: IntoIterator<Item = V>,
393 {
394 let v = iter.into_iter().map(|a| Item::Value(a.into()));
395 Self {
396 values: v.collect(),
397 ..Default::default()
398 }
399 }
400}
401
402impl IntoIterator for Array {
403 type Item = Value;
404 type IntoIter = ArrayIntoIter;
405
406 fn into_iter(self) -> Self::IntoIter {
407 Box::new(
408 self.values
409 .into_iter()
410 .filter(|v| v.is_value())
411 .map(|v| v.into_value().unwrap()),
412 )
413 }
414}
415
416impl<'s> IntoIterator for &'s Array {
417 type Item = &'s Value;
418 type IntoIter = ArrayIter<'s>;
419
420 fn into_iter(self) -> Self::IntoIter {
421 self.iter()
422 }
423}
424
425fn decorate_array(array: &mut Array) {
426 for (i, value) in array
427 .values
428 .iter_mut()
429 .filter_map(Item::as_value_mut)
430 .enumerate()
431 {
432 // [value1, value2, value3]
433 if i == 0 {
434 value.decorate(DEFAULT_LEADING_VALUE_DECOR.0, DEFAULT_LEADING_VALUE_DECOR.1);
435 } else {
436 value.decorate(DEFAULT_VALUE_DECOR.0, DEFAULT_VALUE_DECOR.1);
437 }
438 }
439 // Since everything is now on the same line, remove trailing commas and whitespace.
440 array.set_trailing_comma(false);
441 array.set_trailing("");
442}