Skip to main content

tflite_c/
error.rs

1//! Error type returned by fallible operations in this crate.
2
3use std::ffi::NulError;
4use std::path::PathBuf;
5
6use thiserror::Error;
7
8use crate::ffi::{TfLiteStatus, TfLiteType};
9
10/// Errors that can arise while loading the TensorFlow Lite shared library,
11/// building an interpreter, or operating on tensors.
12#[derive(Debug, Error)]
13pub enum Error {
14    /// None of the candidate shared-library paths could be opened.
15    ///
16    /// The `paths` list records every probe attempt in order. The `source`
17    /// contains the last underlying `libloading` failure, which is usually
18    /// the most informative.
19    #[error("could not load TensorFlow Lite shared library (tried {paths:?})")]
20    LoadFailed {
21        /// Underlying loader error from the last attempted path.
22        #[source]
23        source: libloading::Error,
24        /// All candidate paths that were probed, in the order they were tried.
25        paths: Vec<String>,
26    },
27
28    /// The shared library opened but a required exported symbol was missing.
29    #[error("symbol `{name}` not found in TensorFlow Lite shared library")]
30    SymbolNotFound {
31        /// Name of the missing symbol (matches the upstream C identifier).
32        name: &'static str,
33        /// Underlying loader error.
34        #[source]
35        source: libloading::Error,
36    },
37
38    /// `TfLiteModelCreateFromFile` returned NULL for the given path.
39    #[error("failed to load model from {path:?}")]
40    ModelLoadFailed {
41        /// Filesystem path passed to the loader.
42        path: PathBuf,
43    },
44
45    /// A filesystem path could not be expressed as a C string (non-UTF-8 or
46    /// embedded NUL byte).
47    #[error("path could not be passed to the C API: {0}")]
48    InvalidPath(#[source] NulError),
49
50    /// A delegate option key/value pair contained an embedded NUL byte.
51    #[error("delegate option key or value contained an embedded NUL byte: {0}")]
52    InvalidOption(#[source] NulError),
53
54    /// `TfLiteInterpreterCreate` returned NULL.
55    #[error("failed to create TensorFlow Lite interpreter")]
56    InterpreterCreateFailed,
57
58    /// `TfLiteInterpreterAllocateTensors` returned a non-Ok status.
59    #[error("interpreter failed to allocate tensors")]
60    AllocateTensorsFailed,
61
62    /// `TfLiteInterpreterInvoke` returned a non-Ok status.
63    #[error("interpreter invocation failed with status {status:?}")]
64    InvokeFailed {
65        /// Raw status code returned by the C API.
66        status: TfLiteStatus,
67    },
68
69    /// Tensor index was out of bounds for the interpreter.
70    #[error("tensor index {index} out of bounds (count is {count})")]
71    TensorIndexOutOfBounds {
72        /// The requested tensor index.
73        index: usize,
74        /// The number of tensors available.
75        count: usize,
76    },
77
78    /// The tensor's actual element type did not match the typed accessor.
79    #[error("tensor dtype mismatch: expected {expected:?}, got {actual:?}")]
80    DtypeMismatch {
81        /// The element type the caller requested.
82        expected: TfLiteType,
83        /// The element type the tensor actually has.
84        actual: TfLiteType,
85    },
86
87    /// A tensor handle returned by the C API was unexpectedly NULL.
88    #[error("tensor handle was NULL")]
89    NullTensor,
90
91    /// A tensor's data pointer was unexpectedly NULL.
92    #[error("tensor data pointer was NULL")]
93    NullData,
94
95    /// `TfLiteExternalDelegateCreate` returned NULL.
96    #[error("failed to create external delegate")]
97    DelegateCreateFailed,
98
99    /// The C API rejected an external-delegate option (non-Ok status).
100    #[error("setting external delegate option failed with status {status:?}")]
101    DelegateOptionRejected {
102        /// Raw status returned by the C API.
103        status: TfLiteStatus,
104    },
105
106    /// The loaded TFLite library does not export the `TfLiteExternalDelegate*`
107    /// symbol group, so this crate cannot construct an [`crate::ExternalDelegate`].
108    ///
109    /// These symbols live in TFLite's experimental C header and are routinely
110    /// omitted from prebuilt shared objects. The core load → invoke path
111    /// remains fully functional — only external-delegate construction is
112    /// blocked.
113    #[error("loaded TFLite library does not export the TfLiteExternalDelegate* symbol group")]
114    ExternalDelegateApiUnavailable,
115}
116
117/// Convenience alias for `Result<T, tflite_c::Error>`.
118pub type Result<T> = std::result::Result<T, Error>;