Skip to main content

onnx_extractor/
error.rs

1/// Custom error type for onnx-extractor
2#[derive(Debug)]
3pub enum Error {
4    /// I/O error when reading files
5    Io(std::io::Error),
6    /// Protobuf decoding error
7    Decode(prost::DecodeError),
8    /// UTF-8 conversion error
9    Utf8(std::str::Utf8Error),
10    /// Integer parse error
11    ParseInt(std::num::ParseIntError),
12    /// Lock poisoned error when accessing external data cache
13    ExternalDataLockPoisoned,
14    /// Invalid graph structure
15    InvalidGraph,
16    /// Missing required field
17    MissingField(&'static str),
18    /// Model contains external tensor data, but was loaded without a filesystem path
19    ExternalDataRequiresPath,
20    /// Unsupported attribute type
21    UnsupportedAttributeType(i32),
22    /// External data slice range exceeds file size
23    ExternalDataOutOfBounds {
24        start: usize,
25        end: usize,
26        file_size: usize,
27    },
28}
29
30impl std::fmt::Display for Error {
31    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32        match self {
33            Error::Io(e) => write!(f, "I/O error: {e}"),
34            Error::Decode(e) => write!(f, "Protobuf decode error: {e}"),
35            Error::Utf8(e) => write!(f, "UTF-8 conversion error: {e}"),
36            Error::ParseInt(e) => write!(f, "Parse int error: {e}"),
37            Error::ExternalDataLockPoisoned => write!(f, "Lock poisoned"),
38            Error::InvalidGraph => write!(
39                f,
40                "Invalid graph: graph has cycles or unresolved dependencies"
41            ),
42            Error::MissingField(field) => write!(f, "Missing required field: {field}"),
43            Error::ExternalDataRequiresPath => write!(
44                f,
45                "Model contains external tensor data, but was loaded without a filesystem path"
46            ),
47            Error::UnsupportedAttributeType(t) => write!(f, "Unsupported attribute type: {t}"),
48            Error::ExternalDataOutOfBounds {
49                start,
50                end,
51                file_size,
52            } => write!(
53                f,
54                "External data range {start}..{end} exceeds file size {file_size}"
55            ),
56        }
57    }
58}
59
60impl std::error::Error for Error {
61    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
62        match self {
63            Error::Io(e) => Some(e),
64            Error::Decode(e) => Some(e),
65            Error::Utf8(e) => Some(e),
66            Error::ParseInt(e) => Some(e),
67            _ => None,
68        }
69    }
70}
71
72impl From<std::io::Error> for Error {
73    fn from(err: std::io::Error) -> Self {
74        Error::Io(err)
75    }
76}
77
78impl From<prost::DecodeError> for Error {
79    fn from(err: prost::DecodeError) -> Self {
80        Error::Decode(err)
81    }
82}
83
84impl From<std::str::Utf8Error> for Error {
85    fn from(err: std::str::Utf8Error) -> Self {
86        Error::Utf8(err)
87    }
88}
89
90impl<T> From<std::sync::PoisonError<T>> for Error {
91    fn from(_: std::sync::PoisonError<T>) -> Self {
92        Error::ExternalDataLockPoisoned
93    }
94}
95
96impl From<std::num::ParseIntError> for Error {
97    fn from(err: std::num::ParseIntError) -> Self {
98        Error::ParseInt(err)
99    }
100}