mirror of
https://github.com/Djeeberjr/fw-anwesenheit.git
synced 2026-04-30 18:49:09 +00:00
changed storage method of ID mappings
This commit is contained in:
@@ -1,33 +0,0 @@
|
||||
use alloc::collections::BTreeMap;
|
||||
use alloc::string::String;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::store::tally_id::TallyID;
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize)]
|
||||
pub struct Name {
|
||||
pub first: String,
|
||||
pub last: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize)]
|
||||
pub struct IDMapping {
|
||||
#[serde(flatten)]
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
use alloc::rc::Rc;
|
||||
use alloc::vec::Vec;
|
||||
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
||||
use embassy_sync::mutex::Mutex;
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
|
||||
use super::IDMapping;
|
||||
use crate::store::day::Day;
|
||||
use crate::store::persistence::Persistence;
|
||||
use crate::store::tally_id::TallyID;
|
||||
@@ -34,40 +36,36 @@ impl AttendanceDay {
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct IDStore<T: Persistence> {
|
||||
pub current_day: AttendanceDay,
|
||||
pub mapping: IDMapping,
|
||||
persistence_layer: T,
|
||||
current_day: AttendanceDay,
|
||||
persistence_layer: Rc<Mutex<CriticalSectionRawMutex, T>>,
|
||||
}
|
||||
|
||||
impl<T: Persistence> IDStore<T> {
|
||||
pub async fn new_from_storage(mut persistence_layer: T, current_date: Day) -> Self {
|
||||
let mapping = match persistence_layer.load_mapping().await {
|
||||
Some(map) => map,
|
||||
None => IDMapping::new(),
|
||||
};
|
||||
|
||||
pub async fn new_from_storage(
|
||||
persistence_layer: Rc<Mutex<CriticalSectionRawMutex, T>>,
|
||||
current_date: Day,
|
||||
) -> Self {
|
||||
let day = persistence_layer
|
||||
.lock()
|
||||
.await
|
||||
.load_day(current_date)
|
||||
.await
|
||||
.unwrap_or(AttendanceDay::new(current_date));
|
||||
|
||||
Self {
|
||||
current_day: day,
|
||||
mapping,
|
||||
persistence_layer,
|
||||
}
|
||||
}
|
||||
|
||||
async fn persist_day(&mut self) {
|
||||
self.persistence_layer
|
||||
.lock()
|
||||
.await
|
||||
.save_day(self.current_day.date, &self.current_day)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn persist_mapping(&mut self) {
|
||||
self.persistence_layer.save_mapping(&self.mapping).await
|
||||
}
|
||||
|
||||
/// Add a new id for the current day
|
||||
/// Returns false if ID is already present at the current day.
|
||||
pub async fn add_id(&mut self, id: TallyID, current_date: Day) -> bool {
|
||||
@@ -95,11 +93,11 @@ impl<T: Persistence> IDStore<T> {
|
||||
return Some(self.current_day.clone());
|
||||
}
|
||||
|
||||
self.persistence_layer.load_day(day).await
|
||||
self.persistence_layer.lock().await.load_day(day).await
|
||||
}
|
||||
|
||||
pub async fn list_days_in_timespan(&mut self, from: Day, to: Day) -> Vec<Day> {
|
||||
let all_days = self.persistence_layer.list_days().await;
|
||||
let all_days = self.persistence_layer.lock().await.list_days().await;
|
||||
|
||||
all_days
|
||||
.into_iter()
|
||||
|
||||
31
src/store/mapping_loader.rs
Normal file
31
src/store/mapping_loader.rs
Normal file
@@ -0,0 +1,31 @@
|
||||
use alloc::{rc::Rc, string::String, vec::Vec};
|
||||
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, mutex::Mutex};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::store::{persistence::Persistence, tally_id::TallyID};
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize)]
|
||||
pub struct Name {
|
||||
pub first: String,
|
||||
pub last: String,
|
||||
}
|
||||
|
||||
pub struct MappingLoader<T: Persistence>(Rc<Mutex<CriticalSectionRawMutex, T>>);
|
||||
|
||||
impl<T: Persistence> MappingLoader<T> {
|
||||
pub fn new(persistence_layer: Rc<Mutex<CriticalSectionRawMutex, T>>) -> Self {
|
||||
Self(persistence_layer)
|
||||
}
|
||||
|
||||
pub async fn get_mapping(&self, id: TallyID) -> Option<Name> {
|
||||
self.0.lock().await.load_mapping_for_id(id).await
|
||||
}
|
||||
|
||||
pub async fn set_mapping(&self, id: TallyID, name: Name) {
|
||||
self.0.lock().await.save_mapping_for_id(id, name).await;
|
||||
}
|
||||
|
||||
pub async fn list_mappings(&self) -> Vec<TallyID> {
|
||||
self.0.lock().await.list_mappings().await
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,7 @@
|
||||
pub use id_mapping::{IDMapping, Name};
|
||||
pub use id_store::{IDStore,AttendanceDay};
|
||||
|
||||
mod id_mapping;
|
||||
pub mod persistence;
|
||||
mod id_store;
|
||||
pub mod tally_id;
|
||||
pub mod day;
|
||||
|
||||
pub mod mapping_loader;
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
use alloc::vec::Vec;
|
||||
|
||||
use crate::store::{IDMapping, day::Day, id_store::AttendanceDay};
|
||||
use crate::store::{day::Day, id_store::AttendanceDay, mapping_loader::Name, tally_id::TallyID};
|
||||
|
||||
pub trait Persistence {
|
||||
async fn load_day(&mut self, day: Day) -> Option<AttendanceDay>;
|
||||
async fn save_day(&mut self, day: Day, data: &AttendanceDay);
|
||||
async fn list_days(&mut self) -> Vec<Day>;
|
||||
|
||||
async fn load_mapping(&mut self) -> Option<IDMapping>;
|
||||
async fn save_mapping(&mut self, data: &IDMapping);
|
||||
async fn load_mapping_for_id(&mut self, id:TallyID ) -> Option<Name>;
|
||||
async fn save_mapping_for_id(&mut self, id:TallyID, name: Name);
|
||||
async fn list_mappings(&mut self) -> Vec<TallyID>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user