Skip to main content

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 = VariableDirectives::try_from_tokens(tokens, depth_limiter.bump()?)?;
24        let span = ellipse_span.merge(name.span());
25        Ok(Self {
26            name,
27            directives,
28            span,
29        })
30    }
31}
32
33impl<'a> IsMatch<'a> for FragmentSpread<'a> {
34    #[inline]
35    fn is_match(tokens: &mut impl Tokens<'a>) -> bool {
36        tokens.peek_punctuator_matches(0, PunctuatorType::Ellipse)
37            && tokens
38                .peek_name(1)
39                .map(|n| n.as_ref() != TypeCondition::ON)
40                .unwrap_or(false)
41    }
42}
43
44impl<'a> FragmentSpread<'a> {
45    pub fn name(&self) -> &Name<'a> {
46        &self.name
47    }
48}
49
50impl<'a> bluejay_core::executable::FragmentSpread for FragmentSpread<'a> {
51    type Directives = VariableDirectives<'a>;
52
53    fn name(&self) -> &str {
54        self.name.as_ref()
55    }
56
57    fn directives(&self) -> Option<&Self::Directives> {
58        self.directives.as_ref()
59    }
60}
61
62impl HasSpan for FragmentSpread<'_> {
63    fn span(&self) -> &Span {
64        &self.span
65    }
66}