edgefirst_decoder/
error.rs1use core::fmt;
7
8pub type DecoderResult<T, E = DecoderError> = std::result::Result<T, E>;
10
11#[derive(Debug)]
12pub enum DecoderError {
13 Internal(String),
15 NotSupported(String),
17 InvalidShape(String),
19 Yaml(serde_yaml::Error),
21 Json(serde_json::Error),
23 NoConfig,
25 InvalidConfig(String),
27 NDArrayShape(ndarray::ShapeError),
29 DtypeMismatch {
31 expected: edgefirst_tensor::DType,
32 actual: edgefirst_tensor::DType,
33 role: &'static str,
34 level: usize,
35 },
36 QuantMissing {
41 dtype: edgefirst_tensor::DType,
42 role: &'static str,
43 level: usize,
44 },
45 KernelDispatchUnreachable(String),
49 ForcedKernelUnavailable {
52 tier: &'static str,
53 missing_feature: &'static str,
54 },
55}
56
57impl fmt::Display for DecoderError {
58 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70 write!(f, "{self:?}")
71 }
72}
73
74impl std::error::Error for DecoderError {}
75
76impl From<serde_yaml::Error> for DecoderError {
77 fn from(err: serde_yaml::Error) -> Self {
78 DecoderError::Yaml(err)
79 }
80}
81
82impl From<serde_json::Error> for DecoderError {
83 fn from(err: serde_json::Error) -> Self {
84 DecoderError::Json(err)
85 }
86}
87
88impl From<ndarray::ShapeError> for DecoderError {
89 fn from(err: ndarray::ShapeError) -> Self {
90 DecoderError::NDArrayShape(err)
91 }
92}
93
94#[cfg(test)]
95mod tests {
96 use super::*;
97
98 #[test]
99 fn test_decoder_error_display() {
100 let e = DecoderError::Internal("something broke".to_string());
101 let msg = e.to_string();
102 assert!(!msg.is_empty());
103 assert!(
104 msg.contains("Internal") && msg.contains("something broke"),
105 "unexpected Internal message: {msg}"
106 );
107
108 let e = DecoderError::NotSupported("yolov99".to_string());
109 let msg = e.to_string();
110 assert!(!msg.is_empty());
111 assert!(
112 msg.contains("NotSupported") && msg.contains("yolov99"),
113 "unexpected NotSupported message: {msg}"
114 );
115
116 let e = DecoderError::InvalidShape("expected 3D".to_string());
117 let msg = e.to_string();
118 assert!(!msg.is_empty());
119 assert!(
120 msg.contains("InvalidShape") && msg.contains("expected 3D"),
121 "unexpected InvalidShape message: {msg}"
122 );
123
124 let e = DecoderError::NoConfig;
125 let msg = e.to_string();
126 assert!(!msg.is_empty());
127 assert!(
128 msg.contains("NoConfig"),
129 "unexpected NoConfig message: {msg}"
130 );
131
132 let e = DecoderError::InvalidConfig("missing field".to_string());
133 let msg = e.to_string();
134 assert!(!msg.is_empty());
135 assert!(
136 msg.contains("InvalidConfig") && msg.contains("missing field"),
137 "unexpected InvalidConfig message: {msg}"
138 );
139 }
140}