use stateset_core::{
CaptureStockSnapshot, Result, StockSnapshot, StockSnapshotFilter, StockSnapshotId,
};
use stateset_db::{Database, DatabaseCapability};
use std::sync::Arc;
pub struct StockSnapshots {
db: Arc<dyn Database>,
}
impl std::fmt::Debug for StockSnapshots {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("StockSnapshots").finish_non_exhaustive()
}
}
impl StockSnapshots {
pub(crate) fn new(db: Arc<dyn Database>) -> Self {
Self { db }
}
#[must_use]
pub fn is_supported(&self) -> bool {
self.db.supports_capability(DatabaseCapability::StockSnapshots)
}
fn ensure(&self) -> Result<()> {
self.db.ensure_capability(DatabaseCapability::StockSnapshots)
}
pub fn capture(&self, input: CaptureStockSnapshot) -> Result<StockSnapshot> {
self.ensure()?;
self.db.stock_snapshots().capture(input)
}
pub fn get(&self, id: StockSnapshotId) -> Result<Option<StockSnapshot>> {
self.ensure()?;
self.db.stock_snapshots().get(id)
}
pub fn latest(&self) -> Result<Option<StockSnapshot>> {
self.ensure()?;
self.db.stock_snapshots().latest()
}
pub fn list(&self, filter: StockSnapshotFilter) -> Result<Vec<StockSnapshot>> {
self.ensure()?;
self.db.stock_snapshots().list(filter)
}
pub fn delete(&self, id: StockSnapshotId) -> Result<()> {
self.ensure()?;
self.db.stock_snapshots().delete(id)
}
}