1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use crate::ast::executable::TypeCondition;
use crate::ast::{FromTokens, IsMatch, ParseError, Tokens, VariableDirectives};
use crate::lexical_token::{Name, PunctuatorType};

#[derive(Debug)]
pub struct FragmentSpread<'a> {
    name: Name<'a>,
    directives: VariableDirectives<'a>,
}

impl<'a> FromTokens<'a> for FragmentSpread<'a> {
    fn from_tokens(tokens: &mut impl Tokens<'a>) -> Result<Self, ParseError> {
        tokens.expect_punctuator(PunctuatorType::Ellipse)?;
        let name = tokens.expect_name()?;
        assert_ne!(TypeCondition::ON, name.as_ref());
        let directives = VariableDirectives::from_tokens(tokens)?;
        Ok(Self { name, directives })
    }
}

impl<'a> IsMatch<'a> for FragmentSpread<'a> {
    fn is_match(tokens: &mut impl Tokens<'a>) -> bool {
        tokens.peek_punctuator_matches(0, PunctuatorType::Ellipse)
            && tokens
                .peek_name(1)
                .map(|n| n.as_ref() != TypeCondition::ON)
                .unwrap_or(false)
    }
}

impl<'a> FragmentSpread<'a> {
    pub fn name(&self) -> &str {
        self.name.as_ref()
    }
}

impl<'a> bluejay_core::executable::FragmentSpread for FragmentSpread<'a> {
    type Directives = VariableDirectives<'a>;

    fn name(&self) -> &str {
        self.name.as_ref()
    }

    fn directives(&self) -> &Self::Directives {
        &self.directives
    }
}