Skip to main content

km003c_lib/
error.rs

1use std::array::TryFromSliceError;
2use std::io;
3use thiserror::Error;
4
5/// The primary error type for the `km003c-rs` library.
6#[derive(Error, Debug)]
7pub enum KMError {
8    #[error("USB device not found. Is the POWER-Z KM003C connected?")]
9    DeviceNotFound,
10
11    #[error("USB error: {0}")]
12    Usb(#[from] nusb::Error),
13
14    #[error("I/O error: {0}")]
15    Io(#[from] io::Error),
16
17    #[error("Timeout during USB operation: {0}")]
18    Timeout(#[from] tokio::time::error::Elapsed),
19
20    #[error("Protocol error: {0}")]
21    Protocol(String),
22
23    #[error("Invalid packet: {0}")]
24    InvalidPacket(String),
25
26    #[error("Insufficient data: expected at least {expected} bytes, got {actual}")]
27    InsufficientData { expected: usize, actual: usize },
28
29    #[error("Parse error at offset {offset}: {message}")]
30    ParseError { offset: usize, message: String },
31
32    #[error("Attribute mismatch: expected {expected:?}, got {actual:?}")]
33    AttributeMismatch { expected: Vec<u16>, actual: Vec<u16> },
34
35    #[error("Serialization is not supported for {packet}")]
36    UnsupportedSerialization { packet: &'static str },
37}
38
39impl From<TryFromSliceError> for KMError {
40    fn from(_: TryFromSliceError) -> Self {
41        KMError::InvalidPacket("Failed to convert slice to array".to_string())
42    }
43}