pub fn parse_from_path(path: &PathBuf) -> Result<Parser>Expand description
Create a parser for the given file. The source name is set to the given path. This method can fail if it is unable to open the given file, or is unable to read from it.
For the specific errors that can result, see std::fs::OpenOptions::open() in the
Rust standard library, and also std::io::Read::read_to_end().
Examples found in repository?
examples/json_validator.rs (line 21)
13pub fn main() {
14 let args: Vec<String> = env::args().collect();
15 if args.len() != 2 {
16 // Missing argument. Issue a message and stop.
17 println!("Missing filename as only argument.");
18 std::process::exit(0);
19 }
20 let path = PathBuf::from(&args[1]);
21 let mut parser = match parse_from_path(&path) {
22 Ok(parser) => parser,
23 Err(error) => {
24 println!("Error: {}", error);
25 std::process::exit(1)
26 }
27 };
28 match JSONParser::new().parse_value_ws(&mut parser) {
29 Ok(_) => {
30 // If there is any trailing stuff that is not whitespace, then this is not a valid
31 // JSON file.
32 if parser.is_at_eof() {
33 std::process::exit(0)
34 } else {
35 println!("Found unexpected trailing characters after JSON value.");
36 std::process::exit(1);
37 }
38 }
39 Err(error) => {
40 println!("Error: {}", error);
41 std::process::exit(1)
42 }
43 }
44}