utf8_forge/error.rs
1// Copyright 2026 houseme
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Error types shared across the transcoding pipeline.
16//!
17//! `ForgeError` is the per-file domain error. Each variant maps 1:1 to a
18//! stable report category, which keeps report aggregation deterministic.
19//! Fatal / setup errors bubble up as `anyhow::Error` instead.
20
21use std::fmt;
22use std::io;
23
24/// Domain error for a single file operation.
25#[derive(Debug)]
26pub enum ForgeError {
27 /// Detection could not classify the bytes — never guessed, always reported.
28 UnknownEncoding,
29 /// A legal encoding was detected but the byte stream is malformed
30 /// (`had_errors == true` from `encoding_rs`). Never half-decoded.
31 DecodeError(String),
32 /// File (or remote payload) exceeds `--max-file-size`.
33 TooLarge { size: u64, max: u64 },
34 /// Local filesystem failure (permissions, locked file, ...).
35 Io(io::Error),
36 /// `--url` network / TLS / status failure.
37 Fetch(String),
38}
39
40impl ForgeError {
41 /// Stable report category string for this error (used by the JSON report
42 /// summary buckets and `error_files` lists).
43 pub fn category(&self) -> &'static str {
44 match self {
45 ForgeError::UnknownEncoding => "unknown",
46 ForgeError::DecodeError(_) => "decode_error",
47 ForgeError::TooLarge { .. } => "too_large",
48 ForgeError::Io(_) => "io_error",
49 ForgeError::Fetch(_) => "fetch_error",
50 }
51 }
52}
53
54impl fmt::Display for ForgeError {
55 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56 match self {
57 ForgeError::UnknownEncoding => write!(f, "unknown encoding (never guessed)"),
58 ForgeError::DecodeError(detail) => write!(f, "strict decode failed: {detail}"),
59 ForgeError::TooLarge { size, max } => {
60 write!(f, "file too large: {size} bytes > --max-file-size {max}")
61 }
62 ForgeError::Io(e) => write!(f, "io error: {e}"),
63 ForgeError::Fetch(e) => write!(f, "fetch error: {e}"),
64 }
65 }
66}
67
68impl std::error::Error for ForgeError {
69 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
70 match self {
71 ForgeError::Io(e) => Some(e),
72 _ => None,
73 }
74 }
75}
76
77impl From<io::Error> for ForgeError {
78 fn from(e: io::Error) -> Self {
79 ForgeError::Io(e)
80 }
81}
82
83#[cfg(test)]
84mod tests {
85 use super::*;
86
87 #[test]
88 fn categories_match_report_schema() {
89 assert_eq!(ForgeError::UnknownEncoding.category(), "unknown");
90 assert_eq!(
91 ForgeError::DecodeError("x".into()).category(),
92 "decode_error"
93 );
94 assert_eq!(
95 ForgeError::TooLarge { size: 10, max: 5 }.category(),
96 "too_large"
97 );
98 assert_eq!(ForgeError::Io(io::Error::other("x")).category(), "io_error");
99 assert_eq!(ForgeError::Fetch("x".into()).category(), "fetch_error");
100 }
101}