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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use std::collections::HashSet;
use std::fmt::Debug;
use ast::{Definition, Document, Type};
use schema::meta::MetaType;
use schema::model::SchemaType;
use parser::SourcePosition;
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct RuleError {
locations: Vec<SourcePosition>,
message: String,
}
#[doc(hidden)]
pub struct ValidatorContext<'a, S: Debug + 'a> {
pub schema: &'a SchemaType<'a, S>,
errors: Vec<RuleError>,
type_stack: Vec<Option<&'a MetaType<'a, S>>>,
type_literal_stack: Vec<Option<Type<'a>>>,
input_type_stack: Vec<Option<&'a MetaType<'a, S>>>,
input_type_literal_stack: Vec<Option<Type<'a>>>,
parent_type_stack: Vec<Option<&'a MetaType<'a, S>>>,
fragment_names: HashSet<&'a str>,
}
impl RuleError {
#[doc(hidden)]
pub fn new(message: &str, locations: &[SourcePosition]) -> RuleError {
RuleError {
message: message.to_owned(),
locations: locations.to_vec(),
}
}
pub fn message(&self) -> &str {
&self.message
}
pub fn locations(&self) -> &[SourcePosition] {
&self.locations
}
}
impl<'a, S: Debug> ValidatorContext<'a, S> {
#[doc(hidden)]
pub fn new(schema: &'a SchemaType<S>, document: &Document<'a, S>) -> ValidatorContext<'a, S> {
ValidatorContext {
errors: Vec::new(),
schema: schema,
type_stack: Vec::new(),
type_literal_stack: Vec::new(),
parent_type_stack: Vec::new(),
input_type_stack: Vec::new(),
input_type_literal_stack: Vec::new(),
fragment_names: document
.iter()
.filter_map(|def| match *def {
Definition::Fragment(ref frag) => Some(frag.item.name.item),
_ => None,
})
.collect(),
}
}
#[doc(hidden)]
pub fn append_errors(&mut self, mut errors: Vec<RuleError>) {
self.errors.append(&mut errors);
}
#[doc(hidden)]
pub fn report_error(&mut self, message: &str, locations: &[SourcePosition]) {
self.errors.push(RuleError::new(message, locations))
}
#[doc(hidden)]
pub fn into_errors(mut self) -> Vec<RuleError> {
self.errors.sort();
self.errors
}
#[doc(hidden)]
pub fn with_pushed_type<F, R>(&mut self, t: Option<&Type<'a>>, f: F) -> R
where
F: FnOnce(&mut ValidatorContext<'a, S>) -> R,
{
if let Some(t) = t {
self.type_stack
.push(self.schema.concrete_type_by_name(t.innermost_name()));
} else {
self.type_stack.push(None);
}
self.type_literal_stack.push(t.cloned());
let res = f(self);
self.type_literal_stack.pop();
self.type_stack.pop();
res
}
#[doc(hidden)]
pub fn with_pushed_parent_type<F, R>(&mut self, f: F) -> R
where
F: FnOnce(&mut ValidatorContext<'a, S>) -> R,
{
self.parent_type_stack
.push(*self.type_stack.last().unwrap_or(&None));
let res = f(self);
self.parent_type_stack.pop();
res
}
#[doc(hidden)]
pub fn with_pushed_input_type<F, R>(&mut self, t: Option<&Type<'a>>, f: F) -> R
where
F: FnOnce(&mut ValidatorContext<'a, S>) -> R,
{
if let Some(t) = t {
self.input_type_stack
.push(self.schema.concrete_type_by_name(t.innermost_name()));
} else {
self.input_type_stack.push(None);
}
self.input_type_literal_stack.push(t.cloned());
let res = f(self);
self.input_type_literal_stack.pop();
self.input_type_stack.pop();
res
}
#[doc(hidden)]
pub fn current_type(&self) -> Option<&'a MetaType<'a, S>> {
*self.type_stack.last().unwrap_or(&None)
}
#[doc(hidden)]
pub fn current_type_literal(&self) -> Option<&Type<'a>> {
match self.type_literal_stack.last() {
Some(&Some(ref t)) => Some(t),
_ => None,
}
}
#[doc(hidden)]
pub fn parent_type(&self) -> Option<&'a MetaType<'a, S>> {
*self.parent_type_stack.last().unwrap_or(&None)
}
#[doc(hidden)]
pub fn current_input_type_literal(&self) -> Option<&Type<'a>> {
match self.input_type_literal_stack.last() {
Some(&Some(ref t)) => Some(t),
_ => None,
}
}
#[doc(hidden)]
pub fn is_known_fragment(&self, name: &str) -> bool {
self.fragment_names.contains(name)
}
}