use anyhow::Result;
use super::types::SessionCost;
#[allow(async_fn_in_trait)]
pub trait CostSink {
async fn save(&self, costs: &[SessionCost]) -> Result<()>;
async fn load(&self) -> Result<Vec<SessionCost>>;
}
#[derive(Debug)]
pub struct InMemoryCostSink {
inner: tokio::sync::Mutex<Vec<SessionCost>>,
}
impl Default for InMemoryCostSink {
fn default() -> Self {
Self::new()
}
}
impl InMemoryCostSink {
pub fn new() -> Self {
Self {
inner: tokio::sync::Mutex::new(Vec::new()),
}
}
}
impl CostSink for InMemoryCostSink {
async fn save(&self, costs: &[SessionCost]) -> Result<()> {
let mut guard = self.inner.lock().await;
guard.clear();
guard.extend_from_slice(costs);
Ok(())
}
async fn load(&self) -> Result<Vec<SessionCost>> {
let guard = self.inner.lock().await;
Ok(guard.clone())
}
}