bluejay-parser 0.4.0

A GraphQL parser
Documentation
use crate::ast::executable::SelectionSet;
use crate::ast::{
    DepthLimiter, FromTokens, IsMatch, ParseError, Tokens, VariableArguments, VariableDirectives,
};
use crate::lexical_token::{Name, PunctuatorType};
use crate::{HasSpan, Span};
use std::cmp::{Eq, PartialEq};
use std::hash::{Hash, Hasher};

#[derive(Debug)]
pub struct Field<'a> {
    alias: Option<Name<'a>>,
    name: Name<'a>,
    arguments: Option<VariableArguments<'a>>,
    directives: Option<VariableDirectives<'a>>,
    selection_set: Option<SelectionSet<'a>>,
    span: Span,
}

impl<'a> FromTokens<'a> for Field<'a> {
    #[inline]
    fn from_tokens(
        tokens: &mut impl Tokens<'a>,
        depth_limiter: DepthLimiter,
    ) -> Result<Self, ParseError> {
        // Consume the first name, then check if followed by `:` (alias syntax).
        // This avoids the peek(1) that requires buffering 2 tokens.
        let first_name = tokens.expect_name()?;
        let (alias, name) = if tokens.next_if_punctuator(PunctuatorType::Colon).is_some() {
            let name = tokens.expect_name()?;
            (Some(first_name), name)
        } else {
            (None, first_name)
        };
        let arguments = if VariableArguments::is_match(tokens) {
            Some(VariableArguments::from_tokens(
                tokens,
                depth_limiter.bump()?,
            )?)
        } else {
            None
        };
        let directives = if VariableDirectives::is_match(tokens) {
            Some(VariableDirectives::from_tokens(
                tokens,
                depth_limiter.bump()?,
            )?)
        } else {
            None
        };
        let selection_set = if SelectionSet::is_match(tokens) {
            Some(SelectionSet::from_tokens(tokens, depth_limiter.bump()?)?)
        } else {
            None
        };
        let start_span = alias.as_ref().unwrap_or(&name).span();
        let directives_span = directives.as_ref().and_then(|directives| directives.span());
        let end_span = if let Some(selection_set) = &selection_set {
            selection_set.span()
        } else if let Some(directive_span) = directives_span {
            directive_span
        } else if let Some(arguments) = &arguments {
            arguments.span()
        } else {
            name.span()
        };
        let span = start_span.merge(end_span);
        Ok(Self {
            alias,
            name,
            arguments,
            directives,
            selection_set,
            span,
        })
    }
}

impl<'a> IsMatch<'a> for Field<'a> {
    #[inline]
    fn is_match(tokens: &mut impl Tokens<'a>) -> bool {
        tokens.peek_name(0).is_some()
    }
}

impl<'a> Field<'a> {
    pub fn response_key(&self) -> &str {
        if let Some(alias) = &self.alias {
            alias.as_ref()
        } else {
            self.name.as_ref()
        }
    }

    pub fn name(&self) -> &Name<'a> {
        &self.name
    }

    pub fn arguments(&self) -> Option<&VariableArguments<'_>> {
        self.arguments.as_ref()
    }

    pub fn selection_set(&self) -> Option<&SelectionSet<'_>> {
        self.selection_set.as_ref()
    }
}

impl<'a> bluejay_core::executable::Field for Field<'a> {
    type Arguments = VariableArguments<'a>;
    type Directives = VariableDirectives<'a>;
    type SelectionSet = SelectionSet<'a>;

    fn alias(&self) -> Option<&str> {
        self.alias.as_ref().map(|name| name.as_ref())
    }

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

    fn arguments(&self) -> Option<&Self::Arguments> {
        self.arguments.as_ref()
    }

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

    fn selection_set(&self) -> Option<&Self::SelectionSet> {
        self.selection_set.as_ref()
    }
}

impl HasSpan for Field<'_> {
    fn span(&self) -> &Span {
        &self.span
    }
}

impl Hash for Field<'_> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.span().hash(state);
    }
}

impl PartialEq for Field<'_> {
    fn eq(&self, other: &Self) -> bool {
        self.span() == other.span()
    }
}

impl Eq for Field<'_> {}