Debian Codegraph @

third_party/rust/fluent-syntax/src/bin/parser.rs

raw
1 use fluent_syntax::parser::parse;
2 use std::env;
3 use std::fs::File;
4 use std::io;
5 use std::io::Read;
6
7 fn read_file(path: &str) -> Result<String, io::Error> {
8 let mut f = File::open(path)?;
9 let mut s = String::new();
10 f.read_to_string(&mut s)?;
11 Ok(s)
12 }
13
14 fn main() {
15 let args: Vec<String> = env::args().collect();
16 let source = read_file(args.get(1).expect("Pass an argument")).expect("Failed to fetch file");
17
18 let (ast, errors) = match parse(source.as_str()) {
19 Ok(ast) => (ast, None),
20 Err((ast, err)) => (ast, Some(err)),
21 };
22
23 #[cfg(feature = "json")]
24 {
25 let target_json = serde_json::to_string_pretty(&ast).unwrap();
26 println!("{}", target_json);
27 }
28 #[cfg(not(feature = "json"))]
29 {
30 use std::fmt::Write;
31 let mut result = String::new();
32 write!(result, "{:#?}", ast).unwrap();
33 println!("{}", result);
34 }
35
36 if let Some(errors) = errors {
37 println!("\n======== Errors ========== \n");
38 for err in errors {
39 println!("Err: {:#?}", err);
40 }
41 }
42 }