Skip to main content

ddc_macos/
error.rs

1use core_graphics::base::CGError;
2use ddc::ErrorCode;
3use io_kit_sys::ret::kIOReturnSuccess;
4use mach2::kern_return::{kern_return_t, KERN_FAILURE};
5use thiserror::Error;
6
7/// An error that can occur during DDC/CI communication with a monitor
8#[derive(Error, Debug)]
9pub enum Error {
10    /// Core Graphics errors
11    #[error("Core Graphics error: {0}")]
12    CoreGraphics(CGError),
13    /// Kernel I/O errors
14    #[error("MacOS kernel I/O error: {0}")]
15    Io(kern_return_t),
16    /// DDC/CI errors
17    #[error("DDC/CI error: {0}")]
18    Ddc(ErrorCode),
19    /// Service not found
20    #[error("Service not found")]
21    ServiceNotFound,
22    /// Display location not found
23    #[error("Display location not found")]
24    DisplayLocationNotFound,
25}
26
27pub fn verify_io(result: kern_return_t) -> Result<(), Error> {
28    if result == kIOReturnSuccess {
29        Ok(())
30    } else {
31        Err(Error::Io(result))
32    }
33}
34
35impl From<std::io::Error> for Error {
36    fn from(error: std::io::Error) -> Self {
37        Error::Io(error.raw_os_error().unwrap_or(KERN_FAILURE))
38    }
39}
40
41impl From<ErrorCode> for Error {
42    fn from(error: ErrorCode) -> Self {
43        Error::Ddc(error)
44    }
45}
46
47impl From<CGError> for Error {
48    fn from(error: CGError) -> Self {
49        Error::CoreGraphics(error)
50    }
51}