quantize_rs/errors.rs
1//! Typed error handling for the quantize-rs library.
2//!
3//! All public API functions return [`Result<T>`](type@Result), which uses
4//! [`QuantizeError`] as the error type. The CLI binary converts these into
5//! `anyhow::Error` automatically via the blanket `From<E: std::error::Error>`
6//! impl, so callers that prefer `anyhow` can use `?` without `.map_err()`.
7
8use std::fmt;
9use std::path::PathBuf;
10
11/// Result type alias used throughout the quantize-rs public API.
12pub type Result<T> = std::result::Result<T, QuantizeError>;
13
14/// Errors produced by the quantize-rs library.
15///
16/// Each variant covers a distinct failure category. The `reason` field
17/// carries a human-readable explanation suitable for display.
18///
19/// Marked `#[non_exhaustive]` so future variants can be added without a
20/// breaking change. Match arms should always include a `_ => ...` catch-all.
21#[derive(Debug)]
22#[non_exhaustive]
23pub enum QuantizeError {
24 /// Empty tensor, shape mismatch, per-channel on a scalar, etc.
25 InvalidTensor {
26 /// What went wrong.
27 reason: String,
28 },
29
30 /// Unsupported quantization configuration (e.g. bits != 4 or 8).
31 UnsupportedConfig {
32 /// What went wrong.
33 reason: String,
34 },
35
36 /// Failed to load an ONNX model from disk.
37 ModelLoad {
38 /// Path that was being loaded.
39 path: PathBuf,
40 /// What went wrong.
41 reason: String,
42 },
43
44 /// Failed to save a quantized ONNX model to disk.
45 ModelSave {
46 /// Path that was being written.
47 path: PathBuf,
48 /// What went wrong.
49 reason: String,
50 },
51
52 /// Error during QDQ graph transformation (weight not found, size mismatch, etc.).
53 GraphTransform {
54 /// What went wrong.
55 reason: String,
56 },
57
58 /// Error during calibration (invalid dataset, inference failure, etc.).
59 Calibration {
60 /// What went wrong.
61 reason: String,
62 },
63
64 /// Configuration file parsing or validation error.
65 Config {
66 /// What went wrong.
67 reason: String,
68 },
69}
70
71impl fmt::Display for QuantizeError {
72 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73 match self {
74 QuantizeError::InvalidTensor { reason } => {
75 write!(f, "invalid tensor: {reason}")
76 }
77 QuantizeError::UnsupportedConfig { reason } => {
78 write!(f, "unsupported config: {reason}")
79 }
80 QuantizeError::ModelLoad { path, reason } => {
81 // An empty PathBuf is the sentinel used by `from_bytes` —
82 // there's no on-disk source to name. Print a friendlier
83 // label so the error doesn't read as "failed to load model
84 // '': ...".
85 if path.as_os_str().is_empty() {
86 write!(f, "failed to load model (from bytes): {reason}")
87 } else {
88 write!(f, "failed to load model '{}': {reason}", path.display())
89 }
90 }
91 QuantizeError::ModelSave { path, reason } => {
92 if path.as_os_str().is_empty() {
93 write!(f, "failed to save model: {reason}")
94 } else {
95 write!(f, "failed to save model '{}': {reason}", path.display())
96 }
97 }
98 QuantizeError::GraphTransform { reason } => {
99 write!(f, "graph transform error: {reason}")
100 }
101 QuantizeError::Calibration { reason } => {
102 write!(f, "calibration error: {reason}")
103 }
104 QuantizeError::Config { reason } => {
105 write!(f, "config error: {reason}")
106 }
107 }
108 }
109}
110
111impl std::error::Error for QuantizeError {}