improved error handeling for sdcard functions

This commit is contained in:
2025-11-03 18:35:10 +01:00
parent 07d51264f9
commit 02798d90c4
6 changed files with 206 additions and 130 deletions

View File

@@ -58,7 +58,7 @@ impl<T: Persistence> IDStore<T> {
}
}
async fn persist_day(&mut self) {
async fn persist_day(&mut self) -> Result<(), T::Error> {
self.persistence_layer
.lock()
.await
@@ -88,21 +88,25 @@ impl<T: Persistence> IDStore<T> {
}
/// Load and return a AttendanceDay. Nothing more. Nothing less.
pub async fn load_day(&mut self, day: Day) -> Option<AttendanceDay> {
pub async fn load_day(&mut self, day: Day) -> Result<AttendanceDay, T::Error> {
if day == self.current_day.date {
return Some(self.current_day.clone());
return Ok(self.current_day.clone());
}
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.lock().await.list_days().await;
pub async fn list_days_in_timespan(
&mut self,
from: Day,
to: Day,
) -> Result<Vec<Day>, T::Error> {
let all_days = self.persistence_layer.lock().await.list_days().await?;
all_days
Ok(all_days
.into_iter()
.filter(|e| *e >= from)
.filter(|e| *e <= to)
.collect()
.collect())
}
}

View File

@@ -17,15 +17,15 @@ impl<T: Persistence> MappingLoader<T> {
Self(persistence_layer)
}
pub async fn get_mapping(&self, id: TallyID) -> Option<Name> {
pub async fn get_mapping(&self, id: TallyID) -> Result<Name, T::Error> {
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 set_mapping(&self, id: TallyID, name: Name) -> Result<(), T::Error> {
self.0.lock().await.save_mapping_for_id(id, name).await
}
pub async fn list_mappings(&self) -> Vec<TallyID> {
pub async fn list_mappings(&self) -> Result<Vec<TallyID>,T::Error> {
self.0.lock().await.list_mappings().await
}
}

View File

@@ -3,11 +3,13 @@ use alloc::vec::Vec;
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>;
type Error: core::error::Error;
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>;
async fn load_day(&mut self, day: Day) -> Result<AttendanceDay, Self::Error>;
async fn save_day(&mut self, day: Day, data: &AttendanceDay) -> Result<(), Self::Error>;
async fn list_days(&mut self) -> Result<Vec<Day>, Self::Error>;
async fn load_mapping_for_id(&mut self, id: TallyID) -> Result<Name, Self::Error>;
async fn save_mapping_for_id(&mut self, id: TallyID, name: Name) -> Result<(), Self::Error>;
async fn list_mappings(&mut self) -> Result<Vec<TallyID>, Self::Error>;
}