mithril_common/digesters/cache/
provider.rs

1use std::collections::BTreeMap;
2use std::io;
3
4use async_trait::async_trait;
5use thiserror::Error;
6
7use crate::digesters::ImmutableFile;
8use crate::entities::{HexEncodedDigest, ImmutableFileName};
9use crate::StdError;
10
11/// A specialized result type for [ImmutableFileDigestCacheProvider].
12pub type CacheProviderResult<T> = Result<T, ImmutableDigesterCacheProviderError>;
13
14/// [ImmutableFileDigestCacheProvider] related errors.
15#[derive(Error, Debug)]
16pub enum ImmutableDigesterCacheProviderError {
17    /// Error raised by [ImmutableFileDigestCacheProvider::store].
18    #[error("Could not store immutable file digests cache")]
19    Store(#[from] ImmutableDigesterCacheStoreError),
20
21    /// Error raised by [ImmutableFileDigestCacheProvider::get].
22    #[error("Could not read immutable file digests cache")]
23    Get(#[from] ImmutableDigesterCacheGetError),
24}
25
26/// [ImmutableFileDigestCacheProvider::store] related errors.
27#[derive(Error, Debug)]
28pub enum ImmutableDigesterCacheStoreError {
29    /// Raised when an IO error is raised when storing a cache.
30    #[error("IO error when storing cache")]
31    Io(#[from] io::Error),
32
33    /// Raised when json cache serialization fails.
34    #[error("IO error when serializing json cache")]
35    JsonSerialization(#[from] serde_json::Error),
36
37    /// Raised when the underlying store failed storing a cache.
38    #[error("Underlying store raised an error when storing cache")]
39    StoreError(#[source] StdError),
40}
41
42/// [ImmutableFileDigestCacheProvider::get] related errors.
43#[derive(Error, Debug)]
44pub enum ImmutableDigesterCacheGetError {
45    /// Raised when an IO error is raised when getting a cache.
46    #[error("IO error when getting cache")]
47    Io(#[from] io::Error),
48
49    /// Raised when json cache deserialization fails.
50    #[error("IO error when deserializing json cache")]
51    JsonDeserialization(#[from] serde_json::Error),
52
53    /// Raised when the underlying store failed getting a cache.
54    #[error("Underlying store raised an error when getting cache")]
55    StoreError(#[source] StdError),
56}
57
58/// A cache provider that store individual [ImmutableFile] digests.
59#[cfg_attr(test, mockall::automock)]
60#[async_trait]
61pub trait ImmutableFileDigestCacheProvider: Sync + Send {
62    /// Store the given digests
63    async fn store(
64        &self,
65        digest_per_filenames: Vec<(ImmutableFileName, HexEncodedDigest)>,
66    ) -> CacheProviderResult<()>;
67
68    /// Associate each given [immutable files][ImmutableFile] with a cached value if one exist.
69    async fn get(
70        &self,
71        immutables: Vec<ImmutableFile>,
72    ) -> CacheProviderResult<BTreeMap<ImmutableFile, Option<HexEncodedDigest>>>;
73
74    /// Reset the stored values
75    async fn reset(&self) -> CacheProviderResult<()>;
76}