use crate::ast::executable::{SelectionSet, TypeCondition};
use crate::ast::{FromTokens, IsMatch, ParseError, Tokens, VariableDirectives};
use crate::lexical_token::Name;
use crate::{HasSpan, Span};
use std::cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd};
use std::hash::{Hash, Hasher};
#[derive(Debug)]
pub struct FragmentDefinition<'a> {
name: Name<'a>,
type_condition: TypeCondition<'a>,
directives: VariableDirectives<'a>,
selection_set: SelectionSet<'a>,
span: Span,
}
impl<'a> IsMatch<'a> for FragmentDefinition<'a> {
fn is_match(tokens: &mut impl Tokens<'a>) -> bool {
tokens.peek_name_matches(0, "fragment")
}
}
impl<'a> FromTokens<'a> for FragmentDefinition<'a> {
fn from_tokens(tokens: &mut impl Tokens<'a>) -> Result<Self, ParseError> {
let fragment_identifier_span = tokens.expect_name_value("fragment")?;
let name = tokens.expect_name()?;
if name.as_ref() == TypeCondition::ON {
return Err(ParseError::UnexpectedToken { span: name.into() });
}
let type_condition = TypeCondition::from_tokens(tokens)?;
let directives = VariableDirectives::from_tokens(tokens)?;
let selection_set = SelectionSet::from_tokens(tokens)?;
let span = fragment_identifier_span.merge(selection_set.span());
Ok(Self {
name,
type_condition,
directives,
selection_set,
span,
})
}
}
impl<'a> FragmentDefinition<'a> {
pub fn name(&self) -> &Name<'a> {
&self.name
}
pub fn type_condition(&self) -> &TypeCondition<'a> {
&self.type_condition
}
pub fn selection_set(&self) -> &SelectionSet {
&self.selection_set
}
}
impl<'a> bluejay_core::executable::FragmentDefinition for FragmentDefinition<'a> {
type Directives = VariableDirectives<'a>;
type SelectionSet = SelectionSet<'a>;
fn name(&self) -> &str {
self.name.as_ref()
}
fn type_condition(&self) -> &str {
self.type_condition.named_type().as_ref()
}
fn directives(&self) -> &Self::Directives {
&self.directives
}
fn selection_set(&self) -> &Self::SelectionSet {
&self.selection_set
}
}
impl<'a> HasSpan for FragmentDefinition<'a> {
fn span(&self) -> &Span {
&self.span
}
}
impl<'a> Hash for FragmentDefinition<'a> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.span().hash(state);
}
}
impl<'a> PartialEq for FragmentDefinition<'a> {
fn eq(&self, other: &Self) -> bool {
self.span() == other.span()
}
}
impl<'a> Eq for FragmentDefinition<'a> {}
impl<'a> Ord for FragmentDefinition<'a> {
fn cmp(&self, other: &Self) -> Ordering {
self.span().cmp(other.span())
}
}
impl<'a> PartialOrd for FragmentDefinition<'a> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}