bluejay_parser/ast/executable/
field.rs

1use crate::ast::executable::SelectionSet;
2use crate::ast::{
3    FromTokens, IsMatch, ParseError, Tokens, TryFromTokens, VariableArguments, VariableDirectives,
4};
5use crate::lexical_token::{Name, PunctuatorType};
6use crate::{HasSpan, Span};
7use std::cmp::{Eq, PartialEq};
8use std::hash::{Hash, Hasher};
9
10#[derive(Debug)]
11pub struct Field<'a> {
12    alias: Option<Name<'a>>,
13    name: Name<'a>,
14    arguments: Option<VariableArguments<'a>>,
15    directives: Option<VariableDirectives<'a>>,
16    selection_set: Option<SelectionSet<'a>>,
17    span: Span,
18}
19
20impl<'a> FromTokens<'a> for Field<'a> {
21    #[inline]
22    fn from_tokens(tokens: &mut impl Tokens<'a>) -> Result<Self, ParseError> {
23        let has_alias = tokens.peek_punctuator_matches(1, PunctuatorType::Colon);
24        let (alias, name) = if has_alias {
25            let alias = Some(tokens.expect_name()?);
26            tokens.expect_punctuator(PunctuatorType::Colon)?;
27            let name = tokens.expect_name()?;
28            (alias, name)
29        } else {
30            (None, tokens.expect_name()?)
31        };
32        let arguments = VariableArguments::try_from_tokens(tokens).transpose()?;
33        let directives = VariableDirectives::try_from_tokens(tokens).transpose()?;
34        let selection_set = SelectionSet::try_from_tokens(tokens).transpose()?;
35        let start_span = alias.as_ref().unwrap_or(&name).span();
36        let directives_span = directives.as_ref().and_then(|directives| directives.span());
37        let end_span = if let Some(selection_set) = &selection_set {
38            selection_set.span()
39        } else if let Some(directive_span) = directives_span {
40            directive_span
41        } else if let Some(arguments) = &arguments {
42            arguments.span()
43        } else {
44            name.span()
45        };
46        let span = start_span.merge(end_span);
47        Ok(Self {
48            alias,
49            name,
50            arguments,
51            directives,
52            selection_set,
53            span,
54        })
55    }
56}
57
58impl<'a> IsMatch<'a> for Field<'a> {
59    #[inline]
60    fn is_match(tokens: &mut impl Tokens<'a>) -> bool {
61        tokens.peek_name(0).is_some()
62    }
63}
64
65impl<'a> Field<'a> {
66    pub fn response_key(&self) -> &str {
67        if let Some(alias) = &self.alias {
68            alias.as_ref()
69        } else {
70            self.name.as_ref()
71        }
72    }
73
74    pub fn name(&self) -> &Name<'a> {
75        &self.name
76    }
77
78    pub fn arguments(&self) -> Option<&VariableArguments> {
79        self.arguments.as_ref()
80    }
81
82    pub fn selection_set(&self) -> Option<&SelectionSet> {
83        self.selection_set.as_ref()
84    }
85}
86
87impl<'a> bluejay_core::executable::Field for Field<'a> {
88    type Arguments = VariableArguments<'a>;
89    type Directives = VariableDirectives<'a>;
90    type SelectionSet = SelectionSet<'a>;
91
92    fn alias(&self) -> Option<&str> {
93        self.alias.as_ref().map(|name| name.as_ref())
94    }
95
96    fn name(&self) -> &str {
97        self.name.as_ref()
98    }
99
100    fn arguments(&self) -> Option<&Self::Arguments> {
101        self.arguments.as_ref()
102    }
103
104    fn directives(&self) -> Option<&Self::Directives> {
105        self.directives.as_ref()
106    }
107
108    fn selection_set(&self) -> Option<&Self::SelectionSet> {
109        self.selection_set.as_ref()
110    }
111}
112
113impl<'a> HasSpan for Field<'a> {
114    fn span(&self) -> &Span {
115        &self.span
116    }
117}
118
119impl<'a> Hash for Field<'a> {
120    fn hash<H: Hasher>(&self, state: &mut H) {
121        self.span().hash(state);
122    }
123}
124
125impl<'a> PartialEq for Field<'a> {
126    fn eq(&self, other: &Self) -> bool {
127        self.span() == other.span()
128    }
129}
130
131impl<'a> Eq for Field<'a> {}