1use crate::sys::OBError;
3use crate::sys::enums::OBExceptionType;
4
5use std::{error::Error, fmt};
6
7#[derive(Debug)]
9pub struct OrbbecErrorData {
10 pub message: String,
12 pub function: String,
14 pub args: String,
16}
17
18#[derive(Debug)]
20pub enum OrbbecError {
21 Unknown(OrbbecErrorData),
23 StdException(OrbbecErrorData),
25 CameraDisconnected(OrbbecErrorData),
27 PlatformException(OrbbecErrorData),
29 InvalidValue(OrbbecErrorData),
31 WrongAPICallSequence(OrbbecErrorData),
33 NotImplemented(OrbbecErrorData),
35 IOException(OrbbecErrorData),
37 MemoryException(OrbbecErrorData),
39 UnsupportedOperation(OrbbecErrorData),
41}
42
43impl fmt::Display for OrbbecError {
44 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
45 match self {
46 OrbbecError::Unknown(data) => write!(f, "Unknown Error: {}", data.message),
47 OrbbecError::StdException(data) => write!(f, "Standard Exception: {}", data.message),
48 OrbbecError::CameraDisconnected(data) => {
49 write!(f, "Camera Disconnected: {}", data.message)
50 }
51 OrbbecError::PlatformException(data) => {
52 write!(f, "Platform Exception: {}", data.message)
53 }
54 OrbbecError::InvalidValue(data) => write!(f, "Invalid Value: {}", data.message),
55 OrbbecError::WrongAPICallSequence(data) => {
56 write!(f, "Wrong API Call Sequence: {}", data.message)
57 }
58 OrbbecError::NotImplemented(data) => write!(f, "Not Implemented: {}", data.message),
59 OrbbecError::IOException(data) => write!(f, "I/O Exception: {}", data.message),
60 OrbbecError::MemoryException(data) => write!(f, "Memory Exception: {}", data.message),
61 OrbbecError::UnsupportedOperation(data) => {
62 write!(f, "Unsupported Operation: {}", data.message)
63 }
64 }
65 }
66}
67
68impl Error for OrbbecError {}
69
70impl From<&OBError> for OrbbecError {
71 fn from(err: &OBError) -> Self {
72 let message = err.message();
73 let function = err.function();
74 let args = err.args();
75 let exception_type = err.exception_type();
76
77 let data = OrbbecErrorData {
78 message,
79 function,
80 args,
81 };
82
83 match exception_type {
84 OBExceptionType::Unknown => OrbbecError::Unknown(data),
85 OBExceptionType::StdException => OrbbecError::StdException(data),
86 OBExceptionType::CameraDisconnected => OrbbecError::CameraDisconnected(data),
87 OBExceptionType::PlatformException => OrbbecError::PlatformException(data),
88 OBExceptionType::InvalidValue => OrbbecError::InvalidValue(data),
89 OBExceptionType::WrongAPICallSequence => OrbbecError::WrongAPICallSequence(data),
90 OBExceptionType::NotImplemented => OrbbecError::NotImplemented(data),
91 OBExceptionType::IOException => OrbbecError::IOException(data),
92 OBExceptionType::MemoryException => OrbbecError::MemoryException(data),
93 OBExceptionType::UnsupportedOperation => OrbbecError::UnsupportedOperation(data),
94 }
95 }
96}
97
98impl From<OBError> for OrbbecError {
99 fn from(err: OBError) -> Self {
100 OrbbecError::from(&err)
101 }
102}