1use std::fmt;
4
5#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
11pub struct Digest128([u8; 16]);
12
13impl Digest128 {
14 pub const LEN: usize = 16;
16
17 #[must_use]
19 pub fn of(bytes: &[u8]) -> Self {
20 Self::from_hash(&blake3::hash(bytes))
21 }
22
23 #[must_use]
25 pub fn from_hash(hash: &blake3::Hash) -> Self {
26 let mut bytes = [0; Self::LEN];
27 bytes.copy_from_slice(&hash.as_bytes()[..Self::LEN]);
28 Self(bytes)
29 }
30
31 #[must_use]
33 pub const fn from_bytes(bytes: [u8; Self::LEN]) -> Self {
34 Self(bytes)
35 }
36
37 #[must_use]
38 pub const fn as_bytes(&self) -> &[u8; Self::LEN] {
39 &self.0
40 }
41}
42
43impl fmt::Display for Digest128 {
44 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45 for byte in self.0 {
46 write!(f, "{byte:02x}")?;
47 }
48 Ok(())
49 }
50}
51
52impl fmt::Debug for Digest128 {
53 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54 write!(f, "Digest128({self})")
55 }
56}
57
58#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
64pub struct FamilyId(Digest128);
65
66impl FamilyId {
67 #[must_use]
68 pub const fn from_digest(digest: Digest128) -> Self {
69 Self(digest)
70 }
71
72 #[must_use]
73 pub const fn digest(&self) -> Digest128 {
74 self.0
75 }
76}
77
78impl fmt::Display for FamilyId {
79 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
80 self.0.fmt(f)
81 }
82}
83
84impl fmt::Debug for FamilyId {
85 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
86 write!(f, "FamilyId({})", self.0)
87 }
88}
89
90#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
93pub struct ContentKey(Digest128);
94
95impl ContentKey {
96 #[must_use]
98 pub fn of(bytes: &[u8]) -> Self {
99 Self(Digest128::of(bytes))
100 }
101
102 #[must_use]
104 pub const fn from_digest(digest: Digest128) -> Self {
105 Self(digest)
106 }
107
108 #[must_use]
113 pub fn stand_in(path: &[u8], evidence: &[u8]) -> Self {
114 let mut hasher = blake3::Hasher::new_derive_key("aft-store 2026-11 unread file stand-in");
115 hasher.update(&u64::try_from(path.len()).unwrap_or(u64::MAX).to_le_bytes());
116 hasher.update(path);
117 hasher.update(evidence);
118 Self(Digest128::from_hash(&hasher.finalize()))
119 }
120
121 #[must_use]
122 pub const fn digest(&self) -> Digest128 {
123 self.0
124 }
125}
126
127impl fmt::Display for ContentKey {
128 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
129 self.0.fmt(f)
130 }
131}
132
133impl fmt::Debug for ContentKey {
134 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
135 write!(f, "ContentKey({})", self.0)
136 }
137}
138
139#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
142pub struct SegmentId(u64);
143
144impl SegmentId {
145 #[must_use]
146 pub const fn new(value: u64) -> Self {
147 Self(value)
148 }
149
150 #[must_use]
151 pub const fn get(self) -> u64 {
152 self.0
153 }
154}
155
156impl fmt::Display for SegmentId {
157 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
158 write!(f, "{}", self.0)
159 }
160}
161
162#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
165pub struct CheckoutId(Digest128);
166
167impl CheckoutId {
168 #[must_use]
169 pub const fn from_digest(digest: Digest128) -> Self {
170 Self(digest)
171 }
172
173 #[must_use]
174 pub const fn digest(&self) -> Digest128 {
175 self.0
176 }
177}
178
179impl fmt::Display for CheckoutId {
180 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
181 self.0.fmt(f)
182 }
183}
184
185impl fmt::Debug for CheckoutId {
186 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
187 write!(f, "CheckoutId({})", self.0)
188 }
189}
190
191#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
194pub struct Generation(u64);
195
196impl Generation {
197 pub const EMPTY: Self = Self(0);
200
201 #[must_use]
202 pub const fn new(value: u64) -> Self {
203 Self(value)
204 }
205
206 #[must_use]
207 pub const fn get(self) -> u64 {
208 self.0
209 }
210
211 #[must_use]
213 pub const fn next(self) -> Self {
214 Self(self.0 + 1)
215 }
216}
217
218impl fmt::Display for Generation {
219 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
220 write!(f, "{}", self.0)
221 }
222}
223
224#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
227pub struct WriterIncarnation(u64);
228
229impl WriterIncarnation {
230 #[must_use]
231 pub const fn new(value: u64) -> Self {
232 Self(value)
233 }
234
235 #[must_use]
236 pub const fn get(self) -> u64 {
237 self.0
238 }
239}
240
241#[cfg(test)]
242mod tests {
243 use super::*;
244
245 #[test]
246 fn digest_is_the_first_half_of_blake3() {
247 let full = blake3::hash(b"fn main() {}\n");
248 let digest = Digest128::of(b"fn main() {}\n");
249 assert_eq!(digest.as_bytes()[..], full.as_bytes()[..16]);
250 assert_eq!(digest.to_string(), full.to_hex()[..32]);
251 }
252
253 #[test]
254 fn content_keys_follow_bytes() {
255 assert_eq!(ContentKey::of(b"a"), ContentKey::of(b"a"));
256 assert_ne!(ContentKey::of(b"a"), ContentKey::of(b"a\n"));
257 }
258}