From d18b967f393147a3577dfadeee1440e155c058bc Mon Sep 17 00:00:00 2001 From: Niklas Kapelle Date: Wed, 16 Apr 2025 18:58:47 +0200 Subject: [PATCH] added export_csv to id_store Also some fixes and formatting --- src/id_store.rs | 85 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 62 insertions(+), 23 deletions(-) diff --git a/src/id_store.rs b/src/id_store.rs index bd1f57f..8145f29 100644 --- a/src/id_store.rs +++ b/src/id_store.rs @@ -1,72 +1,111 @@ -use std::{collections::HashMap, error::Error, fs::{self, read_to_string}, result}; use serde::{Deserialize, Serialize}; +use std::{ + collections::{HashMap, HashSet}, + error::Error, + fs, +}; -#[derive(PartialEq, Eq)] -#[derive(Deserialize, Serialize)] -struct TellyID (String); +#[derive(PartialEq, Eq, Deserialize, Serialize, Hash, Clone, PartialOrd, Ord)] +pub struct TellyID(pub String); #[derive(Deserialize, Serialize)] -struct AttendanceDay { +pub struct AttendanceDay { date: String, ids: Vec, } #[derive(Deserialize, Serialize)] -struct IDStore { - days: HashMap +pub struct IDStore { + days: HashMap, } impl IDStore { - - fn new() -> Self { - IDStore{ + pub fn new() -> Self { + IDStore { days: HashMap::new(), } } - fn new_from_json(filepath:&str) -> Result>{ + pub fn new_from_json(filepath: &str) -> Result> { let readed_string = fs::read_to_string(filepath)?; Ok(serde_json::from_str(&readed_string)?) } - fn add_id(&mut self, id: TellyID){ + pub fn add_id(&mut self, id: TellyID) { let day = self.get_current_day(); day.add_id(id); - } - fn get_current_day(&mut self) -> &mut AttendanceDay { + pub fn get_current_day(&mut self) -> &mut AttendanceDay { let current_day = get_day_str(); if self.days.contains_key(¤t_day) { return self.days.get_mut(¤t_day).unwrap(); } - self.days.insert(current_day.clone(), AttendanceDay::new(¤t_day.clone())); + self.days.insert( + current_day.clone(), + AttendanceDay::new(¤t_day.clone()), + ); self.days.get_mut(¤t_day.clone()).unwrap() } - fn export_jason(&self, filepath:&str) -> Result <(), Box> { - + pub fn export_json(&self, filepath: &str) -> Result<(), Box> { // Serialize it to a JSON string and safe it in filepath file - Ok(fs::write("attendence_list.json", serde_json::to_string(&self)?)?) + Ok(fs::write(filepath, serde_json::to_string(&self)?)?) + } + + pub fn export_csv(&self) -> Result> { + let seperator = ";"; + let mut user_ids: HashSet = HashSet::new(); + + for day in self.days.values() { + for id in day.ids.iter() { + user_ids.insert(id.clone()); + } + } + + let mut user_ids: Vec = user_ids.into_iter().collect(); + user_ids.sort(); + + let mut days: Vec = self.days.keys().cloned().collect(); + days.sort(); + + let header = days.join(seperator); + println!("ID,{}", header); + + for user_id in user_ids.iter() { + print!("{},", user_id.0); + for day in days.iter() { + let was_there: bool = self.days.get(day).unwrap().ids.contains(user_id); + + if was_there { + print!("{}x", seperator); + } else { + print!("{}", seperator); + } + } + println!(); + } + + Ok("".to_owned()) } } impl AttendanceDay { - fn new(day: &str) -> Self{ - Self{ + fn new(day: &str) -> Self { + Self { date: day.to_owned(), ids: Vec::new(), } } - fn add_id(&mut self, id: TellyID){ + fn add_id(&mut self, id: TellyID) { if self.ids.contains(&id) { - return - } + return; + } self.ids.push(id); } }