1use std::fs::{self, File};
8use std::io::{BufRead, BufReader, LineWriter, Write};
9
10use colored::*;
11
12const FOLDER: &str = "data";
13const INPUT_TYPE: &str = "txt";
14
15fn main() {
17 println!("{}", "MMI Parser".cyan().bold());
18 println!("{}", "============".cyan().bold());
19 println!("Reading files from: {}", FOLDER.cyan());
20
21 match fs::read_dir(FOLDER) {
22 Ok(files) => {
23 for file in files {
24 let file = file.expect("Could not process file.");
25 let path = file.path();
26 let filename = path.to_str().expect("could not parse file path");
27 if filename.ends_with(INPUT_TYPE) && filename.contains("MMI") {
28 println!("Reading file: {}", filename.cyan());
29 let out_file_name = filename.replace(".txt", "_parsed.jsonl").to_string();
30 let out_file =
31 fs::File::create(&out_file_name).expect("could not create output file");
32 let mut out_writer = LineWriter::new(out_file);
33 let file = File::open(&path).expect("could not open file");
35 let reader = BufReader::new(file);
36 for line in reader.lines().flatten() {
37 let result = mmi_parser::parse_record(&line);
38 if result.is_err() {
39 panic!("Example failed!")
40 }
41 let json_val = serde_json::to_value(result.unwrap())
42 .expect("unable to serialize json");
43 let json_string =
44 serde_json::to_string(&json_val).expect("unable to deserialize json");
45 out_writer.write_all(json_string.as_bytes()).unwrap();
46 out_writer.write_all(b"\n").unwrap();
47 }
48 }
49 }
50 }
51 Err(e) => println!("Error: {}", e),
52 }
53}