ccap/
error.rs

1//! Error handling for ccap library
2
3use thiserror::Error;
4
5/// Error types for ccap operations
6#[derive(Debug, Error)]
7pub enum CcapError {
8    /// No error occurred
9    #[error("No error")]
10    None,
11
12    /// No camera device found
13    #[error("No camera device found")]
14    NoDeviceFound,
15
16    /// Invalid device specified
17    #[error("Invalid device: {0}")]
18    InvalidDevice(String),
19
20    /// Camera device open failed
21    #[error("Camera device open failed")]
22    DeviceOpenFailed,
23
24    /// Device already opened
25    #[error("Device already opened")]
26    DeviceAlreadyOpened,
27
28    /// Device not opened
29    #[error("Device not opened")]
30    DeviceNotOpened,
31
32    /// Capture start failed
33    #[error("Capture start failed")]
34    CaptureStartFailed,
35
36    /// Capture stop failed
37    #[error("Capture stop failed")]
38    CaptureStopFailed,
39
40    /// Frame grab failed
41    #[error("Frame grab failed")]
42    FrameGrabFailed,
43
44    /// Timeout occurred
45    #[error("Timeout occurred")]
46    Timeout,
47
48    /// Invalid parameter
49    #[error("Invalid parameter: {0}")]
50    InvalidParameter(String),
51
52    /// Not supported operation
53    #[error("Operation not supported")]
54    NotSupported,
55
56    /// Backend set failed
57    #[error("Backend set failed")]
58    BackendSetFailed,
59
60    /// String conversion error
61    #[error("String conversion error: {0}")]
62    StringConversionError(String),
63
64    /// File operation failed
65    #[error("File operation failed: {0}")]
66    FileOperationFailed(String),
67
68    /// Device not found (alias for NoDeviceFound for compatibility)
69    #[error("Device not found")]
70    DeviceNotFound,
71
72    /// Internal error
73    #[error("Internal error: {0}")]
74    InternalError(String),
75
76    /// Unknown error with error code
77    #[error("Unknown error: {code}")]
78    Unknown {
79        /// Error code from the underlying system
80        code: i32,
81    },
82}
83
84impl From<i32> for CcapError {
85    fn from(code: i32) -> Self {
86        use crate::sys::*;
87
88        // Convert i32 to CcapErrorCode for matching
89        // On some platforms CcapErrorCode might be unsigned
90        let code_u = code as CcapErrorCode;
91
92        #[allow(non_upper_case_globals)]
93        match code_u {
94            CcapErrorCode_CCAP_ERROR_NONE => CcapError::None,
95            CcapErrorCode_CCAP_ERROR_NO_DEVICE_FOUND => CcapError::NoDeviceFound,
96            CcapErrorCode_CCAP_ERROR_INVALID_DEVICE => CcapError::InvalidDevice("".to_string()),
97            CcapErrorCode_CCAP_ERROR_DEVICE_OPEN_FAILED => CcapError::DeviceOpenFailed,
98            CcapErrorCode_CCAP_ERROR_DEVICE_START_FAILED => CcapError::CaptureStartFailed,
99            CcapErrorCode_CCAP_ERROR_DEVICE_STOP_FAILED => CcapError::CaptureStopFailed,
100            CcapErrorCode_CCAP_ERROR_FRAME_CAPTURE_FAILED => CcapError::FrameGrabFailed,
101            CcapErrorCode_CCAP_ERROR_FRAME_CAPTURE_TIMEOUT => CcapError::Timeout,
102            CcapErrorCode_CCAP_ERROR_UNSUPPORTED_PIXEL_FORMAT => CcapError::NotSupported,
103            CcapErrorCode_CCAP_ERROR_UNSUPPORTED_RESOLUTION => CcapError::NotSupported,
104            CcapErrorCode_CCAP_ERROR_PROPERTY_SET_FAILED => {
105                CcapError::InvalidParameter("".to_string())
106            }
107            CcapErrorCode_CCAP_ERROR_MEMORY_ALLOCATION_FAILED => CcapError::Unknown { code },
108            CcapErrorCode_CCAP_ERROR_INTERNAL_ERROR => CcapError::Unknown { code },
109            _ => CcapError::Unknown { code },
110        }
111    }
112}
113
114/// Result type for ccap operations
115pub type Result<T> = std::result::Result<T, CcapError>;