bluejay_parser/ast/executable/
inline_fragment.rs

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