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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
use crate::commands::tracker::StatusContext;
use crate::commands::validate::{OutputFormatType, Reporter};
use crate::rules::eval_context::EventRecord;
use crate::rules::path_value::traversal::Traversal;
use crate::rules::values::CmpOperator;
use crate::rules::{
BlockCheck, ClauseCheck, ComparisonClauseCheck, NamedStatus, QueryResult, RecordType, Status,
TypeBlockCheck, UnaryValueCheck, ValueCheck,
};
use std::io::Write;
use std::rc::Rc;
#[derive(Debug)]
pub(crate) struct ConsoleReporter {}
//
// https://vallentin.dev/2019/05/14/pretty-print-tree
//
fn pprint_failed_sub_tree(
current: &EventRecord<'_>,
prefix: String,
last: bool,
rules_file_name: &str,
data_file_name: &str,
writer: &mut dyn Write,
) -> crate::rules::Result<()> {
let prefix_current = if last { "`- " } else { "|- " };
let increment_prefix = match ¤t.container {
Some(RecordType::TypeBlock(Status::FAIL))
| Some(RecordType::BlockGuardCheck(BlockCheck {
status: Status::FAIL,
..
}))
| Some(RecordType::GuardClauseBlockCheck(BlockCheck {
status: Status::FAIL,
..
}))
| Some(RecordType::TypeCheck(TypeBlockCheck {
block:
BlockCheck {
status: Status::FAIL,
..
},
..
}))
| Some(RecordType::WhenCheck(BlockCheck {
status: Status::FAIL,
..
})) => false,
Some(RecordType::FileCheck(NamedStatus {
status: Status::FAIL,
..
}))
| Some(RecordType::RuleCheck(NamedStatus {
status: Status::FAIL,
..
}))
| Some(RecordType::Disjunction(BlockCheck {
status: Status::FAIL,
..
})) => {
writeln!(writer, "{}{}{}", prefix, prefix_current, current)?;
true
}
Some(RecordType::ClauseValueCheck(check)) => {
match check {
ClauseCheck::NoValueForEmptyCheck(msg) => {
let custom_message = msg
.as_ref()
.map_or("".to_string(), |s| s.replace('\n', ";"));
writeln!(
writer,
"{}{}Check was not compliant as variable in context [{}] was not empty. Message [{}]",
prefix,
prefix_current,
current.context,
custom_message
)?;
}
ClauseCheck::Success => {}
ClauseCheck::DependentRule(missing) => {
writeln!(
writer,
"{prefix}{prefix_current}Check was not compliant as dependent rule [{rule}] evaluated to FAIL in [{file}]. Context [{cxt}]",
prefix=prefix,
prefix_current=prefix_current,
rule=missing.rule,
file=rules_file_name,
cxt=current.context
)?;
}
ClauseCheck::MissingBlockValue(missing) => {
let (property, far) = match &missing.from {
QueryResult::UnResolved(ur) => {
(ur.remaining_query.as_str(), Rc::clone(&ur.traversed_to))
}
_ => unreachable!(),
};
writeln!(
writer,
"{}{}Check was not compliant as property [{}] is missing in data [{}]. Value traversed to [{}]",
prefix,
prefix_current,
property,
data_file_name,
far
)?;
}
ClauseCheck::Unary(UnaryValueCheck {
comparison: (cmp, not),
value:
ValueCheck {
status: Status::FAIL,
from,
message,
custom_message,
},
}) => {
let cmp_msg = match cmp {
CmpOperator::Exists => {
if *not {
"existed"
} else {
"did not exist"
}
}
CmpOperator::Empty => {
if *not {
"was empty"
} else {
"was not empty"
}
}
CmpOperator::IsList => {
if *not {
"was a list "
} else {
"was not list"
}
}
CmpOperator::IsMap => {
if *not {
"was a struct"
} else {
"was not struct"
}
}
CmpOperator::IsString => {
if *not {
"was a string "
} else {
"was not string"
}
}
_ => unreachable!(),
};
let custom_message = custom_message.as_ref().map_or("".to_string(), |s| {
format!(" Message = [{}]", s.replace('\n', ";"))
});
let error_message = message
.as_ref()
.map_or("".to_string(), |s| format!(" Error = [{}]", s));
match from {
QueryResult::Literal(_) => unreachable!(),
QueryResult::Resolved(res) => {
writeln!(
writer,
"{}{}Check was not compliant as property [{prop}] {cmp_msg}.{err}{msg}",
prefix,
prefix_current,
prop=res.self_path(),
cmp_msg=cmp_msg,
err=error_message,
msg=custom_message
)?;
}
QueryResult::UnResolved(unres) => {
writeln!(
writer,
"{}{}Check was not compliant as property [{remain}] is missing. Value traversed to [{tr}].{err}{msg}",
prefix,
prefix_current,
remain=unres.remaining_query,
tr=unres.traversed_to,
err=error_message,
msg=custom_message
)?;
}
}
}
ClauseCheck::Comparison(ComparisonClauseCheck {
custom_message,
message,
comparison: (cmp, not),
from,
status: Status::FAIL,
to,
}) => {
let custom_message = custom_message.as_ref().map_or("".to_string(), |s| {
format!(" Message = [{}]", s.replace('\n', ";"))
});
let error_message = message
.as_ref()
.map_or("".to_string(), |s| format!(" Error = [{}]", s));
let to_result = match to {
Some(to) => match to {
QueryResult::Literal(_) => unreachable!(),
QueryResult::Resolved(to_res) => Some(Rc::clone(to_res)),
QueryResult::UnResolved(to_unres) => {
writeln!(
writer,
"{}{}Check was not compliant as property [{remain}] to compare to is missing. Value traversed to [{to}].{err}{msg}",
prefix,
prefix_current,
remain=to_unres.remaining_query,
to=to_unres.traversed_to,
err=error_message,
msg=custom_message
)?;
return Ok(());
}
},
None => None,
};
match from {
QueryResult::Literal(_) => unreachable!(),
QueryResult::UnResolved(to_unres) => {
writeln!(
writer,
"{}{}Check was not compliant as property [{remain}] to compare from is missing. Value traversed to [{to}].{err}{msg}",
prefix,
prefix_current,
remain=to_unres.remaining_query,
to=to_unres.traversed_to,
err=error_message,
msg=custom_message
)?;
}
QueryResult::Resolved(res) => {
writeln!(
writer,
"{}{}Check was not compliant as property value [{from}] {op_msg} value [{to}].{err}{msg}",
prefix,
prefix_current,
from=res,
to=to_result.map_or("NULL".to_string(), |t| format!("{}", t)),
op_msg=match cmp {
CmpOperator::Eq => if *not { "equal to" } else { "not equal to" },
CmpOperator::Le => if *not { "less than equal to" } else { "not less than equal to" },
CmpOperator::Lt => if *not { "less than" } else { "not less than" },
CmpOperator::Ge => if *not { "greater than equal to" } else { "not greater than equal" },
CmpOperator::Gt => if *not { "greater than" } else { "not greater than" },
CmpOperator::In => if *not { "in" } else { "not in" },
_ => unreachable!()
},
err=error_message,
msg=custom_message
)?;
}
}
}
_ => return Ok(()), // Success skip
}
false
}
_ => return Ok(()),
};
let prefix = if increment_prefix {
let prefix_child = if last { " " } else { "| " };
prefix + prefix_child
} else {
prefix
};
if !current.children.is_empty() {
let last_child = current.children.len() - 1;
for (i, child) in current.children.iter().enumerate() {
pprint_failed_sub_tree(
child,
prefix.clone(),
i == last_child,
rules_file_name,
data_file_name,
writer,
)?;
}
}
Ok(())
}
impl Reporter for ConsoleReporter {
fn report(
&self,
_writer: &mut dyn Write,
_status: Option<Status>,
_failed_rules: &[&StatusContext],
_passed_or_skipped: &[&StatusContext],
_longest_rule_name: usize,
_rules_file: &str,
_data_file: &str,
_data: &Traversal<'_>,
_output_format_type: OutputFormatType,
) -> crate::rules::Result<()> {
Ok(())
}
fn report_eval<'value>(
&self,
_write: &mut dyn Write,
_status: Status,
_root_record: &EventRecord<'value>,
_rules_file: &str,
_data_file: &str,
_data_file_bytes: &str,
_data: &Traversal<'value>,
_output_type: OutputFormatType,
) -> crate::rules::Result<()> {
pprint_failed_sub_tree(
_root_record,
"".to_string(),
true,
_rules_file,
_data_file,
_write,
)
}
}