diff --git a/src/bin/main.rs b/src/bin/main.rs index 0ba3ae3..f3ae829 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -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) { diff --git a/src/bin/store/id_mapping.rs b/src/bin/store/id_mapping.rs new file mode 100644 index 0000000..de25995 --- /dev/null +++ b/src/bin/store/id_mapping.rs @@ -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, +} + +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); + } +} + diff --git a/src/bin/store/id_store.rs b/src/bin/store/id_store.rs new file mode 100644 index 0000000..bc82b35 --- /dev/null +++ b/src/bin/store/id_store.rs @@ -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, +} + +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, + 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() + } +} diff --git a/src/bin/store/mod.rs b/src/bin/store/mod.rs new file mode 100644 index 0000000..d72bfee --- /dev/null +++ b/src/bin/store/mod.rs @@ -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;