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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
use bluejay_core::definition::{
    DirectiveDefinition, FieldDefinition, InputValueDefinition, SchemaDefinition,
};
use bluejay_core::executable::ExecutableDocument;
#[cfg(feature = "parser-integration")]
use bluejay_parser::{
    ast::executable::ExecutableDocument as ParserExecutableDocument,
    error::{Annotation, Error as ParserError},
    HasSpan,
};
use itertools::Itertools;

pub enum ArgumentError<'a, const CONST: bool, E: ExecutableDocument, S: SchemaDefinition> {
    NonUniqueArgumentNames {
        arguments: Vec<&'a E::Argument<CONST>>,
        name: &'a str,
    },
    ArgumentDoesNotExistOnField {
        argument: &'a E::Argument<CONST>,
        field_definition: &'a S::FieldDefinition,
    },
    ArgumentDoesNotExistOnDirective {
        argument: &'a E::Argument<CONST>,
        directive_definition: &'a S::DirectiveDefinition,
    },
    DirectiveMissingRequiredArguments {
        directive: &'a E::Directive<CONST>,
        directive_definition: &'a S::DirectiveDefinition,
        missing_argument_definitions: Vec<&'a S::InputValueDefinition>,
    },
    FieldMissingRequiredArguments {
        field: &'a E::Field,
        field_definition: &'a S::FieldDefinition,
        missing_argument_definitions: Vec<&'a S::InputValueDefinition>,
    },
}

#[cfg(feature = "parser-integration")]
impl<'a, const CONST: bool, S: SchemaDefinition>
    From<ArgumentError<'a, CONST, ParserExecutableDocument<'a>, S>> for ParserError
{
    fn from(value: ArgumentError<'a, CONST, ParserExecutableDocument<'a>, S>) -> Self {
        match value {
            ArgumentError::NonUniqueArgumentNames { arguments, name } => Self::new(
                format!("Multiple arguments with name `{name}`"),
                None,
                arguments
                    .into_iter()
                    .map(|argument| {
                        Annotation::new(
                            format!("Argument with name `{name}`"),
                            argument.name().span().clone(),
                        )
                    })
                    .collect(),
            ),
            ArgumentError::ArgumentDoesNotExistOnField {
                argument,
                field_definition,
            } => Self::new(
                format!(
                    "Field `{}` does not define an argument named `{}`",
                    field_definition.name(),
                    argument.name().as_ref(),
                ),
                Some(Annotation::new(
                    "No argument definition with this name",
                    argument.name().span().clone(),
                )),
                Vec::new(),
            ),
            ArgumentError::ArgumentDoesNotExistOnDirective {
                argument,
                directive_definition,
            } => Self::new(
                format!(
                    "Directive `{}` does not define an argument named `{}`",
                    directive_definition.name(),
                    argument.name().as_ref(),
                ),
                Some(Annotation::new(
                    "No argument definition with this name",
                    argument.name().span().clone(),
                )),
                Vec::new(),
            ),
            ArgumentError::DirectiveMissingRequiredArguments {
                directive,
                missing_argument_definitions,
                ..
            } => {
                let missing_argument_names = missing_argument_definitions
                    .into_iter()
                    .map(InputValueDefinition::name)
                    .join(", ");
                Self::new(
                    format!(
                        "Directive `{}` missing argument(s): {missing_argument_names}",
                        directive.name().as_ref(),
                    ),
                    Some(Annotation::new(
                        format!("Missing argument(s): {missing_argument_names}"),
                        directive.span().clone(),
                    )),
                    Vec::new(),
                )
            }
            ArgumentError::FieldMissingRequiredArguments {
                field,
                field_definition: _,
                missing_argument_definitions,
            } => {
                let missing_argument_names = missing_argument_definitions
                    .into_iter()
                    .map(InputValueDefinition::name)
                    .join(", ");
                let span = match field.arguments() {
                    Some(arguments) => field.name().span().merge(arguments.span()),
                    None => field.name().span().clone(),
                };
                Self::new(
                    format!(
                        "Field `{}` missing argument(s): {missing_argument_names}",
                        field.response_key()
                    ),
                    Some(Annotation::new(
                        format!("Missing argument(s): {missing_argument_names}"),
                        span,
                    )),
                    Vec::new(),
                )
            }
        }
    }
}