mithril_aggregator/database/record/
immutable_file_digest.rs

1use sqlite::Row;
2
3use mithril_common::entities::{HexEncodedDigest, ImmutableFileName};
4use mithril_persistence::sqlite::{HydrationError, Projection, SourceAlias, SqLiteEntity};
5
6/// ImmutableFileDigestRecord is the record that stores the digest of an immutable file.
7#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
8pub struct ImmutableFileDigestRecord {
9    /// Immutable file name
10    pub immutable_file_name: ImmutableFileName,
11
12    /// Digest of an immutable file
13    pub digest: HexEncodedDigest,
14}
15
16impl ImmutableFileDigestRecord {
17    /// Construct a [Projection] that will allow to hydrate this `ImmutableFileDigestRecord` and expend table alias.
18    pub fn expand_projection(table: &str) -> String {
19        let aliases = SourceAlias::new(&[("{:immutable_file_digest:}", table)]);
20        Self::get_projection().expand(aliases)
21    }
22
23    #[cfg(test)]
24    /// Create a dumb ImmutableFileDigestRecord instance mainly for test purposes
25    pub fn dummy() -> Self {
26        Self {
27            immutable_file_name: "123.chunk".to_string(),
28            digest: "dummy_digest".to_string(),
29        }
30    }
31}
32
33impl SqLiteEntity for ImmutableFileDigestRecord {
34    fn hydrate(row: Row) -> Result<Self, HydrationError>
35    where
36        Self: Sized,
37    {
38        let immutable_file_name = row.read::<&str, _>(0).to_string();
39        let digest = row.read::<&str, _>(1).to_string();
40
41        Ok(Self {
42            immutable_file_name,
43            digest,
44        })
45    }
46
47    fn get_projection() -> Projection {
48        Projection::from(&[
49            (
50                "immutable_file_name",
51                "{:immutable_file_digest:}.immutable_file_name",
52                "text",
53            ),
54            ("digest", "{:immutable_file_digest:}.digest", "text"),
55        ])
56    }
57}