utf8-forge 0.1.0

A focused, idempotent, bidirectional charset transcoder for source trees. Default output: UTF-8 without BOM.
Documentation
//! Error types shared across the transcoding pipeline.
//!
//! `ForgeError` is the per-file domain error. Each variant maps 1:1 to a
//! report category (see `EXECUTION.md §6`), which keeps report aggregation
//! deterministic. Fatal / setup errors bubble up as `anyhow::Error` instead.

use std::fmt;
use std::io;

/// Domain error for a single file operation.
#[derive(Debug)]
pub enum ForgeError {
    /// Detection could not classify the bytes — never guessed, always reported.
    UnknownEncoding,
    /// A legal encoding was detected but the byte stream is malformed
    /// (`had_errors == true` from `encoding_rs`). Never half-decoded.
    DecodeError(String),
    /// File (or remote payload) exceeds `--max-file-size`.
    TooLarge { size: u64, max: u64 },
    /// Local filesystem failure (permissions, locked file, ...).
    Io(io::Error),
    /// `--url` network / TLS / status failure.
    Fetch(String),
}

impl ForgeError {
    /// Stable report category string for this error (JSON `error_files` /
    /// summary buckets in `EXECUTION.md §12`).
    pub fn category(&self) -> &'static str {
        match self {
            ForgeError::UnknownEncoding => "unknown",
            ForgeError::DecodeError(_) => "decode_error",
            ForgeError::TooLarge { .. } => "too_large",
            ForgeError::Io(_) => "io_error",
            ForgeError::Fetch(_) => "fetch_error",
        }
    }
}

impl fmt::Display for ForgeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ForgeError::UnknownEncoding => write!(f, "unknown encoding (never guessed)"),
            ForgeError::DecodeError(detail) => write!(f, "strict decode failed: {detail}"),
            ForgeError::TooLarge { size, max } => {
                write!(f, "file too large: {size} bytes > --max-file-size {max}")
            }
            ForgeError::Io(e) => write!(f, "io error: {e}"),
            ForgeError::Fetch(e) => write!(f, "fetch error: {e}"),
        }
    }
}

impl std::error::Error for ForgeError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            ForgeError::Io(e) => Some(e),
            _ => None,
        }
    }
}

impl From<io::Error> for ForgeError {
    fn from(e: io::Error) -> Self {
        ForgeError::Io(e)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn categories_match_report_schema() {
        assert_eq!(ForgeError::UnknownEncoding.category(), "unknown");
        assert_eq!(ForgeError::DecodeError("x".into()).category(), "decode_error");
        assert_eq!(
            ForgeError::TooLarge { size: 10, max: 5 }.category(),
            "too_large"
        );
        assert_eq!(
            ForgeError::Io(io::Error::new(io::ErrorKind::Other, "x")).category(),
            "io_error"
        );
        assert_eq!(ForgeError::Fetch("x".into()).category(), "fetch_error");
    }
}