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
84
use crate::executable::{Cache, DirectiveError, Error, Rule, Visitor};
use crate::utils::duplicates;
use bluejay_core::definition::{DirectiveDefinition, DirectiveLocation, SchemaDefinition};
use bluejay_core::executable::ExecutableDocument;
use bluejay_core::{AsIter, Directive};
use std::ops::Not;

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

impl<'a, E: ExecutableDocument + 'a, S: SchemaDefinition + 'a>
    DirectivesAreUniquePerLocation<'a, E, S>
{
    fn visit_directives<
        const CONST: bool,
        F: Fn(DirectiveError<'a, CONST, E, S>) -> Error<'a, E, S>,
    >(
        &mut self,
        directives: &'a <E as ExecutableDocument>::Directives<CONST>,
        build_error: F,
    ) {
        self.errors
            .extend(duplicates(directives.iter(), Directive::name).filter_map(
                |(directive_name, directives)| {
                    self.schema_definition
                        .get_directive_definition(directive_name)
                        .and_then(|directive_definition| {
                            directive_definition.is_repeatable().not().then(|| {
                                build_error(DirectiveError::DirectivesNotUniquePerLocation {
                                    directives,
                                    directive_definition,
                                })
                            })
                        })
                },
            ));
    }
}

impl<'a, E: ExecutableDocument + 'a, S: SchemaDefinition + 'a> Visitor<'a, E, S>
    for DirectivesAreUniquePerLocation<'a, E, S>
{
    fn visit_variable_directives(
        &mut self,
        directives: &'a <E as ExecutableDocument>::Directives<false>,
        _: DirectiveLocation,
    ) {
        self.visit_directives(directives, Error::InvalidVariableDirective)
    }

    fn visit_const_directives(
        &mut self,
        directives: &'a <E as ExecutableDocument>::Directives<true>,
        _: DirectiveLocation,
    ) {
        self.visit_directives(directives, Error::InvalidConstDirective)
    }
}

impl<'a, E: ExecutableDocument + 'a, S: SchemaDefinition + 'a> IntoIterator
    for DirectivesAreUniquePerLocation<'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 DirectivesAreUniquePerLocation<'a, E, S>
{
    type Error = Error<'a, E, S>;

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