toml_spanner/item/
to_toml.rs1use super::array::Array;
2use super::table::Table;
3use super::{FLAG_MASK, HINTS_BIT, Item, ItemMetadata, NOT_PROJECTED, TAG_MASK, TAG_SHIFT};
4
5pub(crate) const FULL_MATCH_BIT: u32 = 1 << 30;
7pub(crate) const IGNORE_SOURCE_ORDER_BIT: u32 = 1 << 29;
10pub(crate) const IGNORE_SOURCE_STYLE_BIT: u32 = 1 << 28;
13pub(crate) const ARRAY_REORDERED_BIT: u32 = 1 << 27;
18pub(crate) const EXPANDED_BIT: u32 = 1 << 25;
22pub(crate) const IGNORE_SOURCE_FORMATTING_RECURSIVELY_BIT: u32 = 1 << 24;
25pub(crate) const PRESERVED_SPAN_START_BIT: u32 = 1 << 23;
28
29impl ItemMetadata {
30 #[inline]
32 pub(crate) fn projected_index(&self) -> u32 {
33 self.start_and_tag >> TAG_SHIFT
34 }
35
36 #[inline]
39 fn hints_preserve_mask(&self) -> u32 {
40 ((self.end_and_flag as i32) >> 31) as u32 | FLAG_MASK
41 }
42
43 #[inline]
44 fn enter_hints_preserving_span_start(&mut self) {
45 let was_span_mode = self.is_span_mode();
46 self.end_and_flag |= HINTS_BIT;
47 if was_span_mode {
48 self.end_and_flag |= PRESERVED_SPAN_START_BIT;
49 }
50 }
51
52 #[inline]
53 pub(crate) fn span_identity_start(&self) -> Option<u32> {
54 let has_preserved_start = self.end_and_flag & (HINTS_BIT | PRESERVED_SPAN_START_BIT)
55 == (HINTS_BIT | PRESERVED_SPAN_START_BIT);
56 if self.is_span_mode() || has_preserved_start {
57 Some(self.start_and_tag >> TAG_SHIFT)
58 } else {
59 None
60 }
61 }
62
63 #[inline]
66 pub(crate) fn set_reprojected_index(&mut self, index: usize) -> bool {
67 if index <= (u32::MAX >> TAG_SHIFT) as usize {
68 self.start_and_tag = (self.start_and_tag & TAG_MASK) | ((index as u32) << TAG_SHIFT);
69 self.end_and_flag =
70 (self.end_and_flag & (self.hints_preserve_mask() & !PRESERVED_SPAN_START_BIT))
71 | HINTS_BIT;
72 true
73 } else {
74 false
75 }
76 }
77
78 #[inline]
81 pub(crate) fn set_reprojected_to_none(&mut self) {
82 self.start_and_tag |= NOT_PROJECTED;
83 self.end_and_flag =
84 (self.end_and_flag
85 & (self.hints_preserve_mask()
86 & !(FULL_MATCH_BIT | PRESERVED_SPAN_START_BIT)))
87 | HINTS_BIT;
88 }
89
90 #[inline]
91 pub(crate) fn set_reprojected_full_match(&mut self) {
92 self.end_and_flag |= FULL_MATCH_BIT;
93 }
94
95 #[inline]
96 pub(crate) fn is_reprojected_full_match(&self) -> bool {
97 self.end_and_flag & FULL_MATCH_BIT != 0
98 }
99
100 #[inline]
102 pub(crate) fn set_ignore_source_order(&mut self) {
103 self.enter_hints_preserving_span_start();
104 self.end_and_flag |= IGNORE_SOURCE_ORDER_BIT;
105 }
106
107 #[inline]
110 pub(crate) fn ignore_source_order(&self) -> bool {
111 self.end_and_flag & (HINTS_BIT | IGNORE_SOURCE_ORDER_BIT)
112 == (HINTS_BIT | IGNORE_SOURCE_ORDER_BIT)
113 }
114
115 #[inline]
117 pub(crate) fn set_array_reordered(&mut self) {
118 self.enter_hints_preserving_span_start();
119 self.end_and_flag |= ARRAY_REORDERED_BIT;
120 }
121
122 #[inline]
124 pub(crate) fn array_reordered(&self) -> bool {
125 self.end_and_flag & (HINTS_BIT | ARRAY_REORDERED_BIT) == (HINTS_BIT | ARRAY_REORDERED_BIT)
126 }
127
128 #[inline]
130 pub(crate) fn set_ignore_source_style(&mut self) {
131 self.enter_hints_preserving_span_start();
132 self.end_and_flag |= IGNORE_SOURCE_STYLE_BIT;
133 }
134
135 #[inline]
137 pub(crate) fn ignore_source_style(&self) -> bool {
138 self.end_and_flag & (HINTS_BIT | IGNORE_SOURCE_STYLE_BIT)
139 == (HINTS_BIT | IGNORE_SOURCE_STYLE_BIT)
140 }
141
142 #[inline]
143 pub(crate) fn set_expanded(&mut self) {
144 self.enter_hints_preserving_span_start();
145 self.end_and_flag |= EXPANDED_BIT;
146 }
147
148 #[inline]
149 pub(crate) fn is_expanded(&self) -> bool {
150 self.end_and_flag & (HINTS_BIT | EXPANDED_BIT) == (HINTS_BIT | EXPANDED_BIT)
151 }
152
153 #[inline]
154 pub(crate) fn clear_expanded(&mut self) {
155 self.end_and_flag &= !EXPANDED_BIT;
156 }
157
158 #[inline]
160 pub(crate) fn set_ignore_source_formatting_recursively(&mut self) {
161 self.enter_hints_preserving_span_start();
162 self.end_and_flag |= IGNORE_SOURCE_FORMATTING_RECURSIVELY_BIT;
163 }
164
165 #[inline]
167 pub(crate) fn ignore_source_formatting_recursively(&self) -> bool {
168 self.end_and_flag & (HINTS_BIT | IGNORE_SOURCE_FORMATTING_RECURSIVELY_BIT)
169 == (HINTS_BIT | IGNORE_SOURCE_FORMATTING_RECURSIVELY_BIT)
170 }
171}
172
173impl<'de> Item<'de> {
174 pub(crate) fn projected<'a>(&self, inputs: &[&'a Item<'a>]) -> Option<&'a Item<'a>> {
176 let index = self.meta.projected_index();
177 inputs.get(index as usize).copied()
178 }
179 pub(crate) fn set_reprojected_to_none(&mut self) {
180 self.meta.set_reprojected_to_none();
181 }
182 pub(crate) fn set_reprojected_index(&mut self, index: usize) -> bool {
183 self.meta.set_reprojected_index(index)
184 }
185 pub(crate) fn set_reprojected_full_match(&mut self) {
187 self.meta.set_reprojected_full_match();
188 }
189 pub(crate) fn is_reprojected_full_match(&self) -> bool {
191 self.meta.is_reprojected_full_match()
192 }
193
194 pub fn set_ignore_source_formatting_recursively(&mut self) {
199 self.meta.set_ignore_source_formatting_recursively();
200 }
201
202 #[must_use]
204 pub fn ignore_source_formatting_recursively(&self) -> bool {
205 self.meta.ignore_source_formatting_recursively()
206 }
207
208 #[inline]
212 pub(crate) fn is_subsection(&self) -> bool {
213 self.has_header_bit() || self.is_implicit_table() || self.is_aot()
214 }
215
216 #[inline]
217 #[cfg(test)]
218 pub(crate) fn set_flag(&mut self, flag: u32) {
219 self.meta.set_flag(flag);
220 }
221}
222
223impl<'de> Table<'de> {
224 pub fn set_ignore_source_order(&mut self) {
227 self.meta.set_ignore_source_order();
228 }
229
230 #[must_use]
232 pub fn ignore_source_order(&self) -> bool {
233 self.meta.ignore_source_order()
234 }
235
236 pub fn set_ignore_source_style(&mut self) {
240 self.meta.set_ignore_source_style();
241 }
242
243 #[must_use]
245 pub fn ignore_source_style(&self) -> bool {
246 self.meta.ignore_source_style()
247 }
248
249 #[must_use]
251 pub fn is_auto_style(&self) -> bool {
252 self.meta.is_auto_style()
253 }
254}
255
256impl<'de> Array<'de> {
257 #[must_use]
259 pub fn is_auto_style(&self) -> bool {
260 self.meta.is_auto_style()
261 }
262
263 #[must_use]
266 pub fn is_expanded(&self) -> bool {
267 self.meta.is_expanded()
268 }
269
270 pub fn set_expanded(&mut self) {
272 self.meta.set_expanded();
273 }
274
275 pub fn clear_expanded(&mut self) {
277 self.meta.clear_expanded();
278 }
279}