Skip to main content

oxabl_cache_crypto/
record_entry.rs

1//! The content of a record, as owned values.
2
3use oxabl_cache_format::{EntryFile, EntryPayload, PublicKey, SegmentId, Signature};
4
5/// One file of an artifact set.
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct FileEntry {
8    /// Relative artifact path.
9    pub path: String,
10    pub size: u64,
11}
12
13/// What a record says about a segment, apart from who signed it.
14#[derive(Debug, Clone, PartialEq, Eq)]
15pub struct EntryFields {
16    pub segment_id: SegmentId,
17    pub segment_len: u32,
18    pub dict_id: Option<SegmentId>,
19    /// Sum of the file sizes.
20    pub plain_len: u64,
21    pub created_at: i64,
22    pub roster_version: u64,
23    pub files: Vec<FileEntry>,
24}
25
26/// A record's content with the key that signed it.
27#[derive(Debug, Clone, PartialEq, Eq)]
28pub struct Entry {
29    pub fields: EntryFields,
30    pub signer: PublicKey,
31}
32
33impl EntryFields {
34    pub(crate) fn payload(&self, signer: PublicKey, signature: Signature) -> EntryPayload<'_> {
35        EntryPayload {
36            segment_id: self.segment_id,
37            segment_len: self.segment_len,
38            dict_id: self.dict_id,
39            plain_len: self.plain_len,
40            created_at: self.created_at,
41            roster_version: self.roster_version,
42            signer,
43            files: self
44                .files
45                .iter()
46                .map(|f| EntryFile {
47                    path: &f.path,
48                    size: f.size,
49                })
50                .collect(),
51            signature,
52        }
53    }
54
55    pub(crate) fn from_payload(payload: &EntryPayload<'_>) -> Self {
56        Self {
57            segment_id: payload.segment_id,
58            segment_len: payload.segment_len,
59            dict_id: payload.dict_id,
60            plain_len: payload.plain_len,
61            created_at: payload.created_at,
62            roster_version: payload.roster_version,
63            files: payload
64                .files
65                .iter()
66                .map(|f| FileEntry {
67                    path: f.path.to_owned(),
68                    size: f.size,
69                })
70                .collect(),
71        }
72    }
73}