pub fn parse_record(text: &str) -> Result<Output, Box<dyn Error>>
Expand description
A better alternative to MmiOutput::assemble
or AaOutput::assemble
Takes a string reference, splits it on vertical bar (pipe) characters,
labels each item with its corresponding field name,
passes labeled data into MmiOutput::assemble
or AaOutput::assemble
.
This is used to scan over lines in fielded MMI output text files in the main CLI. It detects whether the record is MMI or not by looking at the second item in the pipe-delimited vector and whether it matches MMI, AA/UA, or neither.
Arguments:
- text: a string reference representing a single line of MMI/AA output
Returns:
- Result<Output, ValueError>: An enumeration with MMI::MmiOutput and AA::AaOutput options. Could return error if a valid option is not found in the second vector position.
This effectively converts each fielded MMI line into an Output
of either MMI or AA type.
For example:
use std::io::{BufReader, BufRead};
use std::fs::File;
let file = File::open("data/MMI_sample.txt").unwrap();
// or for AA records
// let file = File::open("data/AA_sample.txt".unwrap());
let reader = BufReader::new(file);
for line in reader.lines() {
let record = line.unwrap();
let result = mmi_parser::parse_record(record.as_str());
println!("{:?}", result.unwrap()); // must use debug
}
Examples found in repository?
examples/parse_aa.rs (line 37)
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
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("AA") {
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),
}
}
More examples
examples/parse_mmi.rs (line 37)
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
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),
}
}