bluejay_parser/ast/definition/
arguments_definition.rs

1use crate::ast::definition::{Context, InputValueDefinition};
2use crate::ast::{DepthLimiter, FromTokens, IsMatch, ParseError, Tokens};
3use crate::lexical_token::PunctuatorType;
4use crate::Span;
5use bluejay_core::definition::ArgumentsDefinition as CoreArgumentsDefinition;
6use bluejay_core::AsIter;
7
8#[derive(Debug)]
9pub struct ArgumentsDefinition<'a, C: Context> {
10    argument_definitions: Vec<InputValueDefinition<'a, C>>,
11    _span: Span,
12}
13
14impl<'a, C: Context> AsIter for ArgumentsDefinition<'a, C> {
15    type Item = InputValueDefinition<'a, C>;
16    type Iterator<'b>
17        = std::slice::Iter<'b, Self::Item>
18    where
19        'a: 'b;
20
21    fn iter(&self) -> Self::Iterator<'_> {
22        self.argument_definitions.iter()
23    }
24}
25
26impl<'a, C: Context> CoreArgumentsDefinition for ArgumentsDefinition<'a, C> {
27    type ArgumentDefinition = InputValueDefinition<'a, C>;
28}
29
30impl<'a, C: Context> FromTokens<'a> for ArgumentsDefinition<'a, C> {
31    fn from_tokens(
32        tokens: &mut impl Tokens<'a>,
33        depth_limiter: DepthLimiter,
34    ) -> Result<Self, ParseError> {
35        let open_span = tokens.expect_punctuator(PunctuatorType::OpenRoundBracket)?;
36        let mut argument_definitions: Vec<InputValueDefinition<C>> = Vec::new();
37        let close_span = loop {
38            argument_definitions.push(InputValueDefinition::from_tokens(
39                tokens,
40                depth_limiter.bump()?,
41            )?);
42            if let Some(close_span) = tokens.next_if_punctuator(PunctuatorType::CloseRoundBracket) {
43                break close_span;
44            }
45        };
46        let span = open_span.merge(&close_span);
47        Ok(Self {
48            argument_definitions,
49            _span: span,
50        })
51    }
52}
53
54impl<'a, C: Context> IsMatch<'a> for ArgumentsDefinition<'a, C> {
55    fn is_match(tokens: &mut impl Tokens<'a>) -> bool {
56        tokens.peek_punctuator_matches(0, PunctuatorType::OpenRoundBracket)
57    }
58}