1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3use std::sync::{Arc, Mutex};
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
6pub struct LedgerEntry {
7 pub path: String,
8 pub sha256: String,
9 pub mtime_ms: u64,
10 pub size_bytes: u64,
11 pub timestamp_ms: u64,
12}
13
14pub trait Ledger: Send + Sync + std::fmt::Debug {
16 fn get_latest(&self, path: &str) -> Option<LedgerEntry>;
17 fn record(&self, entry: LedgerEntry);
18}
19
20#[derive(Default)]
21pub struct InMemoryLedger {
22 inner: Arc<Mutex<HashMap<String, LedgerEntry>>>,
23}
24
25impl InMemoryLedger {
26 pub fn new() -> Self {
27 Self {
28 inner: Arc::new(Mutex::new(HashMap::new())),
29 }
30 }
31}
32
33impl std::fmt::Debug for InMemoryLedger {
34 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35 f.debug_struct("InMemoryLedger").finish()
36 }
37}
38
39impl Ledger for InMemoryLedger {
40 fn get_latest(&self, path: &str) -> Option<LedgerEntry> {
41 self.inner.lock().unwrap().get(path).cloned()
42 }
43 fn record(&self, entry: LedgerEntry) {
44 self.inner
45 .lock()
46 .unwrap()
47 .insert(entry.path.clone(), entry);
48 }
49}