cynic_parser/executable/generated/
selections.rs

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