tfledge/
error.rs

1use crate::ffi::*;
2
3#[derive(Debug)]
4pub enum Error {
5    Cancelled,
6    Error,
7    DelegateError,
8    UnresolvedOps,
9    ApplicationError,
10    DelegateDataNotFound,
11    DelegateDataReadError,
12    DelegateDataWriteError,
13    FailedToCreateInterpreter,
14    FailedToLoadModel,
15}
16impl Error {
17    pub(crate) fn from(ffi: TfLiteStatus) -> Result<(), Self> {
18        match ffi {
19            TfLiteStatus::kTfLiteOk => Ok(()),
20            TfLiteStatus::kTfLiteCancelled => Err(Self::Cancelled),
21            TfLiteStatus::kTfLiteError => Err(Self::Error),
22            TfLiteStatus::kTfLiteDelegateError => Err(Self::DelegateError),
23            TfLiteStatus::kTfLiteUnresolvedOps => Err(Self::UnresolvedOps),
24            TfLiteStatus::kTfLiteApplicationError => Err(Self::ApplicationError),
25            TfLiteStatus::kTfLiteDelegateDataNotFound => Err(Self::DelegateDataNotFound),
26            TfLiteStatus::kTfLiteDelegateDataReadError => Err(Self::DelegateDataReadError),
27            TfLiteStatus::kTfLiteDelegateDataWriteError => Err(Self::DelegateDataWriteError),
28        }
29    }
30}
31
32pub(crate) extern "C" fn error_reporter(
33    _data: *mut core::ffi::c_void,
34    format: *const core::ffi::c_char,
35    args: *mut __va_list_tag,
36) {
37    unsafe {
38        let mut buf = [0i8; 512];
39
40        vsnprintf(buf.as_mut_ptr(), 512, format, args);
41
42        let s = core::str::from_utf8_unchecked(core::ffi::CStr::from_ptr(buf.as_ptr()).to_bytes());
43
44        error!(target: "tfledge::tflitec", "{s}");
45    }
46}