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
use crate::executable::{Cache, Error, Path, Rule, Visitor};
use bluejay_core::definition::{BaseOutputType, FieldDefinition, OutputType, SchemaDefinition};
use bluejay_core::executable::{ExecutableDocument, Field};

pub struct LeafFieldSelections<'a, E: ExecutableDocument, S: SchemaDefinition> {
    errors: Vec<Error<'a, E, S>>,
}

impl<'a, E: ExecutableDocument + 'a, S: SchemaDefinition + 'a> Visitor<'a, E, S>
    for LeafFieldSelections<'a, E, S>
{
    fn visit_field(
        &mut self,
        field: &'a <E as ExecutableDocument>::Field,
        field_definition: &'a S::FieldDefinition,
        _: &Path<'a, E>,
    ) {
        let r#type = field_definition.r#type();
        if r#type.as_ref().base().as_ref().is_scalar_or_enum() {
            if let Some(selection_set) = field.selection_set() {
                self.errors.push(Error::LeafFieldSelectionNotEmpty {
                    selection_set,
                    r#type,
                });
            }
        } else if field.selection_set().is_none() {
            self.errors
                .push(Error::NonLeafFieldSelectionEmpty { field, r#type });
        }
    }
}

impl<'a, E: ExecutableDocument + 'a, S: SchemaDefinition + 'a> IntoIterator
    for LeafFieldSelections<'a, E, S>
{
    type Item = Error<'a, E, S>;
    type IntoIter = std::vec::IntoIter<Error<'a, E, S>>;

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

impl<'a, E: ExecutableDocument + 'a, S: SchemaDefinition + 'a> Rule<'a, E, S>
    for LeafFieldSelections<'a, E, S>
{
    type Error = Error<'a, E, S>;

    fn new(_: &'a E, _: &'a S, _: &'a Cache<'a, E, S>) -> Self {
        Self { errors: Vec::new() }
    }
}