diff --git a/Cargo.lock b/Cargo.lock index 86eca2c..61a7893 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -637,6 +637,7 @@ dependencies = [ "rand", "rand_distr", "serde", + "serde_json", "tokio", ] diff --git a/Cargo.toml b/Cargo.toml index 0e2bd62..25425d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,3 +17,4 @@ rand_distr = "0.4.3" tokio = { version = "1.39.2", features = ["macros"] } csv = "1.1" serde = {version = "1.0", features = ["derive"]} +serde_json = "1.0" \ No newline at end of file diff --git a/src/engine/inspector.rs b/src/engine/inspector.rs index fe867ec..ec9fc5d 100644 --- a/src/engine/inspector.rs +++ b/src/engine/inspector.rs @@ -7,6 +7,7 @@ use std::path::Path; use std::fs::OpenOptions; use std::{error::Error, io, process}; use serde::{Serialize, Deserialize}; +use serde_json::{Value, json}; /// Trait allowing custom behavior to be defined for logging and inspecting values. pub trait Inspector { @@ -21,8 +22,10 @@ pub trait Inspector { } pub enum SaveData { - ToFile(String), - ToNewFile(String), + ToCsv(String), + ToNewCsv(String), + ToJson(String), + ToNewJson(String), } #[derive(Serialize, Deserialize, Debug, Clone)] @@ -91,17 +94,52 @@ impl Logger { Ok(()) } + + fn create_json(file_location: &String) -> Result<(), Box> { + let file = OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .open(file_location)?; + + let empty_array = json!([]); + serde_json::to_writer(file, &empty_array)?; + + Ok(()) + } + + fn append_to_json(record: LogMessage, path: &Path) -> Result<(), Box> { + let mut records = Self::read_json_file(&path.to_string_lossy().to_string())?; + + records.push(record); + + let file = OpenOptions::new() + .write(true) + .truncate(true) + .open(path)?; + + serde_json::to_writer(file, &records)?; + + Ok(()) + } + + fn read_json_file(file_loc: &String) -> Result, Box> { + let file = File::open(file_loc)?; + let records: Vec = serde_json::from_reader(file)?; + Ok(records) + } + /* - fn read_csv_file(file_location: &String) -> Result, csv::Error> { + fn read_csv_file(file_loc: &String) -> Result, csv::Error> { let mut records: Vec = vec![]; let file = OpenOptions::new() .write(true) .create(true) .truncate(true) - .open(file_location)?; + .open(file_loc)?; let mut reader = csv::Reader::from_reader(file); - + for record in reader.deserialize() { // println!("Line"); let record: Record = record?; @@ -126,18 +164,34 @@ impl Inspector for Logger { fn save(&self, save_type: Option) { let mut file_loc = String::new(); match save_type.unwrap() { - SaveData::ToNewFile(file_location) => { + SaveData::ToNewCsv(file_location) => { Logger::create_csv(&file_location); - file_loc = file_location; + for log in self.values.clone() { + let file_path = Path::new(&file_location); + Logger::append_to_csv(log, &file_path); + } }, - SaveData::ToFile(file_location) => { - file_loc = file_location; + SaveData::ToCsv(file_location) => { + for log in self.values.clone() { + let file_path = Path::new(&file_location); + Logger::append_to_csv(log, &file_path); + } + }, + + SaveData::ToNewJson(file_location) => { + Logger::create_json(&file_location); + for log in self.values.clone() { + let file_path = Path::new(&file_location); + Logger::append_to_json(log, &file_path); + } }, - } - for log in self.values.clone() { - let file_path = Path::new(&file_loc); - Logger::append_to_csv(log, &file_path); + SaveData::ToJson(file_location) => { + for log in self.values.clone() { + let file_path = Path::new(&file_location); + Logger::append_to_json(log, &file_path); + } + } } } }