Skip to main content

stateset_embedded/
stock_snapshots.rs

1//! Stock snapshot operations (point-in-time inventory).
2
3use stateset_core::{
4    CaptureStockSnapshot, Result, StockSnapshot, StockSnapshotFilter, StockSnapshotId,
5};
6use stateset_db::{Database, DatabaseCapability};
7use std::sync::Arc;
8
9/// Stock snapshot operations.
10pub struct StockSnapshots {
11    db: Arc<dyn Database>,
12}
13
14impl std::fmt::Debug for StockSnapshots {
15    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16        f.debug_struct("StockSnapshots").finish_non_exhaustive()
17    }
18}
19
20impl StockSnapshots {
21    pub(crate) fn new(db: Arc<dyn Database>) -> Self {
22        Self { db }
23    }
24
25    /// Whether stock snapshots are supported by the active backend.
26    #[must_use]
27    pub fn is_supported(&self) -> bool {
28        self.db.supports_capability(DatabaseCapability::StockSnapshots)
29    }
30
31    fn ensure(&self) -> Result<()> {
32        self.db.ensure_capability(DatabaseCapability::StockSnapshots)
33    }
34
35    /// Capture a new stock snapshot (totals computed from lines).
36    pub fn capture(&self, input: CaptureStockSnapshot) -> Result<StockSnapshot> {
37        self.ensure()?;
38        self.db.stock_snapshots().capture(input)
39    }
40
41    /// Get a snapshot by ID.
42    pub fn get(&self, id: StockSnapshotId) -> Result<Option<StockSnapshot>> {
43        self.ensure()?;
44        self.db.stock_snapshots().get(id)
45    }
46
47    /// Get the most recent snapshot.
48    pub fn latest(&self) -> Result<Option<StockSnapshot>> {
49        self.ensure()?;
50        self.db.stock_snapshots().latest()
51    }
52
53    /// List snapshots (header-level) with optional pagination.
54    pub fn list(&self, filter: StockSnapshotFilter) -> Result<Vec<StockSnapshot>> {
55        self.ensure()?;
56        self.db.stock_snapshots().list(filter)
57    }
58
59    /// Delete a snapshot.
60    pub fn delete(&self, id: StockSnapshotId) -> Result<()> {
61        self.ensure()?;
62        self.db.stock_snapshots().delete(id)
63    }
64}