use std::fmt;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Digest128([u8; 16]);
impl Digest128 {
pub const LEN: usize = 16;
#[must_use]
pub fn of(bytes: &[u8]) -> Self {
Self::from_hash(&blake3::hash(bytes))
}
#[must_use]
pub fn from_hash(hash: &blake3::Hash) -> Self {
let mut bytes = [0; Self::LEN];
bytes.copy_from_slice(&hash.as_bytes()[..Self::LEN]);
Self(bytes)
}
#[must_use]
pub const fn from_bytes(bytes: [u8; Self::LEN]) -> Self {
Self(bytes)
}
#[must_use]
pub const fn as_bytes(&self) -> &[u8; Self::LEN] {
&self.0
}
}
impl fmt::Display for Digest128 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for byte in self.0 {
write!(f, "{byte:02x}")?;
}
Ok(())
}
}
impl fmt::Debug for Digest128 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Digest128({self})")
}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct FamilyId(Digest128);
impl FamilyId {
#[must_use]
pub const fn from_digest(digest: Digest128) -> Self {
Self(digest)
}
#[must_use]
pub const fn digest(&self) -> Digest128 {
self.0
}
}
impl fmt::Display for FamilyId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl fmt::Debug for FamilyId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "FamilyId({})", self.0)
}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ContentKey(Digest128);
impl ContentKey {
#[must_use]
pub fn of(bytes: &[u8]) -> Self {
Self(Digest128::of(bytes))
}
#[must_use]
pub const fn from_digest(digest: Digest128) -> Self {
Self(digest)
}
#[must_use]
pub fn stand_in(path: &[u8], evidence: &[u8]) -> Self {
let mut hasher = blake3::Hasher::new_derive_key("aft-store 2026-11 unread file stand-in");
hasher.update(&u64::try_from(path.len()).unwrap_or(u64::MAX).to_le_bytes());
hasher.update(path);
hasher.update(evidence);
Self(Digest128::from_hash(&hasher.finalize()))
}
#[must_use]
pub const fn digest(&self) -> Digest128 {
self.0
}
}
impl fmt::Display for ContentKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl fmt::Debug for ContentKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "ContentKey({})", self.0)
}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct SegmentId(u64);
impl SegmentId {
#[must_use]
pub const fn new(value: u64) -> Self {
Self(value)
}
#[must_use]
pub const fn get(self) -> u64 {
self.0
}
}
impl fmt::Display for SegmentId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct CheckoutId(Digest128);
impl CheckoutId {
#[must_use]
pub const fn from_digest(digest: Digest128) -> Self {
Self(digest)
}
#[must_use]
pub const fn digest(&self) -> Digest128 {
self.0
}
}
impl fmt::Display for CheckoutId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl fmt::Debug for CheckoutId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "CheckoutId({})", self.0)
}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct Generation(u64);
impl Generation {
pub const EMPTY: Self = Self(0);
#[must_use]
pub const fn new(value: u64) -> Self {
Self(value)
}
#[must_use]
pub const fn get(self) -> u64 {
self.0
}
#[must_use]
pub const fn next(self) -> Self {
Self(self.0 + 1)
}
}
impl fmt::Display for Generation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct WriterIncarnation(u64);
impl WriterIncarnation {
#[must_use]
pub const fn new(value: u64) -> Self {
Self(value)
}
#[must_use]
pub const fn get(self) -> u64 {
self.0
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn digest_is_the_first_half_of_blake3() {
let full = blake3::hash(b"fn main() {}\n");
let digest = Digest128::of(b"fn main() {}\n");
assert_eq!(digest.as_bytes()[..], full.as_bytes()[..16]);
assert_eq!(digest.to_string(), full.to_hex()[..32]);
}
#[test]
fn content_keys_follow_bytes() {
assert_eq!(ContentKey::of(b"a"), ContentKey::of(b"a"));
assert_ne!(ContentKey::of(b"a"), ContentKey::of(b"a\n"));
}
}