Skip to main content

toml_edit/
array_of_tables.rs

1use std::iter::FromIterator;
2
3use crate::{Array, Item, Table};
4
5/// A top-level sequence of [`Table`]s, each under their own header
6#[derive(Clone, Debug, Default)]
7pub struct ArrayOfTables {
8    // Always Vec<Item::Table>, just `Item` to make `Index` work
9    pub(crate) span: Option<std::ops::Range<usize>>,
10    pub(crate) values: Vec<Item>,
11}
12
13/// Constructors
14///
15/// See also `FromIterator`
16impl ArrayOfTables {
17    /// Creates an empty array of tables.
18    pub fn new() -> Self {
19        Default::default()
20    }
21}
22
23/// Formatting
24impl ArrayOfTables {
25    /// Convert to an inline array
26    pub fn into_array(mut self) -> Array {
27        for value in self.values.iter_mut() {
28            value.make_value();
29        }
30        let mut a = Array::with_vec(self.values);
31        a.fmt();
32        a
33    }
34
35    /// The location within the original document
36    ///
37    /// This generally requires a [`Document`][crate::Document].
38    pub fn span(&self) -> Option<std::ops::Range<usize>> {
39        self.span.clone()
40    }
41
42    pub(crate) fn despan(&mut self, input: &str) {
43        self.span = None;
44        for value in &mut self.values {
45            value.despan(input);
46        }
47    }
48}
49
50impl ArrayOfTables {
51    /// Returns an iterator over tables.
52    pub fn iter(&self) -> ArrayOfTablesIter<'_> {
53        Box::new(self.values.iter().filter_map(Item::as_table))
54    }
55
56    /// Returns an iterator over tables.
57    pub fn iter_mut(&mut self) -> ArrayOfTablesIterMut<'_> {
58        Box::new(self.values.iter_mut().filter_map(Item::as_table_mut))
59    }
60
61    /// Returns the length of the underlying Vec.
62    /// To get the actual number of items use `a.iter().count()`.
63    pub fn len(&self) -> usize {
64        self.values.len()
65    }
66
67    /// Returns true if `self.len() == 0`.
68    pub fn is_empty(&self) -> bool {
69        self.len() == 0
70    }
71
72    /// Removes all the tables.
73    pub fn clear(&mut self) {
74        self.values.clear();
75    }
76
77    /// Returns an optional reference to the table.
78    pub fn get(&self, index: usize) -> Option<&Table> {
79        self.values.get(index).and_then(Item::as_table)
80    }
81
82    /// Returns an optional mutable reference to the table.
83    pub fn get_mut(&mut self, index: usize) -> Option<&mut Table> {
84        self.values.get_mut(index).and_then(Item::as_table_mut)
85    }
86
87    /// Appends a table to the array.
88    pub fn push(&mut self, table: Table) {
89        self.values.push(Item::Table(table));
90    }
91
92    /// Inserts a table at the given index within the array, shifting all
93    /// tables after it to the right.
94    ///
95    /// # Panics
96    ///
97    /// Panics if `index > len`.
98    pub fn insert(&mut self, index: usize, table: Table) {
99        self.values.insert(index, Item::Table(table));
100    }
101
102    /// Replaces a table at the given index within the array, returning the old table.
103    ///
104    /// # Panics
105    ///
106    /// Panics if `index >= len`.
107    ///
108    /// # Examples
109    ///
110    /// ```rust
111    /// use toml_edit::{ArrayOfTables, Table, value};
112    ///
113    /// let mut arr = ArrayOfTables::new();
114    /// arr.push(Table::from_iter([("name", value("apple"))]));
115    ///
116    /// arr.replace(0, Table::from_iter([("name", value("banana"))]));
117    /// ```
118    pub fn replace(&mut self, index: usize, table: Table) -> Table {
119        match std::mem::replace(&mut self.values[index], Item::Table(table)) {
120            Item::Table(old) => old,
121            x => panic!("non-table item {x:?} in an array of tables"),
122        }
123    }
124
125    /// Removes a table with the given index.
126    pub fn remove(&mut self, index: usize) -> Table {
127        self.values
128            .remove(index)
129            .into_table()
130            .expect("cannot have any other item in an array-of-tables")
131    }
132
133    /// Retains only the elements specified by the `keep` predicate.
134    ///
135    /// In other words, remove all tables for which `keep(&table)` returns `false`.
136    ///
137    /// This method operates in place, visiting each element exactly once in the
138    /// original order, and preserves the order of the retained elements.
139    pub fn retain<F>(&mut self, mut keep: F)
140    where
141        F: FnMut(&Table) -> bool,
142    {
143        self.values
144            .retain(|item| item.as_table().map(&mut keep).unwrap_or(false));
145    }
146}
147
148/// An iterator type over [`ArrayOfTables`]'s [`Table`]s
149pub type ArrayOfTablesIter<'a> = Box<dyn Iterator<Item = &'a Table> + 'a>;
150/// An iterator type over [`ArrayOfTables`]'s [`Table`]s
151pub type ArrayOfTablesIterMut<'a> = Box<dyn Iterator<Item = &'a mut Table> + 'a>;
152/// An iterator type over [`ArrayOfTables`]'s [`Table`]s
153pub type ArrayOfTablesIntoIter = Box<dyn Iterator<Item = Table>>;
154
155impl Extend<Table> for ArrayOfTables {
156    fn extend<T: IntoIterator<Item = Table>>(&mut self, iter: T) {
157        for value in iter {
158            self.push(value);
159        }
160    }
161}
162
163impl FromIterator<Table> for ArrayOfTables {
164    fn from_iter<I>(iter: I) -> Self
165    where
166        I: IntoIterator<Item = Table>,
167    {
168        let v = iter.into_iter().map(Item::Table);
169        Self {
170            values: v.collect(),
171            span: None,
172        }
173    }
174}
175
176impl IntoIterator for ArrayOfTables {
177    type Item = Table;
178    type IntoIter = ArrayOfTablesIntoIter;
179
180    fn into_iter(self) -> Self::IntoIter {
181        Box::new(
182            self.values
183                .into_iter()
184                .filter(|v| v.is_table())
185                .map(|v| v.into_table().unwrap()),
186        )
187    }
188}
189
190impl<'s> IntoIterator for &'s ArrayOfTables {
191    type Item = &'s Table;
192    type IntoIter = ArrayOfTablesIter<'s>;
193
194    fn into_iter(self) -> Self::IntoIter {
195        self.iter()
196    }
197}
198
199#[cfg(feature = "display")]
200impl std::fmt::Display for ArrayOfTables {
201    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
202        // HACK: Without the header, we don't really have a proper way of printing this
203        self.clone().into_array().fmt(f)
204    }
205}