cargo-ratchet 0.1.0

Prevent resolved versions in Cargo.lock from regressing below a committed, self-advancing baseline
Documentation
use std::fs;
use std::path::Path;

use anyhow::{Context, Result};

use crate::RatchetPaths;

/// Read-only check for CI: returns whether the baseline is byte-identical to
/// the lockfile (a missing baseline is a mismatch). Byte-identical files
/// trivially contain no regressions, so nothing is parsed; on a throwaway
/// checkout an advance would be discarded, so any mismatch means someone
/// needs to run `cargo ratchet` locally and commit the result.
pub fn check_fail_on_change(paths: &RatchetPaths) -> Result<bool> {
    if !paths.ratchet_file.exists() {
        return Ok(false);
    }
    Ok(read_bytes(&paths.ratchet_file)? == read_bytes(&paths.lockfile)?)
}

fn read_bytes(path: &Path) -> Result<Vec<u8>> {
    fs::read(path).with_context(|| format!("failed to read {}", path.display()))
}