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 /// The decoded text cannot be encoded into the requested target without
33 /// producing bytes the tool could not re-read on the next run.
34 EncodeError(String),
35 /// File (or remote payload) exceeds `--max-file-size`.
36 TooLarge { size: u64, max: u64 },
37 /// Local filesystem failure (permissions, locked file, ...).
38 Io(io::Error),
39 /// `--url` network / TLS / status failure.
40 Fetch(String),
41}
42
43impl ForgeError {
44 /// Stable report category string for this error (used by the JSON report
45 /// summary buckets and `error_files` lists).
46 pub fn category(&self) -> &'static str {
47 match self {
48 ForgeError::UnknownEncoding => "unknown",
49 ForgeError::DecodeError(_) => "decode_error",
50 ForgeError::EncodeError(_) => "encode_error",
51 ForgeError::TooLarge { .. } => "too_large",
52 ForgeError::Io(_) => "io_error",
53 ForgeError::Fetch(_) => "fetch_error",
54 }
55 }
56}
57
58impl fmt::Display for ForgeError {
59 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60 match self {
61 ForgeError::UnknownEncoding => write!(f, "unknown encoding (never guessed)"),
62 ForgeError::DecodeError(detail) => write!(f, "strict decode failed: {detail}"),
63 ForgeError::EncodeError(detail) => write!(f, "strict encode failed: {detail}"),
64 ForgeError::TooLarge { size, max } => {
65 write!(f, "file too large: {size} bytes > --max-file-size {max}")
66 }
67 ForgeError::Io(e) => write!(f, "io error: {e}"),
68 ForgeError::Fetch(e) => write!(f, "fetch error: {e}"),
69 }
70 }
71}
72
73impl std::error::Error for ForgeError {
74 // The io cause is already spelled out by `Display`; re-exposing it as
75 // `source()` would make anyhow-style `{err:#}` chains print it twice.
76 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
77 None
78 }
79}
80
81impl From<io::Error> for ForgeError {
82 fn from(e: io::Error) -> Self {
83 ForgeError::Io(e)
84 }
85}
86
87#[cfg(test)]
88mod tests {
89 use super::*;
90
91 #[test]
92 fn categories_match_report_schema() {
93 assert_eq!(ForgeError::UnknownEncoding.category(), "unknown");
94 assert_eq!(
95 ForgeError::DecodeError("x".into()).category(),
96 "decode_error"
97 );
98 assert_eq!(
99 ForgeError::EncodeError("x".into()).category(),
100 "encode_error"
101 );
102 assert_eq!(
103 ForgeError::TooLarge { size: 10, max: 5 }.category(),
104 "too_large"
105 );
106 assert_eq!(ForgeError::Io(io::Error::other("x")).category(), "io_error");
107 assert_eq!(ForgeError::Fetch("x".into()).category(), "fetch_error");
108 }
109}