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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//! File format iterators.
use std::ops::Range;

use crate::Timestamp;
use binary_stream::futures::Decodable;

/// Trait for types yielded by the file iterator.
pub trait FileItem: Default + std::fmt::Debug + Decodable {
    /// Get the byte offset for the record.
    fn offset(&self) -> &Range<u64>;

    /// Get the range for the record value.
    fn value(&self) -> &Range<u64>;

    /// Set the byte offset for the record.
    fn set_offset(&mut self, offset: Range<u64>);

    /// Set the range for the record value.
    fn set_value(&mut self, value: Range<u64>);
}

/// Generic reference to a row in a file.
#[derive(Default, Debug)]
pub struct FileRecord {
    /// Byte offset for the record.
    offset: Range<u64>,
    /// The byte range for the value.
    value: Range<u64>,
}

impl FileItem for FileRecord {
    fn offset(&self) -> &Range<u64> {
        &self.offset
    }

    fn value(&self) -> &Range<u64> {
        &self.value
    }

    fn set_offset(&mut self, offset: Range<u64>) {
        self.offset = offset;
    }

    fn set_value(&mut self, value: Range<u64>) {
        self.value = value;
    }
}

/// Reference to a row in a vault.
#[derive(Default, Debug)]
pub struct VaultRecord {
    /// Byte offset for the record.
    offset: Range<u64>,
    /// The byte range for the value.
    value: Range<u64>,
    /// The identifier for the secret.
    pub(crate) id: [u8; 16],
    /// The commit hash for the secret.
    pub(crate) commit: [u8; 32],
}

impl FileItem for VaultRecord {
    fn offset(&self) -> &Range<u64> {
        &self.offset
    }

    fn value(&self) -> &Range<u64> {
        &self.value
    }

    fn set_offset(&mut self, offset: Range<u64>) {
        self.offset = offset;
    }

    fn set_value(&mut self, value: Range<u64>) {
        self.value = value;
    }
}

impl VaultRecord {
    /// Get the identifier for the secret.
    pub fn id(&self) -> [u8; 16] {
        self.id
    }

    /// Get the commit hash for the secret.
    pub fn commit(&self) -> [u8; 32] {
        self.commit
    }
}

/// Reference to a row in the event log.
#[derive(Default, Debug, Clone, Eq)]
pub struct EventLogFileRecord {
    /// Byte offset for the record.
    offset: Range<u64>,
    /// The byte range for the value.
    value: Range<u64>,
    /// The time the row was created.
    pub(crate) time: Timestamp,
    /// The commit hash for the previous row.
    pub(crate) last_commit: [u8; 32],
    /// The commit hash for the value.
    pub(crate) commit: [u8; 32],
}

impl EventLogFileRecord {
    /// Commit hash for this row.
    pub fn commit(&self) -> [u8; 32] {
        self.commit
    }

    /// Commit hash for the previous row.
    pub fn last_commit(&self) -> [u8; 32] {
        self.last_commit
    }

    /// Time the row was appended.
    pub fn time(&self) -> &Timestamp {
        &self.time
    }
}

impl PartialEq for EventLogFileRecord {
    fn eq(&self, other: &Self) -> bool {
        self.time == other.time
            && self.commit == other.commit
            && self.last_commit == other.last_commit
    }
}

impl FileItem for EventLogFileRecord {
    fn offset(&self) -> &Range<u64> {
        &self.offset
    }

    fn value(&self) -> &Range<u64> {
        &self.value
    }

    fn set_offset(&mut self, offset: Range<u64>) {
        self.offset = offset;
    }

    fn set_value(&mut self, value: Range<u64>) {
        self.value = value;
    }
}