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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use crate::ast::executable::Selection;
use crate::ast::{FromTokens, IsMatch, ParseError, Tokens};
use crate::lexical_token::PunctuatorType;
use crate::{HasSpan, Span};
use bluejay_core::AsIter;
use std::cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd};
use std::hash::{Hash, Hasher};

#[derive(Debug)]
pub struct SelectionSet<'a> {
    selections: Vec<Selection<'a>>,
    span: Span,
}

impl<'a> FromTokens<'a> for SelectionSet<'a> {
    fn from_tokens(tokens: &mut impl Tokens<'a>) -> Result<Self, ParseError> {
        let open_span = tokens.expect_punctuator(PunctuatorType::OpenBrace)?;
        let mut selections: Vec<Selection> = Vec::new();
        let close_span = loop {
            selections.push(Selection::from_tokens(tokens)?);
            if let Some(close_span) = tokens.next_if_punctuator(PunctuatorType::CloseBrace) {
                break close_span;
            }
        };
        let span = open_span.merge(&close_span);
        Ok(Self { selections, span })
    }
}

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

impl<'a> bluejay_core::executable::SelectionSet for SelectionSet<'a> {
    type Selection = Selection<'a>;
}

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

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

    fn len(&self) -> usize {
        self.selections.len()
    }
}

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

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

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

impl<'a> Eq for SelectionSet<'a> {}

impl<'a> Ord for SelectionSet<'a> {
    fn cmp(&self, other: &Self) -> Ordering {
        self.span.cmp(&other.span)
    }
}

impl<'a> PartialOrd for SelectionSet<'a> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}