Skip to main content

cynic_parser/executable/generated/
selections.rs

1use super::prelude::*;
2use super::{
3    ExecutableId,
4    argument::Argument,
5    directive::Directive,
6    ids::{
7        ArgumentId, DirectiveId, FieldSelectionId, FragmentSpreadId, InlineFragmentId, SelectionId,
8    },
9};
10#[allow(unused_imports)]
11use std::fmt::{self, Write};
12
13pub enum SelectionRecord {
14    Field(FieldSelectionId),
15    InlineFragment(InlineFragmentId),
16    FragmentSpread(FragmentSpreadId),
17}
18
19#[derive(Clone, Copy, Debug)]
20pub enum Selection<'a> {
21    Field(FieldSelection<'a>),
22    InlineFragment(InlineFragment<'a>),
23    FragmentSpread(FragmentSpread<'a>),
24}
25
26impl<'a> Selection<'a> {
27    pub fn is_field(self) -> bool {
28        matches!(self, Self::Field(_))
29    }
30    pub fn is_inline_fragment(self) -> bool {
31        matches!(self, Self::InlineFragment(_))
32    }
33    pub fn is_fragment_spread(self) -> bool {
34        matches!(self, Self::FragmentSpread(_))
35    }
36    pub fn as_field(self) -> Option<FieldSelection<'a>> {
37        match self {
38            Self::Field(inner) => Some(inner),
39            _ => None,
40        }
41    }
42    pub fn as_inline_fragment(self) -> Option<InlineFragment<'a>> {
43        match self {
44            Self::InlineFragment(inner) => Some(inner),
45            _ => None,
46        }
47    }
48    pub fn as_fragment_spread(self) -> Option<FragmentSpread<'a>> {
49        match self {
50            Self::FragmentSpread(inner) => Some(inner),
51            _ => None,
52        }
53    }
54}
55
56impl ExecutableId for SelectionId {
57    type Reader<'a> = Selection<'a>;
58    fn read(self, document: &ExecutableDocument) -> Self::Reader<'_> {
59        match document.lookup(self) {
60            SelectionRecord::Field(id) => Selection::Field(document.read(*id)),
61            SelectionRecord::InlineFragment(id) => Selection::InlineFragment(document.read(*id)),
62            SelectionRecord::FragmentSpread(id) => Selection::FragmentSpread(document.read(*id)),
63        }
64    }
65}
66
67impl IdReader for Selection<'_> {
68    type Id = SelectionId;
69    type Reader<'a> = Selection<'a>;
70    fn new(id: Self::Id, document: &'_ ExecutableDocument) -> Self::Reader<'_> {
71        document.read(id)
72    }
73}
74
75pub struct FieldSelectionRecord {
76    pub alias: Option<StringId>,
77    pub alias_span: Option<Span>,
78    pub name: StringId,
79    pub name_span: Span,
80    pub arguments: IdRange<ArgumentId>,
81    pub directives: IdRange<DirectiveId>,
82    pub selection_set: IdRange<SelectionId>,
83    pub selection_set_span: Option<Span>,
84}
85
86#[derive(Clone, Copy)]
87pub struct FieldSelection<'a>(pub(in super::super) ReadContext<'a, FieldSelectionId>);
88
89impl<'a> FieldSelection<'a> {
90    pub fn alias(&self) -> Option<&'a str> {
91        let document = self.0.document;
92        document
93            .lookup(self.0.id)
94            .alias
95            .map(|id| document.lookup(id))
96    }
97    pub fn alias_span(&self) -> Option<Span> {
98        let document = self.0.document;
99        document.lookup(self.0.id).alias_span
100    }
101    pub fn name(&self) -> &'a str {
102        let document = &self.0.document;
103        document.lookup(document.lookup(self.0.id).name)
104    }
105    pub fn name_span(&self) -> Span {
106        let document = self.0.document;
107        document.lookup(self.0.id).name_span
108    }
109    pub fn arguments(&self) -> Iter<'a, Argument<'a>> {
110        let document = self.0.document;
111        super::Iter::new(document.lookup(self.0.id).arguments, document)
112    }
113    pub fn directives(&self) -> Iter<'a, Directive<'a>> {
114        let document = self.0.document;
115        super::Iter::new(document.lookup(self.0.id).directives, document)
116    }
117    pub fn selection_set(&self) -> Iter<'a, Selection<'a>> {
118        let document = self.0.document;
119        super::Iter::new(document.lookup(self.0.id).selection_set, document)
120    }
121    pub fn selection_set_span(&self) -> Option<Span> {
122        let document = self.0.document;
123        document.lookup(self.0.id).selection_set_span
124    }
125}
126
127impl FieldSelection<'_> {
128    pub fn id(&self) -> FieldSelectionId {
129        self.0.id
130    }
131}
132
133impl fmt::Debug for FieldSelection<'_> {
134    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
135        f.debug_struct("FieldSelection")
136            .field("alias", &self.alias())
137            .field("name", &self.name())
138            .field("arguments", &self.arguments())
139            .field("directives", &self.directives())
140            .field("selection_set", &self.selection_set())
141            .finish()
142    }
143}
144
145impl ExecutableId for FieldSelectionId {
146    type Reader<'a> = FieldSelection<'a>;
147    fn read(self, document: &ExecutableDocument) -> Self::Reader<'_> {
148        FieldSelection(ReadContext { id: self, document })
149    }
150}
151
152impl IdReader for FieldSelection<'_> {
153    type Id = FieldSelectionId;
154    type Reader<'a> = FieldSelection<'a>;
155    fn new(id: Self::Id, document: &'_ ExecutableDocument) -> Self::Reader<'_> {
156        document.read(id)
157    }
158}
159
160pub struct InlineFragmentRecord {
161    pub type_condition: Option<StringId>,
162    pub type_condition_span: Option<Span>,
163    pub directives: IdRange<DirectiveId>,
164    pub selection_set: IdRange<SelectionId>,
165    pub selection_set_span: Span,
166}
167
168#[derive(Clone, Copy)]
169pub struct InlineFragment<'a>(pub(in super::super) ReadContext<'a, InlineFragmentId>);
170
171impl<'a> InlineFragment<'a> {
172    pub fn type_condition(&self) -> Option<&'a str> {
173        let document = self.0.document;
174        document
175            .lookup(self.0.id)
176            .type_condition
177            .map(|id| document.lookup(id))
178    }
179    pub fn type_condition_span(&self) -> Option<Span> {
180        let document = self.0.document;
181        document.lookup(self.0.id).type_condition_span
182    }
183    pub fn directives(&self) -> Iter<'a, Directive<'a>> {
184        let document = self.0.document;
185        super::Iter::new(document.lookup(self.0.id).directives, document)
186    }
187    pub fn selection_set(&self) -> Iter<'a, Selection<'a>> {
188        let document = self.0.document;
189        super::Iter::new(document.lookup(self.0.id).selection_set, document)
190    }
191    pub fn selection_set_span(&self) -> Span {
192        let document = self.0.document;
193        document.lookup(self.0.id).selection_set_span
194    }
195}
196
197impl InlineFragment<'_> {
198    pub fn id(&self) -> InlineFragmentId {
199        self.0.id
200    }
201}
202
203impl fmt::Debug for InlineFragment<'_> {
204    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
205        f.debug_struct("InlineFragment")
206            .field("type_condition", &self.type_condition())
207            .field("directives", &self.directives())
208            .field("selection_set", &self.selection_set())
209            .finish()
210    }
211}
212
213impl ExecutableId for InlineFragmentId {
214    type Reader<'a> = InlineFragment<'a>;
215    fn read(self, document: &ExecutableDocument) -> Self::Reader<'_> {
216        InlineFragment(ReadContext { id: self, document })
217    }
218}
219
220impl IdReader for InlineFragment<'_> {
221    type Id = InlineFragmentId;
222    type Reader<'a> = InlineFragment<'a>;
223    fn new(id: Self::Id, document: &'_ ExecutableDocument) -> Self::Reader<'_> {
224        document.read(id)
225    }
226}
227
228pub struct FragmentSpreadRecord {
229    pub fragment_name: StringId,
230    pub fragment_name_span: Span,
231    pub directives: IdRange<DirectiveId>,
232}
233
234#[derive(Clone, Copy)]
235pub struct FragmentSpread<'a>(pub(in super::super) ReadContext<'a, FragmentSpreadId>);
236
237impl<'a> FragmentSpread<'a> {
238    pub fn fragment_name(&self) -> &'a str {
239        let document = &self.0.document;
240        document.lookup(document.lookup(self.0.id).fragment_name)
241    }
242    pub fn fragment_name_span(&self) -> Span {
243        let document = self.0.document;
244        document.lookup(self.0.id).fragment_name_span
245    }
246    pub fn directives(&self) -> Iter<'a, Directive<'a>> {
247        let document = self.0.document;
248        super::Iter::new(document.lookup(self.0.id).directives, document)
249    }
250}
251
252impl FragmentSpread<'_> {
253    pub fn id(&self) -> FragmentSpreadId {
254        self.0.id
255    }
256}
257
258impl fmt::Debug for FragmentSpread<'_> {
259    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
260        f.debug_struct("FragmentSpread")
261            .field("fragment_name", &self.fragment_name())
262            .field("directives", &self.directives())
263            .finish()
264    }
265}
266
267impl ExecutableId for FragmentSpreadId {
268    type Reader<'a> = FragmentSpread<'a>;
269    fn read(self, document: &ExecutableDocument) -> Self::Reader<'_> {
270        FragmentSpread(ReadContext { id: self, document })
271    }
272}
273
274impl IdReader for FragmentSpread<'_> {
275    type Id = FragmentSpreadId;
276    type Reader<'a> = FragmentSpread<'a>;
277    fn new(id: Self::Id, document: &'_ ExecutableDocument) -> Self::Reader<'_> {
278        document.read(id)
279    }
280}