ultralytics_inference/
error.rs1use std::fmt;
6
7pub type Result<T> = std::result::Result<T, InferenceError>;
9
10#[derive(Debug)]
12pub enum InferenceError {
13 ModelLoadError(String),
15 InferenceError(String),
17 ImageError(String),
19 ConfigError(String),
21 Io(std::io::Error),
23 VisualizerError(String),
25 VideoError(String),
27 FeatureNotEnabled(String),
29}
30
31impl fmt::Display for InferenceError {
32 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33 match self {
34 Self::ModelLoadError(msg) => write!(f, "Model load error: {msg}"),
35 Self::InferenceError(msg) => write!(f, "Inference error: {msg}"),
36 Self::ImageError(msg) => write!(f, "Image error: {msg}"),
37 Self::ConfigError(msg) => write!(f, "Config error: {msg}"),
38 Self::Io(err) => write!(f, "IO error: {err}"),
39 Self::VisualizerError(msg) => write!(f, "Visualizer error: {msg}"),
40 Self::VideoError(msg) => write!(f, "Video error: {msg}"),
41 Self::FeatureNotEnabled(msg) => write!(f, "Feature not enabled: {msg}"),
42 }
43 }
44}
45
46impl std::error::Error for InferenceError {
47 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
48 match self {
49 Self::Io(err) => Some(err),
50 _ => None,
51 }
52 }
53}
54
55impl From<std::io::Error> for InferenceError {
56 fn from(err: std::io::Error) -> Self {
57 Self::Io(err)
58 }
59}
60
61impl From<image::ImageError> for InferenceError {
62 fn from(err: image::ImageError) -> Self {
63 Self::ImageError(err.to_string())
64 }
65}
66
67#[cfg(test)]
68mod tests {
69 use super::*;
70 use std::io;
71
72 #[test]
73 fn test_error_display() {
74 let err = InferenceError::ModelLoadError("test".to_string());
75 assert_eq!(err.to_string(), "Model load error: test");
76
77 let err = InferenceError::InferenceError("test".to_string());
78 assert_eq!(err.to_string(), "Inference error: test");
79 }
80
81 #[test]
82 fn test_error_conversions() {
83 let io_err = io::Error::new(io::ErrorKind::NotFound, "file not found");
84 let err: InferenceError = io_err.into();
85 assert!(matches!(err, InferenceError::Io(_)));
86 assert_eq!(err.to_string(), "IO error: file not found");
87
88 let img_err = image::ImageError::Parameter(image::error::ParameterError::from_kind(
89 image::error::ParameterErrorKind::DimensionMismatch,
90 ));
91 let err: InferenceError = img_err.into();
92 assert!(matches!(err, InferenceError::ImageError(_)));
93 }
94
95 #[test]
96 fn test_error_source() {
97 let io_err = io::Error::new(io::ErrorKind::NotFound, "source test");
98 let err = InferenceError::Io(io_err);
99 assert!(std::error::Error::source(&err).is_some());
100
101 let err = InferenceError::ModelLoadError("test".to_string());
102 assert!(std::error::Error::source(&err).is_none());
103 }
104}