bluejay_parser/ast/executable/
fragment_definition.rs1use crate::ast::executable::{SelectionSet, TypeCondition};
2use crate::ast::try_from_tokens::TryFromTokens;
3use crate::ast::{DepthLimiter, FromTokens, IsMatch, ParseError, Tokens, VariableDirectives};
4use crate::lexical_token::Name;
5use crate::{HasSpan, Span};
6
7#[derive(Debug)]
8pub struct FragmentDefinition<'a> {
9 name: Name<'a>,
10 type_condition: TypeCondition<'a>,
11 directives: Option<VariableDirectives<'a>>,
12 selection_set: SelectionSet<'a>,
13 span: Span,
14}
15
16impl<'a> IsMatch<'a> for FragmentDefinition<'a> {
17 #[inline]
18 fn is_match(tokens: &mut impl Tokens<'a>) -> bool {
19 tokens.peek_name_matches(0, "fragment")
20 }
21}
22
23impl<'a> FromTokens<'a> for FragmentDefinition<'a> {
24 #[inline]
25 fn from_tokens(
26 tokens: &mut impl Tokens<'a>,
27 depth_limiter: DepthLimiter,
28 ) -> Result<Self, ParseError> {
29 let fragment_identifier_span = tokens.expect_name_value("fragment")?;
30 let name = tokens.expect_name()?;
31 if name.as_ref() == TypeCondition::ON {
32 return Err(ParseError::UnexpectedToken { span: name.into() });
34 }
35 let type_condition = TypeCondition::from_tokens(tokens, depth_limiter.bump()?)?;
36 let directives =
37 VariableDirectives::try_from_tokens(tokens, depth_limiter.bump()?).transpose()?;
38 let selection_set = SelectionSet::from_tokens(tokens, depth_limiter.bump()?)?;
39 let span = fragment_identifier_span.merge(selection_set.span());
40 Ok(Self {
41 name,
42 type_condition,
43 directives,
44 selection_set,
45 span,
46 })
47 }
48}
49
50impl<'a> FragmentDefinition<'a> {
51 pub fn name(&self) -> &Name<'a> {
52 &self.name
53 }
54
55 pub fn type_condition(&self) -> &TypeCondition<'a> {
56 &self.type_condition
57 }
58
59 pub fn selection_set(&self) -> &SelectionSet {
60 &self.selection_set
61 }
62}
63
64impl bluejay_core::Indexable for FragmentDefinition<'_> {
65 type Id = Span;
66
67 fn id(&self) -> &Self::Id {
68 &self.span
69 }
70}
71
72impl<'a> bluejay_core::executable::FragmentDefinition for FragmentDefinition<'a> {
73 type Directives = VariableDirectives<'a>;
74 type SelectionSet = SelectionSet<'a>;
75
76 fn name(&self) -> &str {
77 self.name.as_ref()
78 }
79
80 fn type_condition(&self) -> &str {
81 self.type_condition.named_type().as_ref()
82 }
83
84 fn directives(&self) -> Option<&Self::Directives> {
85 self.directives.as_ref()
86 }
87
88 fn selection_set(&self) -> &Self::SelectionSet {
89 &self.selection_set
90 }
91}
92
93impl HasSpan for FragmentDefinition<'_> {
94 fn span(&self) -> &Span {
95 &self.span
96 }
97}