gamut-cli 0.3.1

Sandbox CLI for the gamut codecs: decode PNG/JPEG/PPM/WebP/JXL and encode AVIF/WebP/TIFF/PNG/JXL/JPEG, plus inspection of the shared color/dsp/bitstream primitives (e.g. `gamut convert input.png output.avif`).
//! `gamut inspect` — strict "deconstruct" of a TIFF or DNG (issues #197/#263).
//!
//! Walks the entire container, classifies every byte into typed segments, and flags anything
//! unrecognised (unknown tags, unknown field types, out-of-spec codes, unclassified bytes).
//! Prints a report to stdout and exits non-zero when the file is not fully accounted for —
//! usable as an archival CI gate.

use std::path::PathBuf;

use clap::{Args, ValueEnum};
use gamut::ifd::SegmentReport;

use crate::error::CliError;

/// The most list entries to print per category before truncating.
const MAX_LIST: usize = 20;

/// The `DNGVersion` tag (50706) — present in every DNG, absent in plain TIFF; the format sniff.
const DNG_VERSION_TAG: u16 = 50706;

/// Arguments for `gamut inspect`.
#[derive(Args)]
pub(crate) struct InspectArgs {
    /// Input TIFF or DNG file.
    input: PathBuf,
    /// Force the container format instead of auto-detecting it.
    #[arg(long, value_enum)]
    format: Option<Format>,
}

/// The container format to deconstruct as.
#[derive(Clone, Copy, ValueEnum)]
pub(crate) enum Format {
    /// TIFF 6.0 / BigTIFF (gamut-tiff).
    Tiff,
    /// DNG (Adobe Digital Negative; gamut-dng).
    Dng,
}

/// A format-agnostic view of a deconstruct report, for printing.
struct Summary {
    segments: SegmentReport,
    unknown_fields: Vec<String>,
    unknown_tags: Vec<String>,
    anomalies: Vec<String>,
    fully_classified: bool,
    fully_accounted: bool,
}

/// Runs the `inspect` command: deconstruct the file, print the report, and exit non-zero if it is
/// not fully accounted for.
pub(crate) fn run(args: &InspectArgs) -> Result<(), CliError> {
    let data = std::fs::read(&args.input).map_err(|source| CliError::Io {
        path: args.input.clone(),
        source,
    })?;
    let format = args.format.unwrap_or_else(|| sniff(&data));

    let summary = match format {
        Format::Dng => summarize_dng(gamut::dng::deconstruct(&data)?),
        Format::Tiff => summarize_tiff(gamut::tiff::deconstruct(&data)?),
    };

    print_summary(&args.input, format, &summary);

    if summary.fully_accounted {
        Ok(())
    } else {
        Err(CliError::NotFullyAccounted(format!(
            "{}: not fully accounted — {} unclassified byte(s), {} unknown tag(s), {} unknown field type(s), {} anomaly/ies",
            args.input.display(),
            summary.segments.unclassified_bytes(),
            summary.unknown_tags.len(),
            summary.unknown_fields.len(),
            summary.anomalies.len(),
        )))
    }
}

/// Detects DNG vs TIFF: a DNG is a TIFF whose IFD 0 carries the mandatory `DNGVersion` tag.
fn sniff(data: &[u8]) -> Format {
    if let Ok(file) = gamut::tiff::read(data)
        && file
            .ifds
            .first()
            .is_some_and(|ifd0| ifd0.get(DNG_VERSION_TAG).is_some())
    {
        return Format::Dng;
    }
    Format::Tiff
}

/// Collapses a TIFF report into the printable [`Summary`].
fn summarize_tiff(report: gamut::tiff::DeconstructReport) -> Summary {
    use gamut::tiff::Anomaly;
    let fully_classified = report.is_fully_classified();
    let fully_accounted = report.is_fully_accounted();
    let unknown_fields = report
        .unknown_fields
        .iter()
        .map(|u| {
            format!(
                "page {} tag {:#06x} type {} (count {})",
                u.page, u.tag, u.type_code, u.count
            )
        })
        .collect();
    let unknown_tags = report
        .unknown_tags
        .iter()
        .map(|u| {
            format!(
                "page {} tag {:#06x} (type {}, count {})",
                u.page, u.tag, u.field_type, u.count
            )
        })
        .collect();
    let anomalies = report
        .anomalies
        .iter()
        .map(|a| match a {
            Anomaly::UnknownCode {
                page,
                tag,
                code,
                detail,
                ..
            } => {
                format!("[error] page {page}: {detail} (tag {tag:#06x}, code {code})")
            }
            Anomaly::UnparsableTag {
                page, tag, detail, ..
            } => {
                format!("[error] page {page}: {detail} (tag {tag:#06x})")
            }
            Anomaly::Structure {
                page,
                detail,
                severity,
                ..
            } => {
                format!("[{}] page {page}: {detail}", severity_label_tiff(*severity))
            }
            // `Anomaly` is non-exhaustive; render future categories generically.
            other => format!("[error] {other:?}"),
        })
        .collect();
    Summary {
        segments: report.segments,
        unknown_fields,
        unknown_tags,
        anomalies,
        fully_classified,
        fully_accounted,
    }
}

