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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
use std::{
collections::BTreeSet,
fmt::{Debug, Display},
ops::Deref,
};
use crate::offsets::{NullableOffsetMarker, OffsetMarker};
pub trait Validate {
fn validate(&self) -> Result<(), ValidationReport> {
let mut ctx = Default::default();
self.validate_impl(&mut ctx);
if ctx.errors.is_empty() {
Ok(())
} else {
Err(ValidationReport { errors: ctx.errors })
}
}
#[allow(unused_variables)]
fn validate_impl(&self, ctx: &mut ValidationCtx);
}
#[derive(Clone, Debug, Default)]
pub struct ValidationCtx {
cur_location: Vec<LocationElem>,
errors: Vec<ValidationError>,
}
#[derive(Debug, Clone)]
struct ValidationError {
error: String,
location: Vec<LocationElem>,
}
pub struct ValidationReport {
errors: Vec<ValidationError>,
}
#[derive(Debug, Clone)]
enum LocationElem {
Table(&'static str),
Field(&'static str),
Index(usize),
}
impl ValidationCtx {
pub fn in_table(&mut self, name: &'static str, f: impl FnOnce(&mut ValidationCtx)) {
self.with_elem(LocationElem::Table(name), f);
}
pub fn in_field(&mut self, name: &'static str, f: impl FnOnce(&mut ValidationCtx)) {
self.with_elem(LocationElem::Field(name), f);
}
pub fn in_array(&mut self, f: impl FnOnce(&mut ValidationCtx)) {
self.with_elem(LocationElem::Index(0), f);
}
pub fn array_item(&mut self, f: impl FnOnce(&mut ValidationCtx)) {
assert!(matches!(
self.cur_location.last(),
Some(LocationElem::Index(_))
));
f(self);
match self.cur_location.last_mut() {
Some(LocationElem::Index(i)) => *i += 1,
_ => panic!("array_item called outside of array"),
}
}
pub fn report(&mut self, msg: impl Display) {
self.errors.push(ValidationError {
location: self.cur_location.clone(),
error: msg.to_string(),
});
}
fn with_elem(&mut self, elem: LocationElem, f: impl FnOnce(&mut ValidationCtx)) {
self.cur_location.push(elem);
f(self);
self.cur_location.pop();
}
}
impl Display for ValidationReport {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.errors.len() == 1 {
return writeln!(f, "Validation error:\n{}", self.errors.first().unwrap());
}
writeln!(f, "{} validation errors:", self.errors.len())?;
for (i, error) in self.errors.iter().enumerate() {
writeln!(f, "#{}\n{error}", i + 1)?;
}
Ok(())
}
}
impl Debug for ValidationReport {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
<Self as Display>::fmt(self, f)
}
}
static MANY_SPACES: &str = " ";
impl Display for ValidationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "\"{}\"", self.error)?;
let mut indent = 0;
for (i, window) in self.location.windows(2).enumerate() {
let prev = &window[0];
let current = &window[1];
if i == 0 {
if let LocationElem::Table(name) = prev {
write!(f, "in: {name}")?;
} else {
panic!("first item always table");
}
}
match current {
LocationElem::Table(name) => {
indent += 1;
let indent_str = &MANY_SPACES[..indent * 2];
write!(f, "\n{indent_str}{name}")
}
LocationElem::Field(name) => write!(f, ".{name}"),
LocationElem::Index(idx) => write!(f, "[{idx}]"),
}?;
}
writeln!(f)
}
}
impl<T: Validate> Validate for Vec<T> {
fn validate_impl(&self, ctx: &mut ValidationCtx) {
ctx.in_array(|ctx| {
for item in self.iter() {
ctx.array_item(|ctx| {
item.validate_impl(ctx);
})
}
});
}
}
impl<const N: usize, T: Validate> Validate for OffsetMarker<T, N> {
fn validate_impl(&self, ctx: &mut ValidationCtx) {
self.deref().validate_impl(ctx)
}
}
impl<const N: usize, T: Validate> Validate for NullableOffsetMarker<T, N> {
fn validate_impl(&self, ctx: &mut ValidationCtx) {
self.deref().validate_impl(ctx)
}
}
impl<T: Validate> Validate for Option<T> {
fn validate_impl(&self, ctx: &mut ValidationCtx) {
match self {
Some(t) => t.validate_impl(ctx),
None => (),
}
}
}
impl<T: Validate> Validate for BTreeSet<T> {
fn validate_impl(&self, ctx: &mut ValidationCtx) {
ctx.in_array(|ctx| {
for item in self.iter() {
ctx.array_item(|ctx| {
item.validate_impl(ctx);
})
}
});
}
}