1pub(crate) trait Visitor {
18 fn process(&mut self, _item: &mut crate::Item<'_>) -> ProcessResult {
23 ProcessResult::default()
24 }
25}
26
27#[must_use]
33#[derive(PartialEq)]
34pub(crate) struct ProcessResult {
35 comments_after: Vec<String>,
36 recurse: bool,
37}
38
39impl Default for ProcessResult {
40 fn default() -> Self {
41 Self {
42 comments_after: vec![],
43 recurse: true,
44 }
45 }
46}
47
48impl ProcessResult {
50 pub(crate) fn with_comment_after(mut self, comment: impl Into<String>) -> Self {
51 self.comments_after.push(comment.into());
52 self
53 }
54
55 pub(crate) fn stop_recursion(self) -> Self {
61 Self {
62 recurse: false,
63 ..self
64 }
65 }
66
67 pub(crate) fn chain(self, other: Self) -> Self {
68 Self {
69 recurse: self.recurse && other.recurse,
70 comments_after: self
71 .comments_after
72 .into_iter()
73 .chain(other.comments_after)
74 .collect(),
75 }
76 }
77}
78
79impl ProcessResult {
84 #[track_caller]
87 pub(crate) fn done(self) {
88 if self != Default::default() {
89 panic!(
90 "use_space_before() and use_space_after() have been called, this should be empty."
91 );
92 }
93 }
94
95 pub(crate) fn take_recurse(&mut self) -> bool {
96 let recurse = self.recurse;
97 self.recurse = true;
100 recurse
101 }
102
103 pub(crate) fn use_space_before(self, _s: &mut impl crate::space::Spaceish) -> Self {
104 self
105 }
106
107 pub(crate) fn use_space_after(mut self, s: &mut impl crate::space::Spaceish) -> Self {
108 for comment in self.comments_after.drain(..) {
109 s.prepend_comment(&comment);
110 }
111 self
112 }
113}
114
115pub(crate) struct ApplicationLiteralsVisitor<F> {
116 pub(crate) user_fn: F,
117}
118
119impl<F: for<'b> FnMut(String, String, &mut crate::Item<'b>) -> Result<(), String>> Visitor
120 for ApplicationLiteralsVisitor<F>
121{
122 fn process(&mut self, item: &mut crate::Item<'_>) -> ProcessResult {
123 let Ok((identifier, value)) = item.get_application_literal() else {
124 return ProcessResult::default();
125 };
126 match (self.user_fn)(identifier, value, item) {
127 Ok(()) => ProcessResult::default(),
128 Err(e) => ProcessResult::default().with_comment_after(e),
129 }
130 }
131}
132
133pub(crate) struct TagVisitor<F> {
134 pub(crate) user_fn: F,
135}
136
137impl<F: for<'b> FnMut(u64, &mut crate::Item<'b>) -> Result<(), String>> Visitor for TagVisitor<F> {
138 fn process(&mut self, item: &mut crate::Item<'_>) -> ProcessResult {
139 let Ok(tag) = item.get_tag() else {
140 return ProcessResult::default();
141 };
142 match (self.user_fn)(tag, item) {
143 Ok(()) => ProcessResult::default(),
144 Err(e) => ProcessResult::default().with_comment_after(e),
145 }
146 }
147}
148
149pub(crate) struct ArrayElementVisitor<F> {
150 seen_first: bool,
151 user_fn: F,
152}
153
154impl<F> ArrayElementVisitor<F> {
155 pub(crate) fn new(user_fn: F) -> Self {
156 Self {
157 seen_first: false,
158 user_fn,
159 }
160 }
161}
162
163impl<F: for<'b> FnMut(&mut crate::Item<'b>) -> Result<Option<String>, String>> Visitor
164 for ArrayElementVisitor<F>
165{
166 fn process(&mut self, item: &mut crate::Item<'_>) -> ProcessResult {
167 if !self.seen_first {
168 if item.get_array_items_mut().is_err() {
170 return ProcessResult::default()
171 .with_comment_after("Expected array".to_string())
172 .stop_recursion();
173 }
174 self.seen_first = true;
175 return ProcessResult::default();
176 }
177
178 match (self.user_fn)(item) {
179 Ok(Some(text)) => ProcessResult::default().with_comment_after(text),
182 Ok(None) => ProcessResult::default(),
183 Err(e) => ProcessResult::default().with_comment_after(e),
184 }
185 .stop_recursion()
186 }
187}
188
189pub(crate) struct MapElementVisitor<F> {
190 seen_first: bool,
191 next_value_handler: Option<Option<MapValueHandler>>,
197 user_fn: F,
198}
199
200impl<F> MapElementVisitor<F> {
201 pub(crate) fn new(user_fn: F) -> Self {
202 Self {
203 seen_first: false,
204 next_value_handler: None,
205 user_fn,
206 }
207 }
208}
209
210pub type MapValueHandler =
211 Box<dyn for<'data> FnOnce(&mut crate::Item<'data>) -> Result<Option<String>, String>>;
212
213impl<
214 F: for<'b> FnMut(
215 &mut crate::Item<'b>,
216 ) -> Result<(Option<String>, Option<MapValueHandler>), String>,
217 > Visitor for MapElementVisitor<F>
218{
219 fn process(&mut self, item: &mut crate::Item<'_>) -> ProcessResult {
220 if !self.seen_first {
221 if item.get_map_items().is_err() {
223 return ProcessResult::default()
224 .with_comment_after("Expected map".to_string())
225 .stop_recursion();
226 }
227 self.seen_first = true;
228 return ProcessResult::default();
229 }
230
231 if let Some(handler) = self.next_value_handler.take() {
232 if let Some(handler) = handler {
233 match (handler)(item) {
234 Ok(Some(text)) => ProcessResult::default().with_comment_after(text),
237 Ok(None) => ProcessResult::default(),
238 Err(e) => ProcessResult::default().with_comment_after(e),
239 }
240 } else {
241 Default::default()
242 }
243 } else {
244 match (self.user_fn)(item) {
245 Ok((comment, value_handler)) => {
248 self.next_value_handler = Some(value_handler);
249 match comment {
250 Some(text) => ProcessResult::default().with_comment_after(text),
251 None => ProcessResult::default(),
252 }
253 }
254 Err(e) => ProcessResult::default().with_comment_after(e),
255 }
256 }
257 .stop_recursion()
258 }
259}