From 57a620d76bafe85a21fe371ce3c8d160d8c870af Mon Sep 17 00:00:00 2001 From: tencalink0 Date: Sun, 18 Aug 2024 15:34:33 +0100 Subject: [PATCH] Implemented serialization --- Cargo.lock | 25 ++++++++++++++- Cargo.toml | 2 ++ src/engine/inspector.rs | 69 +++++++++++++++++++++++++++++------------ 3 files changed, 76 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0e7d4ba..86eca2c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -626,15 +626,17 @@ dependencies = [ [[package]] name = "arena-core" -version = "0.1.0" +version = "0.1.1" dependencies = [ "alloy", "alloy-contract", "alloy-sol-macro", "alloy-sol-types", + "csv", "plotly", "rand", "rand_distr", + "serde", "tokio", ] @@ -1127,6 +1129,27 @@ dependencies = [ "typenum", ] +[[package]] +name = "csv" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac574ff4d437a7b5ad237ef331c17ccca63c46479e5b5453eb8e10bb99a759fe" +dependencies = [ + "csv-core", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "csv-core" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5efa2b3d7902f4b634a20cae3c9c4e6209dc4779feb6863329607560143efa70" +dependencies = [ + "memchr", +] + [[package]] name = "darling" version = "0.20.10" diff --git a/Cargo.toml b/Cargo.toml index a73f380..0e2bd62 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,3 +15,5 @@ plotly = "0.9.0" rand = "0.8.5" rand_distr = "0.4.3" tokio = { version = "1.39.2", features = ["macros"] } +csv = "1.1" +serde = {version = "1.0", features = ["derive"]} diff --git a/src/engine/inspector.rs b/src/engine/inspector.rs index b472f7b..fe867ec 100644 --- a/src/engine/inspector.rs +++ b/src/engine/inspector.rs @@ -5,6 +5,8 @@ use std::fs::File; use std::io::prelude::*; use std::path::Path; use std::fs::OpenOptions; +use std::{error::Error, io, process}; +use serde::{Serialize, Deserialize}; /// Trait allowing custom behavior to be defined for logging and inspecting values. pub trait Inspector { @@ -23,7 +25,7 @@ pub enum SaveData { ToNewFile(String), } -#[derive(Debug, Clone)] +#[derive(Serialize, Deserialize, Debug, Clone)] pub struct LogMessage { id: usize, // Id of the log name: String, // Name of the log @@ -58,22 +60,56 @@ impl Logger { } } - pub fn append_to_csv(file_location: &String, contents: String) { - let path = Path::new(file_location); - let mut file = OpenOptions::new() - .write(true) + fn append_to_csv(record: LogMessage, path: &Path) -> Result<(), csv::Error> { + let file = OpenOptions::new() .append(true) - .open(path) - .unwrap(); + .create(true) + .open(path)?; - if let Err(e) = writeln!(file, "{}", contents) { - eprintln!("Couldn't write to file: {}", e); - } + let mut writer = csv::Writer::from_writer(file); + + writer.serialize(( + record.id, + record.name, + record.data + ))?; + writer.flush()?; + + Ok(()) } - pub fn create_csv(file_location: &String) { - let mut file = File::create(file_location); + fn create_csv(file_location: &String) -> Result<(), csv::Error> { + let file = OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .open(file_location)?; + + let mut writer = csv::Writer::from_writer(file); + writer.write_record(&["id", "name", "data"])?; + writer.flush()?; + + Ok(()) } + /* + fn read_csv_file(file_location: &String) -> Result, csv::Error> { + let mut records: Vec = vec![]; + let file = OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .open(file_location)?; + + let mut reader = csv::Reader::from_reader(file); + + for record in reader.deserialize() { + // println!("Line"); + let record: Record = record?; + records.push(record); + } + Ok(records) + } + */ } impl Inspector for Logger { @@ -97,16 +133,11 @@ impl Inspector for Logger { SaveData::ToFile(file_location) => { file_loc = file_location; }, - _ => { - panic!("Failed to get save_type enum"); - }, } - let intial_data = String::from("id;name;data"); - Logger::append_to_csv(&file_loc, intial_data); for log in self.values.clone() { - let data = format!("{};{};{}", log.id, log.name, log.data); - Logger::append_to_csv(&file_loc, data); + let file_path = Path::new(&file_loc); + Logger::append_to_csv(log, &file_path); } } }