openvino/
error.rs

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