/// Collapses a DNG report into the printable [`Summary`].
fn summarize_dng(report: gamut::dng::DeconstructReport) -> Summary {
    use gamut::dng::Anomaly;
    let fully_classified = report.is_fully_classified();
    let fully_accounted = report.is_fully_accounted();
    let unknown_fields = report
        .unknown_fields
        .iter()
        .map(|u| {
            format!(
                "page {} tag {:#06x} type {} (count {})",
                u.page, u.tag, u.type_code, u.count
            )
        })
        .collect();
    let unknown_tags = report
        .unknown_tags
        .iter()
        .map(|u| {
            format!(
                "page {} tag {:#06x} (type {}, count {})",
                u.page, u.tag, u.field_type, u.count
            )
        })
        .collect();
    let anomalies = report
        .anomalies
        .iter()
        .map(|a| match a {
            Anomaly::UnknownCode {
                page,
                tag,
                code,
                detail,
                ..
            } => {
                format!("[error] page {page}: {detail} (tag {tag:#06x}, code {code})")
            }
            Anomaly::UnparsableTag {
                page, tag, detail, ..
            } => {
                format!("[error] page {page}: {detail} (tag {tag:#06x})")
            }
            Anomaly::Structure {
                page,
                detail,
                severity,
                ..
            } => {
                format!("[{}] page {page}: {detail}", severity_label_dng(*severity))
            }
            // `Anomaly` is non-exhaustive; render future categories generically.
            other => format!("[error] {other:?}"),
        })
        .collect();
    Summary {
        segments: report.segments,
        unknown_fields,
        unknown_tags,
        anomalies,
        fully_classified,
        fully_accounted,
    }
}

/// Renders a TIFF anomaly severity.
fn severity_label_tiff(severity: gamut::tiff::Severity) -> &'static str {
    match severity {
        gamut::tiff::Severity::Warning => "warning",
        gamut::tiff::Severity::Error => "error",
        // `Severity` is non-exhaustive; treat future levels as errors (the conservative label).
        _ => "error",
    }
}

/// Renders a DNG anomaly severity.
fn severity_label_dng(severity: gamut::dng::Severity) -> &'static str {
    match severity {
        gamut::dng::Severity::Warning => "warning",
        gamut::dng::Severity::Error => "error",
        // `Severity` is non-exhaustive; treat future levels as errors (the conservative label).
        _ => "error",
    }
}

/// Prints the human-readable report to stdout.
fn print_summary(path: &std::path::Path, format: Format, s: &Summary) {
    let seg = &s.segments;
    let classified = seg.file_len - seg.unclassified_bytes();
    let pct = if seg.file_len == 0 {
        100.0
    } else {
        classified as f64 / seg.file_len as f64 * 100.0
    };
    println!("inspecting {} as {}", path.display(), format_name(format));
    println!(
        "  classified:    {classified} / {} bytes ({pct:.1}%) across {} segment(s)",
        seg.file_len,
        seg.segments.len()
    );
    println!("  unclassified:  {} bytes", seg.unclassified_bytes());

    print_ranges(
        "unclassified ranges",
        &seg.unclassified
            .iter()
            .map(|g| (g.start, g.len))
            .collect::<Vec<_>>(),
    );
    if !seg.conflicts.is_empty() {
        println!("  conflicts:     {}", seg.conflicts.len());
        for c in seg.conflicts.iter().take(MAX_LIST) {
            println!(
                "    - [{}, {}) overlaps [{}, {})",
                c.b.range.start,
                c.b.range.end(),
                c.a.range.start,
                c.a.range.end()
            );
        }
    }
    if !seg.shared.is_empty() {
        println!("  shared values: {} (legal TIFF sharing)", seg.shared.len());
    }
    print_ranges(
        "out-of-bounds claims",
        &seg.out_of_bounds
            .iter()
            .map(|o| (o.range.start, o.range.len))
            .collect::<Vec<_>>(),
    );
    // The dual-ledger parser cross-check: non-empty lists here are gamut bugs, not file defects.
    print_ranges(
        "unclaimed reads (parser defect)",
        &seg.unclaimed_reads
            .iter()
            .map(|r| (r.start, r.len))
            .collect::<Vec<_>>(),
    );
    print_ranges(
        "unread claims (parser defect)",
        &seg.unread_claims
            .iter()
            .map(|c| (c.range.start, c.range.len))
            .collect::<Vec<_>>(),
    );

    print_lines("unknown field types", &s.unknown_fields);
    print_lines("unknown tags", &s.unknown_tags);
    print_lines("anomalies", &s.anomalies);

    println!("  fully classified: {}", yes_no(s.fully_classified));
    println!("  fully accounted:  {}", yes_no(s.fully_accounted));
}

/// Prints a `(start, len)` range list under `label`, truncating past [`MAX_LIST`].
fn print_ranges(label: &str, ranges: &[(u64, u64)]) {
    if ranges.is_empty() {
        return;
    }
    println!("  {label}: {}", ranges.len());
    for (start, len) in ranges.iter().take(MAX_LIST) {
        println!("    - {len} bytes at offset {start}");
    }
    if ranges.len() > MAX_LIST {
        println!("    … and {} more", ranges.len() - MAX_LIST);
    }
}

/// Prints a pre-formatted line list under `label`, truncating past [`MAX_LIST`].
fn print_lines(label: &str, lines: &[String]) {
    if lines.is_empty() {
        return;
    }
    println!("  {label}: {}", lines.len());
    for line in lines.iter().take(MAX_LIST) {
        println!("    - {line}");
    }
    if lines.len() > MAX_LIST {
        println!("    … and {} more", lines.len() - MAX_LIST);
    }
}

/// The display name of a format.
fn format_name(format: Format) -> &'static str {
    match format {
        Format::Tiff => "TIFF",
        Format::Dng => "DNG",
    }
}

/// `yes`/`no` for a boolean verdict.
fn yes_no(value: bool) -> &'static str {
    if value { "yes" } else { "no" }
}