mirror of
				https://github.com/Djeeberjr/fw-anwesenheit.git
				synced 2025-11-04 07:34:10 +00:00 
			
		
		
		
	first implementation of IDStore and IDmapping
This commit is contained in:
		
							parent
							
								
									c91d2f070f
								
							
						
					
					
						commit
						4b39529a65
					
				@ -14,6 +14,7 @@ use crate::webserver::start_webserver;
 | 
			
		||||
 | 
			
		||||
mod init;
 | 
			
		||||
mod webserver;
 | 
			
		||||
mod store;
 | 
			
		||||
 | 
			
		||||
#[esp_hal_embassy::main]
 | 
			
		||||
async fn main(mut spawner: Spawner) {
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										31
									
								
								src/bin/store/id_mapping.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								src/bin/store/id_mapping.rs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,31 @@
 | 
			
		||||
extern crate alloc;
 | 
			
		||||
 | 
			
		||||
use super::TallyID;
 | 
			
		||||
use alloc::collections::BTreeMap;
 | 
			
		||||
use alloc::string::String;
 | 
			
		||||
 | 
			
		||||
pub struct Name {
 | 
			
		||||
    pub first: String,
 | 
			
		||||
    pub last: String,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub struct IDMapping {
 | 
			
		||||
    id_map: BTreeMap<TallyID, Name>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl IDMapping {
 | 
			
		||||
    pub fn new() -> Self {
 | 
			
		||||
        IDMapping {
 | 
			
		||||
            id_map: BTreeMap::new(),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn map(&self, id: &TallyID) -> Option<&Name> {
 | 
			
		||||
        self.id_map.get(id)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn add_mapping(&mut self, id: TallyID, name: Name) {
 | 
			
		||||
        self.id_map.insert(id, name);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										71
									
								
								src/bin/store/id_store.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										71
									
								
								src/bin/store/id_store.rs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,71 @@
 | 
			
		||||
extern crate alloc;
 | 
			
		||||
 | 
			
		||||
use super::Date;
 | 
			
		||||
use super::IDMapping;
 | 
			
		||||
use super::TallyID;
 | 
			
		||||
use alloc::collections::BTreeMap;
 | 
			
		||||
use alloc::vec::Vec;
 | 
			
		||||
 | 
			
		||||
pub struct AttendanceDay {
 | 
			
		||||
    date: Date,
 | 
			
		||||
    ids: Vec<TallyID>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl AttendanceDay {
 | 
			
		||||
    fn new(date: Date) -> Self {
 | 
			
		||||
        Self {
 | 
			
		||||
            date,
 | 
			
		||||
            ids: Vec::new(),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Add an ID to the day.
 | 
			
		||||
    // Returns false if ID was already present
 | 
			
		||||
    fn add_id(&mut self, id: TallyID) -> bool {
 | 
			
		||||
        if self.ids.contains(&id) {
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
        self.ids.push(id);
 | 
			
		||||
        true
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub struct IDStore {
 | 
			
		||||
    days: BTreeMap<Date, AttendanceDay>,
 | 
			
		||||
    mapping: IDMapping,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl IDStore {
 | 
			
		||||
    pub fn new() -> Self {
 | 
			
		||||
        IDStore {
 | 
			
		||||
            days: BTreeMap::new(),
 | 
			
		||||
            mapping: IDMapping::new(),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn new_from_storage() -> Self {
 | 
			
		||||
        // TODO: implement
 | 
			
		||||
        todo!()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Add a new id for the current day
 | 
			
		||||
    /// Returns false if ID is already present at the current day.
 | 
			
		||||
    pub fn add_id(&mut self, id: TallyID) -> bool {
 | 
			
		||||
        self.get_current_day().add_id(id)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Get the `AttendanceDay` of the current day
 | 
			
		||||
    /// Creates a new if not exists
 | 
			
		||||
    pub fn get_current_day(&mut self) -> &mut AttendanceDay {
 | 
			
		||||
        let current_day: Date = 1;
 | 
			
		||||
 | 
			
		||||
        if self.days.contains_key(¤t_day) {
 | 
			
		||||
            return self.days.get_mut(¤t_day).unwrap();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        self.days
 | 
			
		||||
            .insert(current_day, AttendanceDay::new(current_day));
 | 
			
		||||
 | 
			
		||||
        self.days.get_mut(¤t_day.clone()).unwrap()
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										8
									
								
								src/bin/store/mod.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								src/bin/store/mod.rs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,8 @@
 | 
			
		||||
mod id_mapping;
 | 
			
		||||
mod id_store;
 | 
			
		||||
 | 
			
		||||
pub use id_mapping::{IDMapping, Name};
 | 
			
		||||
pub use id_store::IDStore;
 | 
			
		||||
 | 
			
		||||
pub type TallyID = [u8; 8];
 | 
			
		||||
pub type Date = u64;
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user