bluejay_parser/ast/executable/
fragment_spread.rs

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