libgraphql_parser/ast/
selection.rs1use crate::ast::AstNode;
2use crate::ast::DirectiveAnnotation;
3use crate::ast::FieldSelection;
4use crate::ast::FragmentSpread;
5use crate::ast::InlineFragment;
6use crate::ast::Name;
7use crate::ByteSpan;
8use crate::SourceMap;
9use crate::SourceSpan;
10use inherent::inherent;
11
12#[derive(Clone, Debug, PartialEq)]
18pub enum Selection<'src> {
19 Field(FieldSelection<'src>),
20 FragmentSpread(FragmentSpread<'src>),
21 InlineFragment(InlineFragment<'src>),
22}
23
24impl<'src> Selection<'src> {
25 pub fn directive_annotations(&self) -> &[DirectiveAnnotation<'src>] {
27 match self {
28 Self::Field(s) => &s.directives,
29 Self::FragmentSpread(s) => &s.directives,
30 Self::InlineFragment(s) => &s.directives,
31 }
32 }
33
34 pub fn name(&self) -> Option<&Name<'src>> {
37 match self {
38 Self::Field(s) => Some(&s.name),
39 Self::FragmentSpread(s) => Some(&s.name),
40 Self::InlineFragment(_) => None,
41 }
42 }
43
44 pub fn name_value(&self) -> Option<&str> {
49 self.name().map(|n| n.value.as_ref())
50 }
51}
52
53#[inherent]
54impl AstNode for Selection<'_> {
55 pub fn append_source(
57 &self,
58 sink: &mut String,
59 source: Option<&str>,
60 ) {
61 match self {
62 Selection::Field(s) => {
63 s.append_source(sink, source)
64 },
65 Selection::FragmentSpread(s) => {
66 s.append_source(sink, source)
67 },
68 Selection::InlineFragment(s) => {
69 s.append_source(sink, source)
70 },
71 }
72 }
73
74 pub fn byte_span(&self) -> ByteSpan {
81 match self {
82 Self::Field(s) => s.span,
83 Self::FragmentSpread(s) => s.span,
84 Self::InlineFragment(s) => s.span,
85 }
86 }
87
88 pub fn source_span(
95 &self,
96 source_map: &SourceMap,
97 ) -> Option<SourceSpan> {
98 self.byte_span().resolve(source_map)
99 }
100}