1#![allow(missing_docs)]
2
3use crate::{
76 Array, ArrayOfTables, Datetime, Document, Formatted, InlineTable, Item, Table, TableLike, Value,
77};
78
79pub trait Visit<'doc> {
83 fn visit_document(&mut self, node: &'doc Document) {
84 visit_document(self, node);
85 }
86
87 fn visit_item(&mut self, node: &'doc Item) {
88 visit_item(self, node);
89 }
90
91 fn visit_table(&mut self, node: &'doc Table) {
92 visit_table(self, node);
93 }
94
95 fn visit_inline_table(&mut self, node: &'doc InlineTable) {
96 visit_inline_table(self, node)
97 }
98
99 fn visit_table_like(&mut self, node: &'doc dyn TableLike) {
100 visit_table_like(self, node);
101 }
102
103 fn visit_table_like_kv(&mut self, key: &'doc str, node: &'doc Item) {
104 visit_table_like_kv(self, key, node);
105 }
106
107 fn visit_array(&mut self, node: &'doc Array) {
108 visit_array(self, node);
109 }
110
111 fn visit_array_of_tables(&mut self, node: &'doc ArrayOfTables) {
112 visit_array_of_tables(self, node);
113 }
114
115 fn visit_value(&mut self, node: &'doc Value) {
116 visit_value(self, node);
117 }
118
119 fn visit_boolean(&mut self, node: &'doc Formatted<bool>) {
120 visit_boolean(self, node)
121 }
122
123 fn visit_datetime(&mut self, node: &'doc Formatted<Datetime>) {
124 visit_datetime(self, node);
125 }
126
127 fn visit_float(&mut self, node: &'doc Formatted<f64>) {
128 visit_float(self, node)
129 }
130
131 fn visit_integer(&mut self, node: &'doc Formatted<i64>) {
132 visit_integer(self, node)
133 }
134
135 fn visit_string(&mut self, node: &'doc Formatted<String>) {
136 visit_string(self, node)
137 }
138}
139
140pub fn visit_document<'doc, V>(v: &mut V, node: &'doc Document)
141where
142 V: Visit<'doc> + ?Sized,
143{
144 v.visit_table(node.as_table());
145}
146
147pub fn visit_item<'doc, V>(v: &mut V, node: &'doc Item)
148where
149 V: Visit<'doc> + ?Sized,
150{
151 match node {
152 Item::None => {}
153 Item::Value(value) => v.visit_value(value),
154 Item::Table(table) => v.visit_table(table),
155 Item::ArrayOfTables(array) => v.visit_array_of_tables(array),
156 }
157}
158
159pub fn visit_table<'doc, V>(v: &mut V, node: &'doc Table)
160where
161 V: Visit<'doc> + ?Sized,
162{
163 v.visit_table_like(node)
164}
165
166pub fn visit_inline_table<'doc, V>(v: &mut V, node: &'doc InlineTable)
167where
168 V: Visit<'doc> + ?Sized,
169{
170 v.visit_table_like(node)
171}
172
173pub fn visit_table_like<'doc, V>(v: &mut V, node: &'doc dyn TableLike)
174where
175 V: Visit<'doc> + ?Sized,
176{
177 for (key, item) in node.iter() {
178 v.visit_table_like_kv(key, item)
179 }
180}
181
182pub fn visit_table_like_kv<'doc, V>(v: &mut V, _key: &'doc str, node: &'doc Item)
183where
184 V: Visit<'doc> + ?Sized,
185{
186 v.visit_item(node)
187}
188
189pub fn visit_array<'doc, V>(v: &mut V, node: &'doc Array)
190where
191 V: Visit<'doc> + ?Sized,
192{
193 for value in node.iter() {
194 v.visit_value(value);
195 }
196}
197
198pub fn visit_array_of_tables<'doc, V>(v: &mut V, node: &'doc ArrayOfTables)
199where
200 V: Visit<'doc> + ?Sized,
201{
202 for table in node.iter() {
203 v.visit_table(table);
204 }
205}
206
207pub fn visit_value<'doc, V>(v: &mut V, node: &'doc Value)
208where
209 V: Visit<'doc> + ?Sized,
210{
211 match node {
212 Value::String(s) => v.visit_string(s),
213 Value::Integer(i) => v.visit_integer(i),
214 Value::Float(f) => v.visit_float(f),
215 Value::Boolean(b) => v.visit_boolean(b),
216 Value::Datetime(dt) => v.visit_datetime(dt),
217 Value::Array(array) => v.visit_array(array),
218 Value::InlineTable(table) => v.visit_inline_table(table),
219 }
220}
221
222macro_rules! empty_visit {
223 ($name: ident, $t: ty) => {
224 fn $name<'doc, V>(_v: &mut V, _node: &'doc $t)
225 where
226 V: Visit<'doc> + ?Sized,
227 {
228 }
229 };
230}
231
232empty_visit!(visit_boolean, Formatted<bool>);
233empty_visit!(visit_datetime, Formatted<Datetime>);
234empty_visit!(visit_float, Formatted<f64>);
235empty_visit!(visit_integer, Formatted<i64>);
236empty_visit!(visit_string, Formatted<String>);