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
50
use crate::ast::definition::{Context, InterfaceImplementation};
use crate::ast::{FromTokens, IsMatch, ParseError, Tokens};
use crate::lexical_token::PunctuatorType;
use bluejay_core::definition::InterfaceImplementations as CoreInterfaceImplementations;
use bluejay_core::AsIter;

#[derive(Debug)]
pub struct InterfaceImplementations<'a, C: Context + 'a> {
    interface_implementations: Vec<InterfaceImplementation<'a, C>>,
}

impl<'a, C: Context + 'a> AsIter for InterfaceImplementations<'a, C> {
    type Item = InterfaceImplementation<'a, C>;
    type Iterator<'b> = std::slice::Iter<'b, Self::Item> where 'a: 'b;

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

impl<'a, C: Context + 'a> CoreInterfaceImplementations for InterfaceImplementations<'a, C> {
    type InterfaceImplementation = InterfaceImplementation<'a, C>;
}

impl<'a, C: Context + 'a> InterfaceImplementations<'a, C> {
    const IMPLEMENTS_IDENTIFIER: &'static str = "implements";
}

impl<'a, C: Context + 'a> FromTokens<'a> for InterfaceImplementations<'a, C> {
    fn from_tokens(tokens: &mut impl Tokens<'a>) -> Result<Self, ParseError> {
        tokens.expect_name_value(Self::IMPLEMENTS_IDENTIFIER)?;
        tokens.next_if_punctuator(PunctuatorType::Ampersand);
        let mut interface_implementations = vec![InterfaceImplementation::from_tokens(tokens)?];
        while tokens
            .next_if_punctuator(PunctuatorType::Ampersand)
            .is_some()
        {
            interface_implementations.push(InterfaceImplementation::from_tokens(tokens)?);
        }
        Ok(Self {
            interface_implementations,
        })
    }
}

impl<'a, C: Context + 'a> IsMatch<'a> for InterfaceImplementations<'a, C> {
    fn is_match(tokens: &mut impl Tokens<'a>) -> bool {
        tokens.peek_name_matches(0, Self::IMPLEMENTS_IDENTIFIER)
    }
}