parse_mmi/
parse_mmi.rs

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
45
46
47
48
49
50
51
52
53
//! Example usage of default command line program if passed `data` folder.
//!
//! Looks specifically for the MMI sample data file.
//!
//! Results will be available in `data/MMI_sample_parsed.jsonl`

use std::fs::{self, File};
use std::io::{BufRead, BufReader, LineWriter, Write};

use colored::*;

const FOLDER: &str = "data";
const INPUT_TYPE: &str = "txt";

/// Main function.
fn main() {
    println!("{}", "MMI Parser".cyan().bold());
    println!("{}", "============".cyan().bold());
    println!("Reading files from: {}", FOLDER.cyan());

    match fs::read_dir(FOLDER) {
        Ok(files) => {
            for file in files {
                let file = file.expect("Could not process file.");
                let path = file.path();
                let filename = path.to_str().expect("could not parse file path");
                if filename.ends_with(INPUT_TYPE) && filename.contains("MMI") {
                    println!("Reading file: {}", filename.cyan());
                    let out_file_name = filename.replace(".txt", "_parsed.jsonl").to_string();
                    let out_file =
                        fs::File::create(&out_file_name).expect("could not create output file");
                    let mut out_writer = LineWriter::new(out_file);
                    // utilize read lines buffer
                    let file = File::open(&path).expect("could not open file");
                    let reader = BufReader::new(file);
                    for line in reader.lines().flatten() {
                        let result = mmi_parser::parse_record(&line);
                        if result.is_err() {
                            panic!("Example failed!")
                        }
                        let json_val = serde_json::to_value(result.unwrap())
                            .expect("unable to serialize json");
                        let json_string =
                            serde_json::to_string(&json_val).expect("unable to deserialize json");
                        out_writer.write_all(json_string.as_bytes()).unwrap();
                        out_writer.write_all(b"\n").unwrap();
                    }
                }
            }
        }
        Err(e) => println!("Error: {}", e),
    }
}