use std::fmt;
use std::error::Error as StdError;
#[derive(Debug)]
pub enum Error {
DeviceNotFound,
DeviceError(String),
RenderError(String),
ButtonIndexOutOfBounds(usize),
ElgatoError(elgato_streamdeck::StreamDeckError),
ImageError(String),
IoError(std::io::Error),
Custom(String),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::DeviceNotFound => write!(f, "Stream Deck device not found"),
Error::DeviceError(msg) => write!(f, "Device error: {}", msg),
Error::RenderError(msg) => write!(f, "Render error: {}", msg),
Error::ButtonIndexOutOfBounds(index) => {
write!(f, "Button index {} is out of bounds", index)
}
Error::ElgatoError(err) => write!(f, "Elgato Stream Deck error: {}", err),
Error::ImageError(msg) => write!(f, "Image error: {}", msg),
Error::IoError(err) => write!(f, "I/O error: {}", err),
Error::Custom(msg) => write!(f, "{}", msg),
}
}
}
impl StdError for Error {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match self {
Error::IoError(err) => Some(err),
Error::ElgatoError(_) => None, _ => None,
}
}
}
impl From<elgato_streamdeck::StreamDeckError> for Error {
fn from(err: elgato_streamdeck::StreamDeckError) -> Self {
Error::ElgatoError(err)
}
}
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Error::IoError(err)
}
}
impl From<String> for Error {
fn from(msg: String) -> Self {
Error::Custom(msg)
}
}
impl From<&str> for Error {
fn from(msg: &str) -> Self {
Error::Custom(msg.to_string())
}
}
pub type Result<T> = std::result::Result<T, Error>;