realsense_rust/kind/exception.rs
1//! Possible exception / error types that librealsense2 can produce
2
3use num_derive::{FromPrimitive, ToPrimitive};
4use realsense_sys as sys;
5use std::fmt::{Display, Formatter, Result};
6
7/// Enumeration of possible exception types that can be returned via `rs2_error`
8///
9/// `Rs2Exception` is an enumeration where each variant describes the class of error returned by an
10/// [`rs2_error`](realsense_sys::rs2_error) pointer.
11#[repr(i32)]
12#[derive(FromPrimitive, ToPrimitive, Debug, Clone, Copy, PartialEq, Eq, Hash)]
13pub enum Rs2Exception {
14 /// Unknown error classification.
15 ///
16 /// This is context-dependent. Usually this means that the error isn't a specific class of
17 /// librealsense2 errors (could just be `std::runtime_error` under the hood). You'll need to
18 /// know what API was called at a low-level to be able to trace what this error actually means.
19 Unknown = sys::rs2_exception_type_RS2_EXCEPTION_TYPE_UNKNOWN as i32,
20 /// Error resulted because the device was disconnected.
21 ///
22 /// This can be caused by outside intervention (pulling the plug), by an internal firmware
23 /// error or due to insufficient power to the camera / device.
24 CameraDisconnected = sys::rs2_exception_type_RS2_EXCEPTION_TYPE_CAMERA_DISCONNECTED as i32,
25 /// Error occurred from the underlying OS-specific layer.
26 Backend = sys::rs2_exception_type_RS2_EXCEPTION_TYPE_BACKEND as i32,
27 /// An invalid value was passed to the API.
28 InvalidValue = sys::rs2_exception_type_RS2_EXCEPTION_TYPE_INVALID_VALUE as i32,
29 /// Error resulted because a function precondition was violated.
30 ///
31 /// Usually this means that you tried to call a method before a class was properly initialized
32 /// or configured. E.g. this type of error can occur if you try to wait for frames on a
33 /// pipeline before it is started. We attempt to reduce the number of opportunities where this
34 /// can happen by structuring the types around these constraints at a higher level.
35 WrongApiCallSequence =
36 sys::rs2_exception_type_RS2_EXCEPTION_TYPE_WRONG_API_CALL_SEQUENCE as i32,
37 /// The method you tried to call is not implemented.
38 NotImplemented = sys::rs2_exception_type_RS2_EXCEPTION_TYPE_NOT_IMPLEMENTED as i32,
39 /// Error resulted because the device is in recovery mode
40 ///
41 /// The device might require a firmware update.
42 DeviceInRecoveryMode =
43 sys::rs2_exception_type_RS2_EXCEPTION_TYPE_DEVICE_IN_RECOVERY_MODE as i32,
44 /// Error resulted because of an IO device failure.
45 IoDeviceFailure = sys::rs2_exception_type_RS2_EXCEPTION_TYPE_IO as i32,
46 // Not included since this just tells us the total number of exceptions
47 //
48 // Count = sys::rs2_exception_type_RS2_EXCEPTION_TYPE_COUNT,
49}
50
51impl Display for Rs2Exception {
52 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
53 let s = match self {
54 Rs2Exception::Unknown => "Unknown",
55 Rs2Exception::CameraDisconnected => "CameraDisconnected",
56 Rs2Exception::Backend => "Backend",
57 Rs2Exception::InvalidValue => "InvalidValue",
58 Rs2Exception::WrongApiCallSequence => "WrongAPICallSequence",
59 Rs2Exception::NotImplemented => "NotImplemented",
60 Rs2Exception::DeviceInRecoveryMode => "DeviceInRecoveryMode",
61 Rs2Exception::IoDeviceFailure => "IODeviceFailure",
62 };
63
64 write!(f, "{}", s)
65 }
66}
67
68#[cfg(test)]
69mod tests {
70 use super::*;
71 use num_traits::FromPrimitive;
72
73 #[test]
74 fn all_variants_exist() {
75 for i in 0..sys::rs2_exception_type_RS2_EXCEPTION_TYPE_COUNT as i32 {
76 assert!(
77 Rs2Exception::from_i32(i).is_some(),
78 "Rs2Exception variant for ordinal {} does not exist.",
79 i,
80 );
81 }
82 }
83}