bluejay_parser/ast/executable/
selection.rs1use crate::ast::executable::{Field, FragmentSpread, InlineFragment};
2use crate::ast::{FromTokens, IsMatch, ParseError, Tokens};
3use crate::lexical_token::PunctuatorType;
4use bluejay_core::executable::{Selection as CoreSelection, SelectionReference};
5
6#[derive(Debug)]
7pub enum Selection<'a> {
8 Field(Field<'a>),
9 FragmentSpread(FragmentSpread<'a>),
10 InlineFragment(InlineFragment<'a>),
11}
12
13impl<'a> CoreSelection for Selection<'a> {
14 type Field = Field<'a>;
15 type FragmentSpread = FragmentSpread<'a>;
16 type InlineFragment = InlineFragment<'a>;
17
18 fn as_ref(&self) -> SelectionReference<'_, Self> {
19 match self {
20 Self::Field(f) => SelectionReference::Field(f),
21 Self::FragmentSpread(fs) => SelectionReference::FragmentSpread(fs),
22 Self::InlineFragment(i) => SelectionReference::InlineFragment(i),
23 }
24 }
25}
26
27impl<'a> FromTokens<'a> for Selection<'a> {
28 #[inline]
29 fn from_tokens(tokens: &mut impl Tokens<'a>) -> Result<Self, ParseError> {
30 if Field::is_match(tokens) {
31 Field::from_tokens(tokens).map(Self::Field)
32 } else if FragmentSpread::is_match(tokens) {
33 FragmentSpread::from_tokens(tokens).map(Self::FragmentSpread)
34 } else if InlineFragment::is_match(tokens) {
35 InlineFragment::from_tokens(tokens).map(Self::InlineFragment)
36 } else {
37 Err(tokens.unexpected_token())
38 }
39 }
40}
41
42impl<'a> IsMatch<'a> for Selection<'a> {
43 #[inline]
44 fn is_match(tokens: &mut impl Tokens<'a>) -> bool {
45 Field::is_match(tokens) || tokens.peek_punctuator_matches(0, PunctuatorType::Ellipse)
46 }
47}