loom-diff 0.1.0

Line-level diff for loom. Pure-function unified-diff over byte slices and text strings, used by both the loom CLI and the loom-gateway. Layered above weave-sdk; lower levels untouched.
Documentation
//! # loom-diff
//!
//! Line-level diff used by both the loom CLI and the loom-gateway.
//! Pure functions: feed in two text blobs (or byte slices), get back
//! either a structured [`FileDiff`] (good for JSON / UI consumers) or
//! a `String` of git-style unified diff (good for terminal output).
//!
//! Backed by the `similar` crate's Myers algorithm. Binary blobs are
//! detected via a simple null-byte heuristic and surface as a `Binary`
//! diff variant — we don't try to render diffs of binary content.
//!
//! ## Layering
//!
//! ```text
//! loom-web · loom-gateway   ← consumers
//! loom-diff                 ← THIS CRATE
//! ─── frozen below ─────────────────────────
//! weave-sdk · Strand · Locus · Lens · DHT · Forum
//! ```

#![warn(missing_docs)]

mod render;
mod structured;

pub use render::{unified_diff_string, UnifiedDiffOptions};
pub use structured::{
    diff_blobs, file_diff, BinaryReason, DiffHunk, DiffLine, DiffLineKind, FileDiff, FileDiffStatus,
};

use thiserror::Error;

/// Errors produced by loom-diff.
#[derive(Debug, Error)]
pub enum DiffError {
    /// Caller asked for a diff with non-utf8 bytes; the structured API
    /// surfaces this as a `Binary` diff instead. Reserved for cases where
    /// callers explicitly demanded text decoding.
    #[error("not valid utf-8")]
    NotUtf8,
}

/// Result alias.
pub type Result<T> = std::result::Result<T, DiffError>;

/// Heuristic: a byte slice is "probably binary" if the first 8KB
/// contains a null byte. This is the same check git uses.
pub fn looks_binary(bytes: &[u8]) -> bool {
    let sample_len = bytes.len().min(8192);
    bytes[..sample_len].contains(&0)
}