added export_csv to id_store

Also some fixes and formatting
This commit is contained in:
Niklas Kapelle 2025-04-16 18:58:47 +02:00
parent 02337225cd
commit d18b967f39
Signed by: niklas
GPG Key ID: 4EB651B36D841D16

View File

@ -1,72 +1,111 @@
use std::{collections::HashMap, error::Error, fs::{self, read_to_string}, result};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{
collections::{HashMap, HashSet},
error::Error,
fs,
};
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq, Deserialize, Serialize, Hash, Clone, PartialOrd, Ord)]
#[derive(Deserialize, Serialize)] pub struct TellyID(pub String);
struct TellyID (String);
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
struct AttendanceDay { pub struct AttendanceDay {
date: String, date: String,
ids: Vec<TellyID>, ids: Vec<TellyID>,
} }
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
struct IDStore { pub struct IDStore {
days: HashMap<String,AttendanceDay> days: HashMap<String, AttendanceDay>,
} }
impl IDStore { impl IDStore {
pub fn new() -> Self {
fn new() -> Self { IDStore {
IDStore{
days: HashMap::new(), days: HashMap::new(),
} }
} }
fn new_from_json(filepath:&str) -> Result<Self, Box<dyn Error>>{ pub fn new_from_json(filepath: &str) -> Result<Self, Box<dyn Error>> {
let readed_string = fs::read_to_string(filepath)?; let readed_string = fs::read_to_string(filepath)?;
Ok(serde_json::from_str(&readed_string)?) 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(); let day = self.get_current_day();
day.add_id(id); 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(); let current_day = get_day_str();
if self.days.contains_key(&current_day) { if self.days.contains_key(&current_day) {
return self.days.get_mut(&current_day).unwrap(); return self.days.get_mut(&current_day).unwrap();
} }
self.days.insert(current_day.clone(), AttendanceDay::new(&current_day.clone())); self.days.insert(
current_day.clone(),
AttendanceDay::new(&current_day.clone()),
);
self.days.get_mut(&current_day.clone()).unwrap() self.days.get_mut(&current_day.clone()).unwrap()
} }
fn export_jason(&self, filepath:&str) -> Result <(), Box<dyn Error>> { pub fn export_json(&self, filepath: &str) -> Result<(), Box<dyn Error>> {
// Serialize it to a JSON string and safe it in filepath file // 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<String, Box<dyn Error>> {
let seperator = ";";
let mut user_ids: HashSet<TellyID> = HashSet::new();
for day in self.days.values() {
for id in day.ids.iter() {
user_ids.insert(id.clone());
}
}
let mut user_ids: Vec<TellyID> = user_ids.into_iter().collect();
user_ids.sort();
let mut days: Vec<String> = 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 { impl AttendanceDay {
fn new(day: &str) -> Self{ fn new(day: &str) -> Self {
Self{ Self {
date: day.to_owned(), date: day.to_owned(),
ids: Vec::new(), ids: Vec::new(),
} }
} }
fn add_id(&mut self, id: TellyID){ fn add_id(&mut self, id: TellyID) {
if self.ids.contains(&id) { if self.ids.contains(&id) {
return return;
} }
self.ids.push(id); self.ids.push(id);
} }
} }