Skip to main content

graphql_query/ast/
ast_conversion.rs

1// #![allow(clippy::needless_update)]
2use super::ast::*;
3use bumpalo::collections::{vec::IntoIter, Vec};
4
5// TODO: from_iter_in could be a good helper here as well
6
7impl<'a> IntoIterator for ListValue<'a> {
8    type Item = Value<'a>;
9    type IntoIter = IntoIter<'a, Value<'a>>;
10    #[inline]
11    fn into_iter(self) -> Self::IntoIter {
12        self.children.into_iter()
13    }
14}
15
16impl<'a> IntoIterator for ObjectValue<'a> {
17    type Item = ObjectField<'a>;
18    type IntoIter = IntoIter<'a, ObjectField<'a>>;
19    #[inline]
20    fn into_iter(self) -> Self::IntoIter {
21        self.children.into_iter()
22    }
23}
24
25impl<'a> IntoIterator for Arguments<'a> {
26    type Item = Argument<'a>;
27    type IntoIter = IntoIter<'a, Argument<'a>>;
28    #[inline]
29    fn into_iter(self) -> Self::IntoIter {
30        self.children.into_iter()
31    }
32}
33
34impl<'a> IntoIterator for Directives<'a> {
35    type Item = Directive<'a>;
36    type IntoIter = IntoIter<'a, Directive<'a>>;
37    #[inline]
38    fn into_iter(self) -> Self::IntoIter {
39        self.children.into_iter()
40    }
41}
42
43impl<'a> IntoIterator for VariableDefinitions<'a> {
44    type Item = VariableDefinition<'a>;
45    type IntoIter = IntoIter<'a, VariableDefinition<'a>>;
46    #[inline]
47    fn into_iter(self) -> Self::IntoIter {
48        self.children.into_iter()
49    }
50}
51
52impl<'a> IntoIterator for SelectionSet<'a> {
53    type Item = Selection<'a>;
54    type IntoIter = IntoIter<'a, Selection<'a>>;
55    #[inline]
56    fn into_iter(self) -> Self::IntoIter {
57        self.selections.into_iter()
58    }
59}
60
61pub trait DefaultIn<'a> {
62    fn default_in(arena: &'a bumpalo::Bump) -> Self;
63}
64
65impl<'a, T> DefaultIn<'a> for T
66where
67    T: Default,
68{
69    fn default_in(_ctx: &'a bumpalo::Bump) -> Self {
70        Self::default()
71    }
72}
73
74impl<'a> DefaultIn<'a> for Document<'a> {
75    fn default_in(arena: &'a bumpalo::Bump) -> Self {
76        Document {
77            definitions: Vec::new_in(arena),
78            size_hint: 0,
79        }
80    }
81}
82
83impl<'a> DefaultIn<'a> for VariableDefinitions<'a> {
84    fn default_in(arena: &'a bumpalo::Bump) -> Self {
85        VariableDefinitions {
86            children: Vec::new_in(arena),
87        }
88    }
89}
90
91impl<'a> DefaultIn<'a> for ObjectValue<'a> {
92    fn default_in(arena: &'a bumpalo::Bump) -> Self {
93        ObjectValue {
94            children: Vec::new_in(arena),
95        }
96    }
97}
98
99impl<'a> DefaultIn<'a> for ListValue<'a> {
100    fn default_in(arena: &'a bumpalo::Bump) -> Self {
101        ListValue {
102            children: Vec::new_in(arena),
103        }
104    }
105}
106
107impl<'a> DefaultIn<'a> for Arguments<'a> {
108    fn default_in(arena: &'a bumpalo::Bump) -> Self {
109        Arguments {
110            children: Vec::new_in(arena),
111        }
112    }
113}
114
115impl<'a> DefaultIn<'a> for Directives<'a> {
116    fn default_in(arena: &'a bumpalo::Bump) -> Self {
117        Directives {
118            children: Vec::new_in(arena),
119        }
120    }
121}
122
123impl<'a> DefaultIn<'a> for SelectionSet<'a> {
124    fn default_in(arena: &'a bumpalo::Bump) -> Self {
125        SelectionSet {
126            selections: Vec::new_in(arena),
127        }
128    }
129}
130
131impl<'a> From<&'a str> for NamedType<'a> {
132    #[inline]
133    fn from(name: &'a str) -> Self {
134        NamedType { name }
135    }
136}
137
138impl<'a> From<&'a str> for Variable<'a> {
139    #[inline]
140    fn from(name: &'a str) -> Variable<'a> {
141        Variable { name }
142    }
143}
144
145impl From<bool> for BooleanValue {
146    #[inline]
147    fn from(value: bool) -> Self {
148        BooleanValue { value }
149    }
150}
151
152impl<'a> From<&'a str> for StringValue<'a> {
153    #[inline]
154    fn from(value: &'a str) -> Self {
155        StringValue { value }
156    }
157}
158
159impl<'a> From<Variable<'a>> for Value<'a> {
160    #[inline]
161    fn from(x: Variable<'a>) -> Self {
162        Value::Variable(x)
163    }
164}
165
166impl<'a> From<StringValue<'a>> for Value<'a> {
167    #[inline]
168    fn from(x: StringValue<'a>) -> Self {
169        Value::String(x)
170    }
171}
172
173impl<'a> From<FloatValue<'a>> for Value<'a> {
174    #[inline]
175    fn from(x: FloatValue<'a>) -> Self {
176        Value::Float(x)
177    }
178}
179
180impl<'a> From<IntValue<'a>> for Value<'a> {
181    #[inline]
182    fn from(x: IntValue<'a>) -> Self {
183        Value::Int(x)
184    }
185}
186
187impl<'a> From<BooleanValue> for Value<'a> {
188    #[inline]
189    fn from(x: BooleanValue) -> Self {
190        Value::Boolean(x)
191    }
192}
193
194impl<'a> From<EnumValue<'a>> for Value<'a> {
195    #[inline]
196    fn from(x: EnumValue<'a>) -> Self {
197        Value::Enum(x)
198    }
199}
200
201impl<'a> From<ListValue<'a>> for Value<'a> {
202    #[inline]
203    fn from(x: ListValue<'a>) -> Self {
204        Value::List(x)
205    }
206}
207
208impl<'a> From<ObjectValue<'a>> for Value<'a> {
209    #[inline]
210    fn from(x: ObjectValue<'a>) -> Self {
211        Value::Object(x)
212    }
213}
214
215impl<'a> From<NamedType<'a>> for Type<'a> {
216    #[inline]
217    fn from(x: NamedType<'a>) -> Self {
218        Type::NamedType(x)
219    }
220}
221
222impl<'a> From<Field<'a>> for Selection<'a> {
223    #[inline]
224    fn from(x: Field<'a>) -> Self {
225        Selection::Field(x)
226    }
227}
228
229impl<'a> From<FragmentSpread<'a>> for Selection<'a> {
230    #[inline]
231    fn from(x: FragmentSpread<'a>) -> Self {
232        Selection::FragmentSpread(x)
233    }
234}
235
236impl<'a> From<InlineFragment<'a>> for Selection<'a> {
237    #[inline]
238    fn from(x: InlineFragment<'a>) -> Self {
239        Selection::InlineFragment(x)
240    }
241}
242
243impl<'a> From<OperationDefinition<'a>> for Definition<'a> {
244    #[inline]
245    fn from(x: OperationDefinition<'a>) -> Self {
246        Definition::Operation(x)
247    }
248}
249
250impl<'a> From<FragmentDefinition<'a>> for Definition<'a> {
251    #[inline]
252    fn from(x: FragmentDefinition<'a>) -> Self {
253        Definition::Fragment(x)
254    }
255}