streamdeck_oxide/
error.rs

1//! Error types for the Stream Deck library.
2//!
3//! This module provides custom error types for the Stream Deck library.
4
5use std::fmt;
6use std::error::Error as StdError;
7
8/// Errors that can occur when using the Stream Deck library.
9#[derive(Debug)]
10pub enum Error {
11    /// The requested device was not found.
12    DeviceNotFound,
13    /// An error occurred while communicating with the device.
14    DeviceError(String),
15    /// An error occurred while rendering a button.
16    RenderError(String),
17    /// The button index is out of bounds.
18    ButtonIndexOutOfBounds(usize),
19    /// An error occurred in the underlying Stream Deck library.
20    ElgatoError(elgato_streamdeck::StreamDeckError),
21    /// An error occurred in the image processing library.
22    ImageError(String),
23    /// An I/O error occurred.
24    IoError(std::io::Error),
25    /// A custom error with a message.
26    Custom(String),
27}
28
29impl fmt::Display for Error {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        match self {
32            Error::DeviceNotFound => write!(f, "Stream Deck device not found"),
33            Error::DeviceError(msg) => write!(f, "Device error: {}", msg),
34            Error::RenderError(msg) => write!(f, "Render error: {}", msg),
35            Error::ButtonIndexOutOfBounds(index) => {
36                write!(f, "Button index {} is out of bounds", index)
37            }
38            Error::ElgatoError(err) => write!(f, "Elgato Stream Deck error: {}", err),
39            Error::ImageError(msg) => write!(f, "Image error: {}", msg),
40            Error::IoError(err) => write!(f, "I/O error: {}", err),
41            Error::Custom(msg) => write!(f, "{}", msg),
42        }
43    }
44}
45
46impl StdError for Error {
47    fn source(&self) -> Option<&(dyn StdError + 'static)> {
48        match self {
49            Error::IoError(err) => Some(err),
50            Error::ElgatoError(_) => None, // StreamDeckError doesn't implement StdError
51            _ => None,
52        }
53    }
54}
55
56impl From<elgato_streamdeck::StreamDeckError> for Error {
57    fn from(err: elgato_streamdeck::StreamDeckError) -> Self {
58        Error::ElgatoError(err)
59    }
60}
61
62impl From<std::io::Error> for Error {
63    fn from(err: std::io::Error) -> Self {
64        Error::IoError(err)
65    }
66}
67
68impl From<String> for Error {
69    fn from(msg: String) -> Self {
70        Error::Custom(msg)
71    }
72}
73
74impl From<&str> for Error {
75    fn from(msg: &str) -> Self {
76        Error::Custom(msg.to_string())
77    }
78}
79
80/// A specialized Result type for Stream Deck operations.
81pub type Result<T> = std::result::Result<T, Error>;