mirror of
				https://github.com/Djeeberjr/fw-anwesenheit.git
				synced 2025-11-03 23:24:10 +00:00 
			
		
		
		
	added export_csv to id_store
Also some fixes and formatting
This commit is contained in:
		
							parent
							
								
									02337225cd
								
							
						
					
					
						commit
						d18b967f39
					
				@ -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<TellyID>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Deserialize, Serialize)]
 | 
			
		||||
struct IDStore {
 | 
			
		||||
    days: HashMap<String,AttendanceDay>
 | 
			
		||||
pub struct IDStore {
 | 
			
		||||
    days: HashMap<String, AttendanceDay>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl IDStore {
 | 
			
		||||
    
 | 
			
		||||
    fn new() -> Self {
 | 
			
		||||
        IDStore{
 | 
			
		||||
    pub fn new() -> Self {
 | 
			
		||||
        IDStore {
 | 
			
		||||
            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)?;
 | 
			
		||||
        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<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
 | 
			
		||||
        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 {
 | 
			
		||||
    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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user