Skip to main content

android_usb_serial/
error.rs

1//! USB serial driver errors.
2
3use std::fmt;
4
5/// High-level driver / transport failure.
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub enum UsbSerialError {
8    /// Operation or chip feature not supported.
9    Unsupported(String),
10    /// I/O or USB transfer failure (including stall mapped to a message).
11    Io(String),
12    /// Transfer timed out.
13    TimedOut,
14    /// Device unplugged or USB link lost (bulk IN detach / EPROTO, etc.).
15    Disconnected,
16    /// Transfer cancelled by the host.
17    Cancelled,
18    /// Could not identify / open a suitable driver.
19    ProbeFailed(String),
20}
21
22impl fmt::Display for UsbSerialError {
23    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24        match self {
25            Self::Unsupported(msg) => write!(f, "unsupported: {msg}"),
26            Self::Io(msg) => write!(f, "io: {msg}"),
27            Self::TimedOut => write!(f, "timed out"),
28            Self::Disconnected => write!(f, "disconnected"),
29            Self::Cancelled => write!(f, "cancelled"),
30            Self::ProbeFailed(msg) => write!(f, "probe failed: {msg}"),
31        }
32    }
33}
34
35impl std::error::Error for UsbSerialError {}
36
37/// Result alias for this crate.
38pub type Result<T> = std::result::Result<T, UsbSerialError>;
39
40/// Low-level USB transfer failure (maps into [`UsbSerialError`]).
41#[derive(Debug, Clone, PartialEq, Eq)]
42pub enum TransferError {
43    TimedOut,
44    Stall,
45    Disconnected,
46    Cancelled,
47    Other(String),
48}
49
50impl From<TransferError> for UsbSerialError {
51    fn from(value: TransferError) -> Self {
52        match value {
53            TransferError::TimedOut => Self::TimedOut,
54            TransferError::Disconnected => Self::Disconnected,
55            TransferError::Cancelled => Self::Cancelled,
56            TransferError::Stall => Self::Io("stall".into()),
57            TransferError::Other(msg) => Self::Io(msg),
58        }
59    }
60}
61
62/// Bulk/control transfer outcome for [`crate::transport::BulkIn::read`].
63#[derive(Debug, Clone, PartialEq, Eq)]
64pub enum ReadOutcome {
65    /// Payload bytes received.
66    Data(Vec<u8>),
67    TimedOut,
68    Cancelled,
69}