changed decom to output json
This commit is contained in:
+20
-29
@@ -1,7 +1,6 @@
|
||||
#![feature(bufreader_peek)]
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
env::args,
|
||||
fs::{read_dir, OpenOptions},
|
||||
io::{BufReader, Write},
|
||||
path::PathBuf,
|
||||
@@ -15,6 +14,17 @@ use log::*;
|
||||
use rfe::{msg::MsgPacket, BINCODE_CONFIG};
|
||||
use simple_logger::SimpleLogger;
|
||||
|
||||
use clap::Parser;
|
||||
|
||||
// Decom DS recorded data files
|
||||
#[derive(Parser, Debug)]
|
||||
struct CliArgs {
|
||||
/// The path to the file or folder containing the DS data
|
||||
path: String,
|
||||
/// The path to the folder to output the decommed data
|
||||
out_dir: String,
|
||||
}
|
||||
|
||||
fn decom_file(file_path: String, out_dir: String) -> Result<()> {
|
||||
info!("decomming {file_path}");
|
||||
let f = OpenOptions::new().read(true).open(file_path)?;
|
||||
@@ -29,40 +39,25 @@ fn decom_file(file_path: String, out_dir: String) -> Result<()> {
|
||||
};
|
||||
if !files.contains_key(&msg.msg.kind()) {
|
||||
let path = PathBuf::from_str(&out_dir)?
|
||||
.join(format!("{:?}.csv", msg.msg.kind()).to_lowercase());
|
||||
let exists = path.exists();
|
||||
let mut w = OpenOptions::new()
|
||||
.join(format!("{:?}.json", msg.msg.kind()).to_lowercase());
|
||||
let w = OpenOptions::new()
|
||||
.write(true)
|
||||
.create(true)
|
||||
.append(true)
|
||||
.open(path)?;
|
||||
|
||||
if !exists {
|
||||
let values = msg.to_csv_clean();
|
||||
let mut line = values
|
||||
.iter()
|
||||
.map(|x| x.split_once(" = ").expect("unexpected csv data").0)
|
||||
.collect::<Vec<&str>>()
|
||||
.join(",");
|
||||
line += "\n";
|
||||
w.write(line.as_bytes())?;
|
||||
}
|
||||
|
||||
files.insert(msg.msg.kind(), w);
|
||||
}
|
||||
|
||||
if let Some(w) = files.get_mut(&msg.msg.kind()) {
|
||||
let values = msg.to_csv_clean();
|
||||
let mut line = values
|
||||
.iter()
|
||||
.map(|x| x.split_once(" = ").expect("unexpected csv data").1)
|
||||
.collect::<Vec<&str>>()
|
||||
.join(",");
|
||||
line += "\n";
|
||||
w.write(line.as_bytes())?;
|
||||
let values = serde_json::to_string(&msg)?;
|
||||
|
||||
w.write(values.as_bytes())?;
|
||||
w.write("\n".as_bytes())?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
@@ -99,13 +94,9 @@ fn decom_task(folder: String, out_dir: String) -> Result<()> {
|
||||
|
||||
fn main() -> Result<()> {
|
||||
SimpleLogger::new().init().unwrap();
|
||||
let args = CliArgs::parse();
|
||||
info!("started decom");
|
||||
|
||||
let args = args().skip(1).collect::<Vec<String>>();
|
||||
let folder = args[0].clone();
|
||||
let out_dir = args[1].clone();
|
||||
|
||||
decom_task(folder, out_dir)?;
|
||||
decom_task(args.path, args.out_dir)?;
|
||||
|
||||
info!("finished decom");
|
||||
return Ok(());
|
||||
|
||||
Reference in New Issue
Block a user