Skip to main content

cue_sdk/
error.rs

1use cue_sdk_sys as ffi;
2
3/// All errors that can be returned by SDK operations.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
5pub enum SdkError {
6    #[error("not connected to iCUE")]
7    NotConnected,
8    #[error("no control over the device")]
9    NoControl,
10    #[error("incompatible protocol version")]
11    IncompatibleProtocol,
12    #[error("invalid arguments")]
13    InvalidArguments,
14    #[error("invalid operation")]
15    InvalidOperation,
16    #[error("device not found")]
17    DeviceNotFound,
18    #[error("operation not allowed")]
19    NotAllowed,
20    #[error("unknown SDK error code: {0}")]
21    Unknown(u32),
22}
23
24/// Convenience alias used throughout this crate.
25pub type Result<T> = std::result::Result<T, SdkError>;
26
27/// Convert a raw `CorsairError` code into a `Result<()>`.
28pub(crate) fn check(code: ffi::CorsairError) -> Result<()> {
29    match code {
30        ffi::CorsairError_CE_Success => Ok(()),
31        ffi::CorsairError_CE_NotConnected => Err(SdkError::NotConnected),
32        ffi::CorsairError_CE_NoControl => Err(SdkError::NoControl),
33        ffi::CorsairError_CE_IncompatibleProtocol => Err(SdkError::IncompatibleProtocol),
34        ffi::CorsairError_CE_InvalidArguments => Err(SdkError::InvalidArguments),
35        ffi::CorsairError_CE_InvalidOperation => Err(SdkError::InvalidOperation),
36        ffi::CorsairError_CE_DeviceNotFound => Err(SdkError::DeviceNotFound),
37        ffi::CorsairError_CE_NotAllowed => Err(SdkError::NotAllowed),
38        other => Err(SdkError::Unknown(other)),
39    }
40}