trivet 3.1.0

The trivet Parser Library
Documentation
//! 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)
        }
    }
}