use crate::digesters::ImmutableFile;
use crate::entities::{HexEncodedDigest, ImmutableFileName};
use async_trait::async_trait;
use std::collections::BTreeMap;
use std::io;
use thiserror::Error;
#[cfg(test)]
pub use mockall::automock;
pub type CacheProviderResult<T> = Result<T, ImmutableDigesterCacheProviderError>;
#[derive(Error, Debug)]
pub enum ImmutableDigesterCacheProviderError {
#[error("Could not store immutable file digests cache")]
Store(#[from] ImmutableDigesterCacheStoreError),
#[error("Could not read immutable file digests cache")]
Get(#[from] ImmutableDigesterCacheGetError),
}
#[derive(Error, Debug)]
pub enum ImmutableDigesterCacheStoreError {
#[error("IO error when storing cache")]
Io(#[from] io::Error),
#[error("IO error when serializing json cache")]
JsonSerialization(#[from] serde_json::Error),
}
#[derive(Error, Debug)]
pub enum ImmutableDigesterCacheGetError {
#[error("IO error when getting cache")]
Io(#[from] io::Error),
#[error("IO error when deserializing json cache")]
JsonDeserialization(#[from] serde_json::Error),
}
#[cfg_attr(test, automock)]
#[async_trait]
pub trait ImmutableFileDigestCacheProvider: Sync + Send {
async fn store(
&self,
digest_per_filenames: Vec<(ImmutableFileName, HexEncodedDigest)>,
) -> CacheProviderResult<()>;
async fn get(
&self,
immutables: Vec<ImmutableFile>,
) -> CacheProviderResult<BTreeMap<ImmutableFile, Option<HexEncodedDigest>>>;
async fn reset(&self) -> CacheProviderResult<()>;
}