bluejay_parser/ast/executable/
fragment_definition.rs1use crate::ast::executable::{SelectionSet, TypeCondition};
2use crate::ast::try_from_tokens::TryFromTokens;
3use crate::ast::{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(tokens: &mut impl Tokens<'a>) -> Result<Self, ParseError> {
26 let fragment_identifier_span = tokens.expect_name_value("fragment")?;
27 let name = tokens.expect_name()?;
28 if name.as_ref() == TypeCondition::ON {
29 return Err(ParseError::UnexpectedToken { span: name.into() });
31 }
32 let type_condition = TypeCondition::from_tokens(tokens)?;
33 let directives = VariableDirectives::try_from_tokens(tokens).transpose()?;
34 let selection_set = SelectionSet::from_tokens(tokens)?;
35 let span = fragment_identifier_span.merge(selection_set.span());
36 Ok(Self {
37 name,
38 type_condition,
39 directives,
40 selection_set,
41 span,
42 })
43 }
44}
45
46impl<'a> FragmentDefinition<'a> {
47 pub fn name(&self) -> &Name<'a> {
48 &self.name
49 }
50
51 pub fn type_condition(&self) -> &TypeCondition<'a> {
52 &self.type_condition
53 }
54
55 pub fn selection_set(&self) -> &SelectionSet {
56 &self.selection_set
57 }
58}
59
60impl<'a> bluejay_core::Indexable for FragmentDefinition<'a> {
61 type Id = Span;
62
63 fn id(&self) -> &Self::Id {
64 &self.span
65 }
66}
67
68impl<'a> bluejay_core::executable::FragmentDefinition for FragmentDefinition<'a> {
69 type Directives = VariableDirectives<'a>;
70 type SelectionSet = SelectionSet<'a>;
71
72 fn name(&self) -> &str {
73 self.name.as_ref()
74 }
75
76 fn type_condition(&self) -> &str {
77 self.type_condition.named_type().as_ref()
78 }
79
80 fn directives(&self) -> Option<&Self::Directives> {
81 self.directives.as_ref()
82 }
83
84 fn selection_set(&self) -> &Self::SelectionSet {
85 &self.selection_set
86 }
87}
88
89impl<'a> HasSpan for FragmentDefinition<'a> {
90 fn span(&self) -> &Span {
91 &self.span
92 }
93}