bluejay_parser/ast/executable/
inline_fragment.rs

1use crate::ast::executable::{SelectionSet, TypeCondition};
2use crate::ast::{FromTokens, IsMatch, ParseError, Tokens, TryFromTokens, VariableDirectives};
3use crate::lexical_token::PunctuatorType;
4use crate::{HasSpan, Span};
5
6#[derive(Debug)]
7pub struct InlineFragment<'a> {
8    type_condition: Option<TypeCondition<'a>>,
9    directives: Option<VariableDirectives<'a>>,
10    selection_set: SelectionSet<'a>,
11    span: Span,
12}
13
14impl<'a> FromTokens<'a> for InlineFragment<'a> {
15    #[inline]
16    fn from_tokens(tokens: &mut impl Tokens<'a>) -> Result<Self, ParseError> {
17        let ellipse_span = tokens.expect_punctuator(PunctuatorType::Ellipse)?;
18        let type_condition = TypeCondition::try_from_tokens(tokens).transpose()?;
19        let directives = VariableDirectives::try_from_tokens(tokens).transpose()?;
20        let selection_set = SelectionSet::from_tokens(tokens)?;
21        let span = ellipse_span.merge(selection_set.span());
22        Ok(Self {
23            type_condition,
24            directives,
25            selection_set,
26            span,
27        })
28    }
29}
30
31impl<'a> IsMatch<'a> for InlineFragment<'a> {
32    #[inline]
33    fn is_match(tokens: &mut impl Tokens<'a>) -> bool {
34        tokens.peek_punctuator_matches(0, PunctuatorType::Ellipse)
35            && tokens
36                .peek_name(1)
37                .map(|n| n.as_ref() == TypeCondition::ON)
38                .unwrap_or(true)
39    }
40}
41
42impl<'a> InlineFragment<'a> {
43    pub fn type_condition(&self) -> Option<&TypeCondition<'a>> {
44        self.type_condition.as_ref()
45    }
46
47    pub fn selection_set(&self) -> &SelectionSet<'a> {
48        &self.selection_set
49    }
50}
51
52impl<'a> bluejay_core::executable::InlineFragment for InlineFragment<'a> {
53    type Directives = VariableDirectives<'a>;
54    type SelectionSet = SelectionSet<'a>;
55
56    fn type_condition(&self) -> Option<&str> {
57        self.type_condition
58            .as_ref()
59            .map(|tc| tc.named_type().as_ref())
60    }
61
62    fn directives(&self) -> Option<&Self::Directives> {
63        self.directives.as_ref()
64    }
65
66    fn selection_set(&self) -> &Self::SelectionSet {
67        &self.selection_set
68    }
69}
70
71impl<'a> HasSpan for InlineFragment<'a> {
72    fn span(&self) -> &Span {
73        &self.span
74    }
75}