1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use crate::ast::definition::InputValueDefinition;
use crate::ast::{FromTokens, IsMatch, ParseError, Tokens};
use crate::lexical_token::PunctuatorType;
use crate::Span;
use bluejay_core::definition::ArgumentsDefinition as CoreArgumentsDefinition;
use bluejay_core::AsIter;

#[derive(Debug)]
pub struct ArgumentsDefinition<'a> {
    argument_definitions: Vec<InputValueDefinition<'a>>,
    _span: Span,
}

impl<'a> AsIter for ArgumentsDefinition<'a> {
    type Item = InputValueDefinition<'a>;
    type Iterator<'b> = std::slice::Iter<'b, Self::Item> where 'a: 'b;

    fn iter(&self) -> Self::Iterator<'_> {
        self.argument_definitions.iter()
    }
}

impl<'a> CoreArgumentsDefinition for ArgumentsDefinition<'a> {
    type ArgumentDefinition = InputValueDefinition<'a>;
}

impl<'a> FromTokens<'a> for ArgumentsDefinition<'a> {
    fn from_tokens(tokens: &mut impl Tokens<'a>) -> Result<Self, ParseError> {
        let open_span = tokens.expect_punctuator(PunctuatorType::OpenRoundBracket)?;
        let mut argument_definitions: Vec<InputValueDefinition> = Vec::new();
        let close_span = loop {
            argument_definitions.push(InputValueDefinition::from_tokens(tokens)?);
            if let Some(close_span) = tokens.next_if_punctuator(PunctuatorType::CloseRoundBracket) {
                break close_span;
            }
        };
        let span = open_span.merge(&close_span);
        Ok(Self {
            argument_definitions,
            _span: span,
        })
    }
}

impl<'a> IsMatch<'a> for ArgumentsDefinition<'a> {
    fn is_match(tokens: &mut impl Tokens<'a>) -> bool {
        tokens.peek_punctuator_matches(0, PunctuatorType::OpenRoundBracket)
    }
}