Skip to main content

lxmf_embedded_mini/
store.rs

1use crate::MiniResult;
2
3pub trait MiniStore {
4    fn load_replay_floor(&self, identity: &[u8; 16]) -> MiniResult<u64>;
5    fn save_replay_floor(&mut self, identity: &[u8; 16], floor: u64) -> MiniResult<()>;
6}
7
8#[derive(Debug, Clone, Copy, Default)]
9pub struct NullStore;
10
11impl MiniStore for NullStore {
12    fn load_replay_floor(&self, _identity: &[u8; 16]) -> MiniResult<u64> {
13        Ok(0)
14    }
15
16    fn save_replay_floor(&mut self, _identity: &[u8; 16], _floor: u64) -> MiniResult<()> {
17        Ok(())
18    }
19}
20
21#[derive(Debug, Clone, Copy)]
22pub struct RamReplayStore {
23    identity: [u8; 16],
24    replay_floor: u64,
25    initialized: bool,
26}
27
28impl RamReplayStore {
29    pub const fn new() -> Self {
30        Self { identity: [0; 16], replay_floor: 0, initialized: false }
31    }
32}
33
34impl Default for RamReplayStore {
35    fn default() -> Self {
36        Self::new()
37    }
38}
39
40impl MiniStore for RamReplayStore {
41    fn load_replay_floor(&self, identity: &[u8; 16]) -> MiniResult<u64> {
42        if self.initialized && &self.identity == identity {
43            Ok(self.replay_floor)
44        } else {
45            Ok(0)
46        }
47    }
48
49    fn save_replay_floor(&mut self, identity: &[u8; 16], floor: u64) -> MiniResult<()> {
50        self.identity = *identity;
51        self.replay_floor = floor;
52        self.initialized = true;
53        Ok(())
54    }
55}