1#![allow(non_upper_case_globals)]
16
17use crate::bindings::*;
18
19use std::fmt;
20
21#[derive(Debug, Clone, Copy, PartialEq, Eq)]
23pub enum ErrorCause {
24 Unknown,
25 CreateOptions,
27 SetOptionsHardwareAccelerators,
28 CreateCompiledModel,
29 GetCompiledModelInputBufferRequirements,
30 GetCompiledModelOutputBufferRequirements,
31 InputDoesntSupportAnyTensorBufferTypes,
32 RunCompiledModel,
33 NotSupportedLiteRtAnyType,
35 CreateEnvironment,
36 GetSignatureKey,
38 GetSignatureSubgraph,
39 GetNumSignatureInputs,
40 GetSignatureInputName,
41 GetNumSignatureOutputs,
42 GetSignatureOutputName,
43 GetSignature,
44 GetTensorTypeId,
45 GetUnrankedTensorType,
46 GetRankedTensorType,
47 InvalidTensorTypeId,
48 GetTensorName,
49 GetNumSubgraphInputs,
50 GetNumSubgraphOutputs,
51 GetSubgraphInput,
52 SubgraphInputTensorByNameNotFound,
53 GetSubgraphOutput,
54 SubgraphOutputTensorByNameNotFound,
55 CreateModelFromFile,
56 CreateModelFromBuffer,
57 GetNumModelSubgraphs,
58 GetNumModelSignatures,
59 GetModelSignature,
60 GetTensorBufferRequirementsBufferSize,
62 GetNumTensorBufferRequirementsSupportedBufferTypes,
63 GetTensorBufferRequirementsSupportedTensorBufferType,
64 InvalidElementTypeEnumValue,
65 InvalidTensorBufferTypeEnumValue,
66 CreateManagedTensorBuffer,
67 LockTensorBufferRead,
68 LockTensorBufferWrite,
69 GetTensorBufferPackedSize,
70 IncompatibleWriteType,
71 TensorBufferTooSmall,
72 IncompatibleReadType,
73 ReadBufferTooSmall,
74 InvalidStringEncoding,
76}
77
78#[derive(Clone, Copy, PartialEq, Eq)]
93pub struct Error {
94 cause: ErrorCause,
95 litert_status: LiteRtStatus,
96}
97
98impl fmt::Display for Error {
99 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
100 write!(f, "Error: {:?}", self.cause)?;
101 let status_description = self.litert_status_description();
102 write!(f, " LiteRtStatus: {:?} [{}] ", self.litert_status, status_description)
103 }
104}
105
106impl fmt::Debug for Error {
107 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
108 fmt::Display::fmt(self, f)
109 }
110}
111
112impl std::error::Error for Error {}
113
114impl Error {
115 pub(crate) fn new(cause: ErrorCause, status: LiteRtStatus) -> Self {
116 Error { cause, litert_status: status }
117 }
118
119 pub fn cause(&self) -> ErrorCause {
121 self.cause
122 }
123
124 pub fn litert_status(&self) -> LiteRtStatus {
126 self.litert_status
127 }
128
129 pub fn litert_status_description(&self) -> String {
131 match self.litert_status {
133 LiteRtStatus_kLiteRtStatusOk => "kLiteRtStatusOk",
134
135 LiteRtStatus_kLiteRtStatusErrorInvalidArgument => "kLiteRtStatusErrorInvalidArgument",
137 LiteRtStatus_kLiteRtStatusErrorMemoryAllocationFailure => {
138 "kLiteRtStatusErrorMemoryAllocationFailure"
139 }
140 LiteRtStatus_kLiteRtStatusErrorRuntimeFailure => "kLiteRtStatusErrorRuntimeFailure",
141 LiteRtStatus_kLiteRtStatusErrorMissingInputTensor => {
142 "kLiteRtStatusErrorMissingInputTensor"
143 }
144 LiteRtStatus_kLiteRtStatusErrorUnsupported => "kLiteRtStatusErrorUnsupported",
145 LiteRtStatus_kLiteRtStatusErrorNotFound => "kLiteRtStatusErrorNotFound",
146 LiteRtStatus_kLiteRtStatusErrorTimeoutExpired => "kLiteRtStatusErrorTimeoutExpired",
147 LiteRtStatus_kLiteRtStatusErrorWrongVersion => "kLiteRtStatusErrorWrongVersion",
148 LiteRtStatus_kLiteRtStatusErrorUnknown => "kLiteRtStatusErrorUnknown",
149 LiteRtStatus_kLiteRtStatusErrorAlreadyExists => "kLiteRtStatusErrorAlreadyExists",
150
151 LiteRtStatus_kLiteRtStatusCancelled => "kLiteRtStatusCancelled",
153
154 LiteRtStatus_kLiteRtStatusErrorFileIO => "kLiteRtStatusErrorFileIO",
156 LiteRtStatus_kLiteRtStatusErrorInvalidFlatbuffer => {
157 "kLiteRtStatusErrorInvalidFlatbuffer"
158 }
159 LiteRtStatus_kLiteRtStatusErrorDynamicLoading => "kLiteRtStatusErrorDynamicLoading",
160 LiteRtStatus_kLiteRtStatusErrorSerialization => "kLiteRtStatusErrorSerialization",
161 LiteRtStatus_kLiteRtStatusErrorCompilation => "kLiteRtStatusErrorCompilation",
162
163 LiteRtStatus_kLiteRtStatusErrorIndexOOB => "kLiteRtStatusErrorIndexOOB",
165 LiteRtStatus_kLiteRtStatusErrorInvalidIrType => "kLiteRtStatusErrorInvalidIrType",
166 LiteRtStatus_kLiteRtStatusErrorInvalidGraphInvariant => {
167 "kLiteRtStatusErrorInvalidGraphInvariant"
168 }
169 LiteRtStatus_kLiteRtStatusErrorGraphModification => {
170 "kLiteRtStatusErrorGraphModification"
171 }
172
173 LiteRtStatus_kLiteRtStatusErrorInvalidToolConfig => {
175 "kLiteRtStatusErrorInvalidToolConfig"
176 }
177
178 LiteRtStatus_kLiteRtStatusLegalizeNoMatch => "kLiteRtStatusLegalizeNoMatch",
180 LiteRtStatus_kLiteRtStatusErrorInvalidLegalization => {
181 "kLiteRtStatusErrorInvalidLegalization"
182 }
183
184 LiteRtStatus_kLiteRtStatusPatternNoMatch => "kLiteRtStatusPatternNoMatch",
186 LiteRtStatus_kLiteRtStatusInvalidTransformation => "kLiteRtStatusInvalidTransformation",
187
188 LiteRtStatus_kLiteRtStatusErrorUnsupportedRuntimeVersion => {
190 "kLiteRtStatusErrorUnsupportedRuntimeVersion"
191 }
192 LiteRtStatus_kLiteRtStatusErrorUnsupportedCompilerVersion => {
193 "kLiteRtStatusErrorUnsupportedCompilerVersion"
194 }
195 LiteRtStatus_kLiteRtStatusErrorIncompatibleByteCodeVersion => {
196 "kLiteRtStatusErrorIncompatibleByteCodeVersion"
197 }
198
199 LiteRtStatus_kLiteRtStatusErrorUnsupportedOpShapeInferer => {
201 "kLiteRtStatusErrorUnsupportedOpShapeInferer"
202 }
203 LiteRtStatus_kLiteRtStatusErrorShapeInferenceFailed => {
204 "kLiteRtStatusErrorShapeInferenceFailed"
205 }
206
207 _ => "???",
208 }
209 .to_string()
211 }
212}