use std::fmt;
#[derive(Debug)]
pub enum Error {
PermissionDenied { path: String },
DeviceNotFound { path: String },
MissingReport { report_name: String },
LampIdMismatch { expected: u16, got: u16 },
LampIdOutOfRange { lamp_id: u16, lamp_count: u16 },
DuplicateLampId { lamp_id: u16 },
NoAutonomousMode,
NoMultiUpdate,
TruncatedReport {
report_name: &'static str,
expected: usize,
got: usize,
},
UnsupportedReportType,
InvalidArgument(String),
Io(std::io::Error),
Arg(lexopt::Error),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::PermissionDenied { path } => {
write!(f, "Permission denied on {path}")
}
Self::DeviceNotFound { path } => {
write!(f, "Device not found: {path}")
}
Self::MissingReport { report_name } => {
write!(f, "Device has no '{report_name}' report")
}
Self::LampIdMismatch { expected, got } => {
write!(
f,
"LampAttributesResponse returned LampId {got}, expected {expected}"
)
}
Self::LampIdOutOfRange {
lamp_id,
lamp_count,
} => {
write!(f, "LampId {lamp_id} exceeds device LampCount {lamp_count}")
}
Self::DuplicateLampId { lamp_id } => {
write!(f, "Duplicate LampId {lamp_id} in update request")
}
Self::NoAutonomousMode => {
write!(
f,
"This device does not support autonomous mode. \
Only LampArray (Usage Page 0x59) devices have this feature."
)
}
Self::NoMultiUpdate => {
write!(
f,
"Per-lamp color control requires a LampArray device (Usage Page 0x59) \
with a LampMultiUpdateReport."
)
}
Self::TruncatedReport {
report_name,
expected,
got,
} => {
write!(
f,
"Truncated {report_name} report: expected at least {expected} bytes, got {got}"
)
}
Self::UnsupportedReportType => {
write!(f, "Cannot write to an Input report (device-to-host only)")
}
Self::InvalidArgument(msg) => {
write!(f, "{msg}")
}
Self::Io(e) => write!(f, "{e}"),
Self::Arg(e) => write!(f, "{e}"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Io(e) => Some(e),
Self::Arg(e) => Some(e),
_ => None,
}
}
}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Self::Io(e)
}
}
impl From<lexopt::Error> for Error {
fn from(e: lexopt::Error) -> Self {
Self::Arg(e)
}
}
pub type Result<T> = std::result::Result<T, Error>;