sedimentree_core 0.17.0

Sedimentree: a hash-linked CRDT storage structure with fragments, commits, and content-addressed blobs
Documentation
//! Causal identity for loose commits.

use crate::crypto::fingerprint::{Fingerprint, FingerprintSeed};
use core::hash::Hasher;

/// A user-supplied opaque identifier for a loose commit.
///
/// Unlike [`Digest`](crate::crypto::digest::Digest), which is a content hash
/// computed by the system, `CommitId` is provided by the caller at construction
/// time. The user decides how commits are identified — typically the hash of
/// the underlying document change (e.g., an Automerge `ChangeHash`).
///
/// Two commits with the same `CommitId` are the same item for
/// set reconciliation, regardless of blob metadata. Fragment maps
/// also use `CommitId` as their key (the fragment's head commit).
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CommitId([u8; 32]);

impl CommitId {
    /// Create a [`CommitId`] from raw bytes.
    #[must_use]
    pub const fn new(bytes: [u8; 32]) -> Self {
        Self(bytes)
    }

    /// The raw bytes of this identifier.
    #[must_use]
    pub const fn as_bytes(&self) -> &[u8; 32] {
        &self.0
    }
}

impl From<[u8; 32]> for CommitId {
    fn from(bytes: [u8; 32]) -> Self {
        Self::new(bytes)
    }
}

impl Fingerprint<CommitId> {
    /// Compute a `CommitId` fingerprint under `seed`.
    ///
    /// Hashes the `CommitId`'s 32 raw bytes directly via SipHash-2-4,
    /// bypassing [`core::hash::Hash`] (whose `<[u8; N] as Hash>::hash`
    /// impl prefixes the data with `write_usize(N)` — `usize` differs
    /// in width between 32-bit and 64-bit targets, so a browser and a
    /// native peer would otherwise compute different fingerprints for
    /// the same `CommitId`).
    ///
    /// ```
    /// use sedimentree_core::{
    ///     crypto::fingerprint::{Fingerprint, FingerprintSeed},
    ///     loose_commit::id::CommitId,
    /// };
    ///
    /// let seed = FingerprintSeed::new(42, 99);
    /// let id = CommitId::new([7u8; 32]);
    /// let fp = Fingerprint::<CommitId>::new(&seed, &id);
    /// assert_eq!(fp, Fingerprint::<CommitId>::new(&seed, &id));
    /// ```
    #[must_use]
    pub fn new(seed: &FingerprintSeed, id: &CommitId) -> Self {
        let mut hasher = seed.hasher();
        hasher.write(&id.0);
        Self::from_u64(hasher.finish())
    }
}

impl From<CommitId> for [u8; 32] {
    fn from(id: CommitId) -> Self {
        id.0
    }
}

impl core::fmt::Debug for CommitId {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "CommitId(")?;
        for byte in &self.0[..4] {
            write!(f, "{byte:02x}")?;
        }
        write!(f, "…)")
    }
}

impl core::fmt::Display for CommitId {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        for byte in &self.0 {
            write!(f, "{byte:02x}")?;
        }
        Ok(())
    }
}

#[cfg(feature = "arbitrary")]
impl<'a> arbitrary::Arbitrary<'a> for CommitId {
    fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
        let bytes: [u8; 32] = u.arbitrary()?;
        Ok(Self(bytes))
    }
}

#[cfg(feature = "bolero")]
impl bolero::generator::TypeGenerator for CommitId {
    fn generate<D: bolero::Driver>(driver: &mut D) -> Option<Self> {
        let bytes: [u8; 32] = bolero::generator::TypeGenerator::generate(driver)?;
        Some(Self(bytes))
    }
}