|
1 |
| -use thiserror::Error; |
| 1 | +use std::error::Error; |
| 2 | +use std::fmt; |
2 | 3 |
|
3 | 4 | /// Enumerate errors returned by the OpenVINO implementation. See
|
4 | 5 | /// [`OvStatusCode`](https://docs.openvino.ai/2023.3/api/c_cpp_api/group__ov__base__c__api.html#_CPPv411ov_status_e).
|
5 |
| -// TODO This could be auto-generated (https://github.com/intel/openvino-rs/issues/20). |
6 | 6 | #[allow(missing_docs)]
|
7 |
| -#[derive(Debug, Error, PartialEq, Eq)] |
| 7 | +#[derive(Debug, PartialEq, Eq)] |
8 | 8 | pub enum InferenceError {
|
9 |
| - #[error("general error")] |
10 | 9 | GeneralError,
|
11 |
| - #[error("not implemented")] |
12 | 10 | NotImplemented,
|
13 |
| - #[error("network not loaded")] |
14 | 11 | NetworkNotLoaded,
|
15 |
| - #[error("parameter mismatch")] |
16 | 12 | ParameterMismatch,
|
17 |
| - #[error("not found")] |
18 | 13 | NotFound,
|
19 |
| - #[error("out of bounds")] |
20 | 14 | OutOfBounds,
|
21 |
| - #[error("unexpected")] |
22 | 15 | Unexpected,
|
23 |
| - #[error("request busy")] |
24 | 16 | RequestBusy,
|
25 |
| - #[error("result not ready")] |
26 | 17 | ResultNotReady,
|
27 |
| - #[error("not allocated")] |
28 | 18 | NotAllocated,
|
29 |
| - #[error("infer not started")] |
30 | 19 | InferNotStarted,
|
31 |
| - #[error("network not read")] |
32 | 20 | NetworkNotRead,
|
33 |
| - #[error("infer cancelled")] |
34 | 21 | InferCancelled,
|
35 |
| - #[error("invalid c parameter")] |
36 | 22 | InvalidCParam,
|
37 |
| - #[error("unknown C error")] |
38 | 23 | UnknownCError,
|
39 |
| - #[error("not implemented C method")] |
40 | 24 | NotImplementCMethod,
|
41 |
| - #[error("unknown exception")] |
42 | 25 | UnknownException,
|
43 |
| - #[error("undefined error code: {0}")] |
44 | 26 | Undefined(i32),
|
45 | 27 | }
|
46 | 28 |
|
@@ -75,28 +57,85 @@ impl InferenceError {
|
75 | 57 | }
|
76 | 58 | }
|
77 | 59 |
|
78 |
| -/// Enumerate setup failures: in some cases, this library will call library-loading code that may |
79 |
| -/// fail in a different way (i.e., [`LoadingError`]) than the calls to the OpenVINO libraries (i.e., |
80 |
| -/// [`InferenceError`]). |
81 |
| -#[allow(missing_docs)] |
82 |
| -#[derive(Debug, Error)] |
83 |
| -pub enum SetupError { |
84 |
| - #[error("inference error")] |
85 |
| - Inference(#[from] InferenceError), |
86 |
| - #[error("library loading error")] |
87 |
| - Loading(#[from] LoadingError), |
| 60 | +impl Error for InferenceError {} |
| 61 | + |
| 62 | +impl fmt::Display for InferenceError { |
| 63 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 64 | + match self { |
| 65 | + Self::GeneralError => write!(f, "general error"), |
| 66 | + Self::NotImplemented => write!(f, "not implemented"), |
| 67 | + Self::NetworkNotLoaded => write!(f, "network not loaded"), |
| 68 | + Self::ParameterMismatch => write!(f, "parameter mismatch"), |
| 69 | + Self::NotFound => write!(f, "not found"), |
| 70 | + Self::OutOfBounds => write!(f, "out of bounds"), |
| 71 | + Self::Unexpected => write!(f, "unexpected"), |
| 72 | + Self::RequestBusy => write!(f, "request busy"), |
| 73 | + Self::ResultNotReady => write!(f, "result not ready"), |
| 74 | + Self::NotAllocated => write!(f, "not allocated"), |
| 75 | + Self::InferNotStarted => write!(f, "infer not started"), |
| 76 | + Self::NetworkNotRead => write!(f, "network not read"), |
| 77 | + Self::InferCancelled => write!(f, "infer cancelled"), |
| 78 | + Self::InvalidCParam => write!(f, "invalid C parameter"), |
| 79 | + Self::UnknownCError => write!(f, "unknown C error"), |
| 80 | + Self::NotImplementCMethod => write!(f, "not implemented C method"), |
| 81 | + Self::UnknownException => write!(f, "unknown exception"), |
| 82 | + Self::Undefined(code) => write!(f, "undefined error code: {code}"), |
| 83 | + } |
| 84 | + } |
88 | 85 | }
|
89 | 86 |
|
90 | 87 | /// Enumerate the ways that library loading can fail.
|
91 | 88 | #[allow(missing_docs)]
|
92 |
| -#[derive(Debug, Error)] |
| 89 | +#[derive(Debug)] |
93 | 90 | pub enum LoadingError {
|
94 |
| - #[error("system failed to load shared libraries (see https://github.com/intel/openvino-rs/blob/main/crates/openvino-finder): {0}")] |
95 | 91 | SystemFailure(String),
|
96 |
| - #[error("cannot find path to shared libraries (see https://github.com/intel/openvino-rs/blob/main/crates/openvino-finder)")] |
97 | 92 | CannotFindLibraryPath,
|
98 |
| - #[error("cannot find path to XML plugin configuration (see https://github.com/intel/openvino-rs/blob/main/crates/openvino-finder)")] |
99 | 93 | CannotFindPluginPath,
|
100 |
| - #[error("unable to convert path to a UTF-8 string (see https://doc.rust-lang.org/std/path/struct.Path.html#method.to_str)")] |
101 | 94 | CannotStringifyPath,
|
102 | 95 | }
|
| 96 | + |
| 97 | +impl Error for LoadingError {} |
| 98 | + |
| 99 | +impl fmt::Display for LoadingError { |
| 100 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 101 | + match self { |
| 102 | + Self::SystemFailure(msg) => write!(f, "system failed to load shared libraries (see https://github.com/intel/openvino-rs/blob/main/crates/openvino-finder): {msg}"), |
| 103 | + Self::CannotFindLibraryPath => write!(f, "cannot find path to shared libraries (see https://github.com/intel/openvino-rs/blob/main/crates/openvino-finder)"), |
| 104 | + Self::CannotFindPluginPath => write!(f, "cannot find path to XML plugin configuration (see https://github.com/intel/openvino-rs/blob/main/crates/openvino-finder)"), |
| 105 | + Self::CannotStringifyPath => write!(f, "unable to convert path to a UTF-8 string (see https://doc.rust-lang.org/std/path/struct.Path.html#method.to_str)"), |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +/// Enumerate setup failures: in some cases, this library will call library-loading code that may |
| 111 | +/// fail in a different way (i.e., [`LoadingError`]) than the calls to the OpenVINO libraries (i.e., |
| 112 | +/// [`InferenceError`]). |
| 113 | +#[allow(missing_docs)] |
| 114 | +#[derive(Debug)] |
| 115 | +pub enum SetupError { |
| 116 | + Inference(InferenceError), |
| 117 | + Loading(LoadingError), |
| 118 | +} |
| 119 | + |
| 120 | +impl Error for SetupError {} |
| 121 | + |
| 122 | +impl fmt::Display for SetupError { |
| 123 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 124 | + match self { |
| 125 | + Self::Inference(error) => write!(f, "inference error: {error}"), |
| 126 | + Self::Loading(error) => write!(f, "library loading error: {error}"), |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +impl From<InferenceError> for SetupError { |
| 132 | + fn from(error: InferenceError) -> Self { |
| 133 | + SetupError::Inference(error) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +impl From<LoadingError> for SetupError { |
| 138 | + fn from(error: LoadingError) -> Self { |
| 139 | + SetupError::Loading(error) |
| 140 | + } |
| 141 | +} |
0 commit comments