changed Day implementation

This commit is contained in:
2025-10-10 01:12:46 +02:00
parent 030a372949
commit f1b471c6d8
7 changed files with 85 additions and 47 deletions

63
src/store/day.rs Normal file
View File

@@ -0,0 +1,63 @@
use core::fmt::Write;
use embedded_sdmmc::ShortFileName;
use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Serialize, Deserialize, Debug, PartialEq, Eq)]
pub struct Day(u32);
impl Day {
const SECONDS_PER_DAY: u64 = 86_400;
pub fn new(daystamp: u32) -> Self {
Day(daystamp)
}
pub fn new_from_timestamp(time: u64) -> Self {
let day = time / Self::SECONDS_PER_DAY;
if day > u32::MAX as u64 {
// TBH this would only happen if about 11 million years have passed
// I sure hope i don't have to work on this project any more then
// So we just cap it at this
Day(u32::MAX)
} else {
Day(day as u32)
}
}
pub fn to_timestamp(self) -> u64 {
(self.0 as u64) * Self::SECONDS_PER_DAY
}
pub fn to_string(self) -> heapless::String<8> {
let mut s: heapless::String<8> = heapless::String::new();
write!(s, "{:08X}", self.0).unwrap();
s
}
pub fn from_hex_str(s: &str) -> Result<Self, &'static str> {
if s.len() > 8 {
return Err("hex string too long");
}
u32::from_str_radix(s, 16)
.map_err(|_| "invalid hex string")
.map(Day)
}
}
impl From<u64> for Day {
fn from(value: u64) -> Self {
Self::new_from_timestamp(value)
}
}
impl TryFrom<ShortFileName> for Day {
type Error = ();
fn try_from(value: ShortFileName) -> Result<Self, Self::Error> {
let name = core::str::from_utf8(value.base_name()).map_err(|_| ())?;
Self::from_hex_str(name).map_err(|_| ())
}
}

View File

@@ -2,19 +2,19 @@ use alloc::vec::Vec;
use serde::Deserialize;
use serde::Serialize;
use super::Date;
use super::IDMapping;
use crate::store::day::Day;
use crate::store::persistence::Persistence;
use crate::store::tally_id::TallyID;
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct AttendanceDay {
date: Date,
date: Day,
ids: Vec<TallyID>,
}
impl AttendanceDay {
pub fn new(date: Date) -> Self {
pub fn new(date: Day) -> Self {
Self {
date,
ids: Vec::new(),
@@ -46,7 +46,7 @@ impl<T: Persistence> IDStore<T> {
// None => IDMapping::new(),
// };
let current_date: Date = [0; 10];
let current_date: Day = Day::new(0);
let day = persistence_layer
.load_day(current_date)
@@ -72,7 +72,7 @@ impl<T: Persistence> IDStore<T> {
/// 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: Date) -> bool {
pub async fn add_id(&mut self, id: TallyID, current_date: Day) -> bool {
if self.current_day.date == current_date {
let changed = self.current_day.add_id(id);
if changed {

View File

@@ -5,6 +5,5 @@ mod id_mapping;
pub mod persistence;
mod id_store;
pub mod tally_id;
pub type Date = [u8; 10];
pub mod day;

View File

@@ -1,11 +1,11 @@
use alloc::vec::Vec;
use crate::store::{Date, IDMapping, id_store::AttendanceDay};
use crate::store::{IDMapping, day::Day, id_store::AttendanceDay};
pub trait Persistence {
async fn load_day(&mut self, day: Date) -> Option<AttendanceDay>;
async fn save_day(&mut self, day: Date, data: &AttendanceDay);
async fn list_days(&mut self) -> Vec<Date>;
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);