mithril_common/digesters/cache/
provider.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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;

/// A specialized result type for [ImmutableFileDigestCacheProvider].
pub type CacheProviderResult<T> = Result<T, ImmutableDigesterCacheProviderError>;

/// [ImmutableFileDigestCacheProvider] related errors.
#[derive(Error, Debug)]
pub enum ImmutableDigesterCacheProviderError {
    /// Error raised by [ImmutableFileDigestCacheProvider::store].
    #[error("Could not store immutable file digests cache")]
    Store(#[from] ImmutableDigesterCacheStoreError),

    /// Error raised by [ImmutableFileDigestCacheProvider::get].
    #[error("Could not read immutable file digests cache")]
    Get(#[from] ImmutableDigesterCacheGetError),
}

/// [ImmutableFileDigestCacheProvider::store] related errors.
#[derive(Error, Debug)]
pub enum ImmutableDigesterCacheStoreError {
    /// Raised when an IO error is raised when storing a cache.
    #[error("IO error when storing cache")]
    Io(#[from] io::Error),

    /// Raised when json cache serialization fails.
    #[error("IO error when serializing json cache")]
    JsonSerialization(#[from] serde_json::Error),
}

/// [ImmutableFileDigestCacheProvider::get] related errors.
#[derive(Error, Debug)]
pub enum ImmutableDigesterCacheGetError {
    /// Raised when an IO error is raised when getting a cache.
    #[error("IO error when getting cache")]
    Io(#[from] io::Error),

    /// Raised when json cache deserialization fails.
    #[error("IO error when deserializing json cache")]
    JsonDeserialization(#[from] serde_json::Error),
}

/// A cache provider that store individual [ImmutableFile] digests.
#[cfg_attr(test, automock)]
#[async_trait]
pub trait ImmutableFileDigestCacheProvider: Sync + Send {
    /// Store the given digests
    async fn store(
        &self,
        digest_per_filenames: Vec<(ImmutableFileName, HexEncodedDigest)>,
    ) -> CacheProviderResult<()>;

    /// Associate each given [immutable files][ImmutableFile] with a cached value if one exist.
    async fn get(
        &self,
        immutables: Vec<ImmutableFile>,
    ) -> CacheProviderResult<BTreeMap<ImmutableFile, Option<HexEncodedDigest>>>;

    /// Reset the stored values
    async fn reset(&self) -> CacheProviderResult<()>;
}