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
//! Validator for JSON. This exists to use with JSON test suites.
//!
//! See: https://github.com/nst/JSONTestSuite
use std::env;
use std::path::PathBuf;
use trivet::parse_from_path;
use trivet::parsers::json::JSONParser;
/// Entry point when run from prompt. This accepts a single argument which must be the
/// path to a JSON file. The file is parsed. If no errors are detected, then a zero is
/// return (the file is "valid"). Otherwise a one is returned (the file is "invalid").
pub fn main() {
let args: Vec<String> = env::args().collect();
if args.len() != 2 {
// Missing argument. Issue a message and stop.
println!("Missing filename as only argument.");
std::process::exit(0);
}
let path = PathBuf::from(&args[1]);
let mut parser = match parse_from_path(&path) {
Ok(parser) => parser,
Err(error) => {
println!("Error: {}", error);
std::process::exit(1)
}
};
match JSONParser::new().parse_value_ws(&mut parser) {
Ok(_) => {
// If there is any trailing stuff that is not whitespace, then this is not a valid
// JSON file.
if parser.is_at_eof() {
std::process::exit(0)
} else {
println!("Found unexpected trailing characters after JSON value.");
std::process::exit(1);
}
}
Err(error) => {
println!("Error: {}", error);
std::process::exit(1)
}
}
}