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    /// Removes a table with the given index.
103    pub fn remove(&mut self, index: usize) -> Table {
104        self.values
105            .remove(index)
106            .into_table()
107            .expect("cannot have any other item in an array-of-tables")
108    }
109
110    /// Retains only the elements specified by the `keep` predicate.
111    ///
112    /// In other words, remove all tables for which `keep(&table)` returns `false`.
113    ///
114    /// This method operates in place, visiting each element exactly once in the
115    /// original order, and preserves the order of the retained elements.
116    pub fn retain<F>(&mut self, mut keep: F)
117    where
118        F: FnMut(&Table) -> bool,
119    {
120        self.values
121            .retain(|item| item.as_table().map(&mut keep).unwrap_or(false));
122    }
123}
124
125/// An iterator type over [`ArrayOfTables`]'s [`Table`]s
126pub type ArrayOfTablesIter<'a> = Box<dyn Iterator<Item = &'a Table> + 'a>;
127/// An iterator type over [`ArrayOfTables`]'s [`Table`]s
128pub type ArrayOfTablesIterMut<'a> = Box<dyn Iterator<Item = &'a mut Table> + 'a>;
129/// An iterator type over [`ArrayOfTables`]'s [`Table`]s
130pub type ArrayOfTablesIntoIter = Box<dyn Iterator<Item = Table>>;
131
132impl Extend<Table> for ArrayOfTables {
133    fn extend<T: IntoIterator<Item = Table>>(&mut self, iter: T) {
134        for value in iter {
135            self.push(value);
136        }
137    }
138}
139
140impl FromIterator<Table> for ArrayOfTables {
141    fn from_iter<I>(iter: I) -> Self
142    where
143        I: IntoIterator<Item = Table>,
144    {
145        let v = iter.into_iter().map(Item::Table);
146        Self {
147            values: v.collect(),
148            span: None,
149        }
150    }
151}
152
153impl IntoIterator for ArrayOfTables {
154    type Item = Table;
155    type IntoIter = ArrayOfTablesIntoIter;
156
157    fn into_iter(self) -> Self::IntoIter {
158        Box::new(
159            self.values
160                .into_iter()
161                .filter(|v| v.is_table())
162                .map(|v| v.into_table().unwrap()),
163        )
164    }
165}
166
167impl<'s> IntoIterator for &'s ArrayOfTables {
168    type Item = &'s Table;
169    type IntoIter = ArrayOfTablesIter<'s>;
170
171    fn into_iter(self) -> Self::IntoIter {
172        self.iter()
173    }
174}
175
176#[cfg(feature = "display")]
177impl std::fmt::Display for ArrayOfTables {
178    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
179        // HACK: Without the header, we don't really have a proper way of printing this
180        self.clone().into_array().fmt(f)
181    }
182}