-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathvalidation.rs
More file actions
165 lines (148 loc) · 5.28 KB
/
Copy pathvalidation.rs
File metadata and controls
165 lines (148 loc) · 5.28 KB
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
use std::{fmt::Write, sync::Arc};
use crate::{
package::simulation::output::analysis::analyzer::{AnalysisOperationRepr, AnalysisSourceRepr},
Error, Result,
};
impl AnalysisSourceRepr {
pub fn validate_def(&self) -> Result<()> {
let results: Vec<(Arc<String>, Option<String>)> = self
.outputs
.iter()
.map(|(name, operations)| {
let mut error = ErrorBuilder::new();
if operations.is_empty() {
error.add("Must have at least one operation".into());
return Ok((name.clone(), error.finish()));
}
if let Some(why) = operations[0].is_not_valid_first_operation()? {
error.add(why)
}
let mut prev_operation = &operations[0];
for operation in operations.iter().skip(1) {
if let Some(err) =
operation.is_not_valid_subsequent_operation(prev_operation)?
{
error.add(err);
}
prev_operation = operation;
}
Ok((name.clone(), error.finish()))
})
.collect::<Result<_>>()?;
if results.iter().any(|(_name, b)| b.is_some()) {
let mut error_string = String::new();
for (output, error) in results {
if let Some(error_str) = error {
let _ = write!(
error_string,
"Output with name '{}' has an incorrect definition, errors: {{{}}}. ",
output, error_str
);
}
}
Err(Error::from(format!(
"Analysis file (analysis.json) contains error(s): {}",
error_string
)))
} else {
Ok(())
}
}
}
impl AnalysisOperationRepr {
pub fn is_not_valid_first_operation(&self) -> Result<Option<String>> {
let mut error = ErrorBuilder::new();
if !(self.is_filter() || self.is_map() || self.is_count()) {
error.add("The first operation must either be 'filter', 'sum' or 'count'".into());
}
if let AnalysisOperationRepr::Filter {
field,
comparison: _,
value: _,
} = self
{
if !field.is_string() {
if let Ok(field_repr) = serde_json::to_string(field) {
error.add(format!(
"The first operation (a 'filter') must access a field of an agent by \
string, however the current 'field' value is {}",
field_repr
));
} else {
error.add(
"The first operation (a 'filter') must access a field of an agent by \
string"
.into(),
);
}
}
}
Ok(error.finish())
}
pub fn is_not_valid_subsequent_operation(&self, preceding: &Self) -> Result<Option<String>> {
let result = match preceding {
AnalysisOperationRepr::Filter {
field,
comparison: _,
value: _,
} => {
let mut error = ErrorBuilder::new();
if !(field.is_string() || field.is_u64()) {
error.add(
"A 'filter' operation must access a field (by string) or an element of an \
array (by non-negative integer)"
.into(),
);
}
if !(self.is_filter() || self.is_map() || self.is_count()) {
error.add(
"A 'filter' operation must be followed either by 'filter', 'get' or \
'count' operations"
.into(),
);
}
error.finish()
}
AnalysisOperationRepr::Get { field } => {
let mut error = None;
if !(field.is_string() || field.is_u64()) {
error = Some(
"A 'get' operation must access a field (by string) or an element of an \
array (by non-negative integer)"
.into(),
);
}
error
}
_ => Some(format!(
"A '{}' operation must be terminal",
serde_json::to_string(preceding)?
)),
};
Ok(result)
}
}
struct ErrorBuilder {
inner: Vec<String>,
}
impl ErrorBuilder {
fn new() -> ErrorBuilder {
ErrorBuilder { inner: Vec::new() }
}
fn add(&mut self, error: String) {
self.inner.push(error)
}
fn finish(self) -> Option<String> {
if self.inner.is_empty() {
return None;
}
let mut finished = String::new();
self.inner.iter().enumerate().for_each(|(i, error)| {
finished.push_str(error);
if i != self.inner.len() - 1 {
finished.push_str(". ");
}
});
Some(finished)
}
}