graphql_query/visit/
folder_simple.rs

1use super::folder::Folder;
2use super::VisitInfo;
3use crate::ast::*;
4use crate::error::Result;
5
6/// This trait is a simplified version of the [Folder] trait for ease of use.
7///
8/// All structures implementing `SimpleFolder` automatically implement the `Folder` trait.
9///
10/// Its callbacks don't receive the AST Context and only return the AST Nodes themselves a `Result`
11/// having to wrap them.
12pub trait SimpleFolder<'a> {
13    /// Folds an [`OperationDefinition`] into a new node as part of a new, transformed AST.
14    #[inline]
15    fn operation(&mut self, operation: OperationDefinition<'a>) -> OperationDefinition<'a> {
16        operation
17    }
18
19    /// Folds a [`FragmentDefinition`] into a new node as part of a new, transformed AST.
20    #[inline]
21    fn fragment(&mut self, fragment: FragmentDefinition<'a>) -> FragmentDefinition<'a> {
22        fragment
23    }
24
25    /// Folds a [`VariableDefinitions`] node into a new node as part of a new, transformed AST.
26    #[inline]
27    fn variable_definitions(
28        &mut self,
29        var_defs: VariableDefinitions<'a>,
30    ) -> VariableDefinitions<'a> {
31        var_defs
32    }
33
34    /// Folds a [`VariableDefinition`] into a new node as part of a new, transformed AST.
35    #[inline]
36    fn variable_definition(&mut self, var_def: VariableDefinition<'a>) -> VariableDefinition<'a> {
37        var_def
38    }
39
40    /// Folds a [`SelectionSet`] into a new node as part of a new, transformed AST.
41    #[inline]
42    fn selection_set(&mut self, selection_set: SelectionSet<'a>) -> SelectionSet<'a> {
43        selection_set
44    }
45
46    /// Folds a [`FragmentSpread`] node into a new node as part of a new, transformed AST.
47    #[inline]
48    fn fragment_spread(&mut self, fragment_spread: FragmentSpread<'a>) -> FragmentSpread<'a> {
49        fragment_spread
50    }
51
52    /// Folds an [`InlineFragment`] into a new node as part of a new, transformed AST.
53    #[inline]
54    fn inline_fragment(&mut self, inline_fragment: InlineFragment<'a>) -> InlineFragment<'a> {
55        inline_fragment
56    }
57
58    /// Folds a [Field] into a new node as part of a new, transformed AST.
59    #[inline]
60    fn field(&mut self, field: Field<'a>) -> Field<'a> {
61        field
62    }
63
64    /// Folds a [Directives] node into a new node as part of a new, transformed AST.
65    #[inline]
66    fn directives(&mut self, directives: Directives<'a>) -> Directives<'a> {
67        directives
68    }
69
70    /// Folds a [Directive] into a new node as part of a new, transformed AST.
71    #[inline]
72    fn directive(&mut self, directive: Directive<'a>) -> Directive<'a> {
73        directive
74    }
75
76    /// Folds a [Arguments] node into a new node as part of a new, transformed AST.
77    #[inline]
78    fn arguments(&mut self, arguments: Arguments<'a>) -> Arguments<'a> {
79        arguments
80    }
81
82    /// Folds an [Argument] into a new node as part of a new, transformed AST.
83    #[inline]
84    fn argument(&mut self, argument: Argument<'a>) -> Argument<'a> {
85        argument
86    }
87
88    /// Folds a [Value] node into a new node as part of a new, transformed AST.
89    #[inline]
90    fn value(&mut self, value: Value<'a>) -> Value<'a> {
91        value
92    }
93
94    /// Folds a [Type] node into a new node as part of a new, transformed AST.
95    #[inline]
96    fn of_type(&mut self, of_type: Type<'a>) -> Type<'a> {
97        of_type
98    }
99
100    /// Folds a [Variable] node into a new node as part of a new, transformed AST.
101    #[inline]
102    fn variable(&mut self, var: Variable<'a>) -> Variable<'a> {
103        var
104    }
105
106    /// Folds a [`NamedType`] node into a new node as part of a new, transformed AST.
107    #[inline]
108    fn named_type(&mut self, name: NamedType<'a>) -> NamedType<'a> {
109        name
110    }
111}
112
113impl<'a, F: SimpleFolder<'a>> Folder<'a> for F {
114    #[inline]
115    fn enter_operation(
116        &mut self,
117        _ctx: &'a ASTContext,
118        operation: OperationDefinition<'a>,
119        _info: &VisitInfo,
120    ) -> Result<OperationDefinition<'a>> {
121        Ok(SimpleFolder::operation(self, operation))
122    }
123
124    #[inline]
125    fn leave_operation(
126        &mut self,
127        _ctx: &'a ASTContext,
128        operation: OperationDefinition<'a>,
129        _info: &VisitInfo,
130    ) -> Result<OperationDefinition<'a>> {
131        Ok(operation)
132    }
133
134    #[inline]
135    fn enter_fragment(
136        &mut self,
137        _ctx: &'a ASTContext,
138        fragment: FragmentDefinition<'a>,
139        _info: &VisitInfo,
140    ) -> Result<FragmentDefinition<'a>> {
141        Ok(SimpleFolder::fragment(self, fragment))
142    }
143
144    #[inline]
145    fn leave_fragment(
146        &mut self,
147        _ctx: &'a ASTContext,
148        fragment: FragmentDefinition<'a>,
149        _info: &VisitInfo,
150    ) -> Result<FragmentDefinition<'a>> {
151        Ok(fragment)
152    }
153
154    #[inline]
155    fn variable_definitions(
156        &mut self,
157        _ctx: &'a ASTContext,
158        var_defs: VariableDefinitions<'a>,
159        _info: &VisitInfo,
160    ) -> Result<VariableDefinitions<'a>> {
161        Ok(SimpleFolder::variable_definitions(self, var_defs))
162    }
163
164    #[inline]
165    fn variable_definition(
166        &mut self,
167        _ctx: &'a ASTContext,
168        var_def: VariableDefinition<'a>,
169        _info: &VisitInfo,
170    ) -> Result<VariableDefinition<'a>> {
171        Ok(SimpleFolder::variable_definition(self, var_def))
172    }
173
174    #[inline]
175    fn selection_set(
176        &mut self,
177        _ctx: &'a ASTContext,
178        selection_set: SelectionSet<'a>,
179        _info: &VisitInfo,
180    ) -> Result<SelectionSet<'a>> {
181        Ok(SimpleFolder::selection_set(self, selection_set))
182    }
183
184    #[inline]
185    fn enter_fragment_spread(
186        &mut self,
187        _ctx: &'a ASTContext,
188        fragment_spread: FragmentSpread<'a>,
189        _info: &VisitInfo,
190    ) -> Result<FragmentSpread<'a>> {
191        Ok(SimpleFolder::fragment_spread(self, fragment_spread))
192    }
193
194    #[inline]
195    fn leave_fragment_spread(
196        &mut self,
197        _ctx: &'a ASTContext,
198        fragment_spread: FragmentSpread<'a>,
199        _info: &VisitInfo,
200    ) -> Result<FragmentSpread<'a>> {
201        Ok(fragment_spread)
202    }
203
204    #[inline]
205    fn enter_inline_fragment(
206        &mut self,
207        _ctx: &'a ASTContext,
208        inline_fragment: InlineFragment<'a>,
209        _info: &VisitInfo,
210    ) -> Result<InlineFragment<'a>> {
211        Ok(SimpleFolder::inline_fragment(self, inline_fragment))
212    }
213
214    #[inline]
215    fn leave_inline_fragment(
216        &mut self,
217        _ctx: &'a ASTContext,
218        inline_fragment: InlineFragment<'a>,
219        _info: &VisitInfo,
220    ) -> Result<InlineFragment<'a>> {
221        Ok(inline_fragment)
222    }
223
224    #[inline]
225    fn enter_field(
226        &mut self,
227        _ctx: &'a ASTContext,
228        field: Field<'a>,
229        _info: &VisitInfo,
230    ) -> Result<Field<'a>> {
231        Ok(SimpleFolder::field(self, field))
232    }
233
234    #[inline]
235    fn leave_field(
236        &mut self,
237        _ctx: &'a ASTContext,
238        field: Field<'a>,
239        _info: &VisitInfo,
240    ) -> Result<Field<'a>> {
241        Ok(field)
242    }
243
244    #[inline]
245    fn directives(
246        &mut self,
247        _ctx: &'a ASTContext,
248        directives: Directives<'a>,
249        _info: &VisitInfo,
250    ) -> Result<Directives<'a>> {
251        Ok(SimpleFolder::directives(self, directives))
252    }
253
254    #[inline]
255    fn enter_directive(
256        &mut self,
257        _ctx: &'a ASTContext,
258        directive: Directive<'a>,
259        _info: &VisitInfo,
260    ) -> Result<Directive<'a>> {
261        Ok(SimpleFolder::directive(self, directive))
262    }
263
264    #[inline]
265    fn leave_directive(
266        &mut self,
267        _ctx: &'a ASTContext,
268        directive: Directive<'a>,
269        _info: &VisitInfo,
270    ) -> Result<Directive<'a>> {
271        Ok(directive)
272    }
273
274    #[inline]
275    fn arguments(
276        &mut self,
277        _ctx: &'a ASTContext,
278        arguments: Arguments<'a>,
279        _info: &VisitInfo,
280    ) -> Result<Arguments<'a>> {
281        Ok(SimpleFolder::arguments(self, arguments))
282    }
283
284    #[inline]
285    fn argument(
286        &mut self,
287        _ctx: &'a ASTContext,
288        argument: Argument<'a>,
289        _info: &VisitInfo,
290    ) -> Result<Argument<'a>> {
291        Ok(SimpleFolder::argument(self, argument))
292    }
293
294    #[inline]
295    fn value(
296        &mut self,
297        _ctx: &'a ASTContext,
298        value: Value<'a>,
299        _info: &VisitInfo,
300    ) -> Result<Value<'a>> {
301        Ok(SimpleFolder::value(self, value))
302    }
303
304    #[inline]
305    fn of_type(
306        &mut self,
307        _ctx: &'a ASTContext,
308        of_type: Type<'a>,
309        _info: &VisitInfo,
310    ) -> Result<Type<'a>> {
311        Ok(SimpleFolder::of_type(self, of_type))
312    }
313
314    #[inline]
315    fn variable(
316        &mut self,
317        _ctx: &'a ASTContext,
318        var: Variable<'a>,
319        _info: &VisitInfo,
320    ) -> Result<Variable<'a>> {
321        Ok(SimpleFolder::variable(self, var))
322    }
323
324    #[inline]
325    fn named_type(
326        &mut self,
327        _ctx: &'a ASTContext,
328        name: NamedType<'a>,
329        _info: &VisitInfo,
330    ) -> Result<NamedType<'a>> {
331        Ok(SimpleFolder::named_type(self, name))
332    }
333}