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
51
52
53
54
55
56
57
58
59
60
61
62
63
use crate::{
    ast::{Directive, FromTokens, IsMatch, ParseError, Tokens, TryFromTokens},
    HasSpan, Span,
};
use bluejay_core::AsIter;

#[derive(Debug)]
pub struct Directives<'a, const CONST: bool> {
    directives: Vec<Directive<'a, CONST>>,
    span: Option<Span>,
}

pub type ConstDirectives<'a> = Directives<'a, true>;
pub type VariableDirectives<'a> = Directives<'a, false>;

impl<'a, const CONST: bool> FromTokens<'a> for Directives<'a, CONST> {
    fn from_tokens(tokens: &mut impl Tokens<'a>) -> Result<Self, ParseError> {
        let mut directives: Vec<Directive<'a, CONST>> = Vec::new();
        while let Some(directive) = Directive::try_from_tokens(tokens) {
            directives.push(directive?);
        }
        let span = match directives.as_slice() {
            [] => None,
            [first] => Some(first.span().clone()),
            [first, .., last] => Some(first.span().merge(last.span())),
        };
        Ok(Self { directives, span })
    }
}

impl<'a, const CONST: bool> IsMatch<'a> for Directives<'a, CONST> {
    fn is_match(tokens: &mut impl Tokens<'a>) -> bool {
        Directive::<'a, CONST>::is_match(tokens)
    }
}

impl<'a, const CONST: bool> bluejay_core::Directives<CONST> for Directives<'a, CONST> {
    type Directive = Directive<'a, CONST>;
}

impl<'a, const CONST: bool> AsIter for Directives<'a, CONST> {
    type Item = Directive<'a, CONST>;
    type Iterator<'b> = std::slice::Iter<'b, Self::Item> where 'a: 'b;

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

impl<'a, const CONST: bool> Directives<'a, CONST> {
    pub(crate) fn span(&self) -> Option<&Span> {
        self.span.as_ref()
    }
}

impl<'a, const CONST: bool> IntoIterator for Directives<'a, CONST> {
    type Item = Directive<'a, CONST>;
    type IntoIter = std::vec::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        self.directives.into_iter()
    }
}