toml/de/parser/
dearray.rs

1use serde_spanned::Spanned;
2
3use crate::alloc_prelude::*;
4use crate::de::DeValue;
5
6/// Type representing a TOML array, payload of the `DeValue::Array` variant
7#[derive(Clone)]
8pub struct DeArray<'i> {
9    items: Vec<Spanned<DeValue<'i>>>,
10    array_of_tables: bool,
11}
12
13impl<'i> DeArray<'i> {
14    /// Constructs a new, empty `DeArray`.
15    ///
16    /// This will not allocate until elements are pushed onto it.
17    pub const fn new() -> Self {
18        Self {
19            items: Vec::new(),
20            array_of_tables: false,
21        }
22    }
23
24    /// Appends an element to the back of a collection.
25    ///
26    /// # Panics
27    ///
28    /// Panics if the new capacity exceeds `isize::MAX` _bytes_.
29    pub fn push(&mut self, value: Spanned<DeValue<'i>>) {
30        self.items.push(value);
31    }
32}
33
34impl DeArray<'_> {
35    pub(crate) fn is_array_of_tables(&self) -> bool {
36        self.array_of_tables
37    }
38
39    pub(crate) fn set_array_of_tables(&mut self, yes: bool) {
40        self.array_of_tables = yes;
41    }
42}
43
44impl<'i> core::ops::Deref for DeArray<'i> {
45    type Target = [Spanned<DeValue<'i>>];
46
47    #[inline]
48    fn deref(&self) -> &[Spanned<DeValue<'i>>] {
49        self.items.as_slice()
50    }
51}
52
53impl<'i> core::ops::DerefMut for DeArray<'i> {
54    #[inline]
55    fn deref_mut(&mut self) -> &mut [Spanned<DeValue<'i>>] {
56        self.items.as_mut_slice()
57    }
58}
59
60impl<'i> AsRef<[Spanned<DeValue<'i>>]> for DeArray<'i> {
61    fn as_ref(&self) -> &[Spanned<DeValue<'i>>] {
62        &self.items
63    }
64}
65
66impl<'i> AsMut<[Spanned<DeValue<'i>>]> for DeArray<'i> {
67    fn as_mut(&mut self) -> &mut [Spanned<DeValue<'i>>] {
68        &mut self.items
69    }
70}
71
72impl<'i> core::borrow::Borrow<[Spanned<DeValue<'i>>]> for DeArray<'i> {
73    fn borrow(&self) -> &[Spanned<DeValue<'i>>] {
74        &self.items[..]
75    }
76}
77
78impl<'i> core::borrow::BorrowMut<[Spanned<DeValue<'i>>]> for DeArray<'i> {
79    fn borrow_mut(&mut self) -> &mut [Spanned<DeValue<'i>>] {
80        &mut self.items[..]
81    }
82}
83
84impl<'i, I: core::slice::SliceIndex<[Spanned<DeValue<'i>>]>> core::ops::Index<I> for DeArray<'i> {
85    type Output = I::Output;
86
87    #[inline]
88    fn index(&self, index: I) -> &Self::Output {
89        self.items.index(index)
90    }
91}
92
93impl<'a, 'i> IntoIterator for &'a DeArray<'i> {
94    type Item = &'a Spanned<DeValue<'i>>;
95
96    type IntoIter = core::slice::Iter<'a, Spanned<DeValue<'i>>>;
97
98    fn into_iter(self) -> Self::IntoIter {
99        self.iter()
100    }
101}
102
103impl<'i> IntoIterator for DeArray<'i> {
104    type Item = Spanned<DeValue<'i>>;
105
106    type IntoIter = alloc::vec::IntoIter<Spanned<DeValue<'i>>>;
107
108    #[inline]
109    fn into_iter(self) -> Self::IntoIter {
110        self.items.into_iter()
111    }
112}
113
114impl<'i> FromIterator<Spanned<DeValue<'i>>> for DeArray<'i> {
115    #[inline]
116    #[track_caller]
117    fn from_iter<I: IntoIterator<Item = Spanned<DeValue<'i>>>>(iter: I) -> Self {
118        Self {
119            items: iter.into_iter().collect(),
120            array_of_tables: false,
121        }
122    }
123}
124
125impl Default for DeArray<'static> {
126    #[inline]
127    fn default() -> Self {
128        Self {
129            items: Default::default(),
130            array_of_tables: false,
131        }
132    }
133}
134
135impl core::fmt::Debug for DeArray<'_> {
136    #[inline]
137    fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
138        self.items.fmt(formatter)
139    }
140}