-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy patherror.rs
More file actions
104 lines (89 loc) · 3.31 KB
/
error.rs
File metadata and controls
104 lines (89 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
//! Error types for the inference library.
use std::fmt;
/// Result type alias for inference operations.
pub type Result<T> = std::result::Result<T, InferenceError>;
/// Main error type for the inference library.
#[derive(Debug)]
pub enum InferenceError {
/// Error loading the ONNX model.
ModelLoadError(String),
/// Error during model inference.
InferenceError(String),
/// Error processing images.
ImageError(String),
/// Invalid configuration provided.
ConfigError(String),
/// IO error (file not found, permission denied, etc.).
Io(std::io::Error),
/// Visualizer error.
VisualizerError(String),
/// Video/stream processing error.
VideoError(String),
/// Feature not enabled.
FeatureNotEnabled(String),
}
impl fmt::Display for InferenceError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ModelLoadError(msg) => write!(f, "Model load error: {msg}"),
Self::InferenceError(msg) => write!(f, "Inference error: {msg}"),
Self::ImageError(msg) => write!(f, "Image error: {msg}"),
Self::ConfigError(msg) => write!(f, "Config error: {msg}"),
Self::Io(err) => write!(f, "IO error: {err}"),
Self::VisualizerError(msg) => write!(f, "Visualizer error: {msg}"),
Self::VideoError(msg) => write!(f, "Video error: {msg}"),
Self::FeatureNotEnabled(msg) => write!(f, "Feature not enabled: {msg}"),
}
}
}
impl std::error::Error for InferenceError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Io(err) => Some(err),
_ => None,
}
}
}
impl From<std::io::Error> for InferenceError {
fn from(err: std::io::Error) -> Self {
Self::Io(err)
}
}
impl From<image::ImageError> for InferenceError {
fn from(err: image::ImageError) -> Self {
Self::ImageError(err.to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::io;
#[test]
fn test_error_display() {
let err = InferenceError::ModelLoadError("test".to_string());
assert_eq!(err.to_string(), "Model load error: test");
let err = InferenceError::InferenceError("test".to_string());
assert_eq!(err.to_string(), "Inference error: test");
}
#[test]
fn test_error_conversions() {
let io_err = io::Error::new(io::ErrorKind::NotFound, "file not found");
let err: InferenceError = io_err.into();
assert!(matches!(err, InferenceError::Io(_)));
assert_eq!(err.to_string(), "IO error: file not found");
let img_err = image::ImageError::Parameter(image::error::ParameterError::from_kind(
image::error::ParameterErrorKind::DimensionMismatch,
));
let err: InferenceError = img_err.into();
assert!(matches!(err, InferenceError::ImageError(_)));
}
#[test]
fn test_error_source() {
let io_err = io::Error::new(io::ErrorKind::NotFound, "source test");
let err = InferenceError::Io(io_err);
assert!(std::error::Error::source(&err).is_some());
let err = InferenceError::ModelLoadError("test".to_string());
assert!(std::error::Error::source(&err).is_none());
}
}