1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use crate::interface::{Command, CommandStatus};

pub type Result<T> = std::result::Result<T, Error>;

#[derive(Error, Debug)]
pub enum Error {
    #[error("Unknown error.")]
    Unknown,
    #[error("No data was available to be read.")]
    NoData,
    #[error("IO error: {:?}.", source)]
    IO {
        #[source]
        source: ::std::io::Error,
    },
    #[error("Command {:?} returned {:?}.", status, opcode)]
    CommandError {
        opcode: Command,
        status: CommandStatus,
    },
    #[error("Unknown opcode: {:x}.", opcode)]
    UnknownOpcode { opcode: u16 },
    #[error("Unknown command status: {:x}.", status)]
    UnknownStatus { status: u8 },
    #[error("Timed out.")]
    TimedOut,
    #[error("The socket received invalid data.")]
    InvalidData,
    #[error(
        "The name {} is too long; the maximum length is {} bytes.",
        name,
        max_len
    )]
    NameTooLong { name: String, max_len: u32 },
    #[error("A string was supplied that contained a null byte: {}", source)]
    NullByte {
        #[source]
        source: ::std::ffi::NulError,
    },
    #[error("The pin code is too long; the maximum length is {} bytes.", max_len)]
    PinCodeTooLong { max_len: u32 },
}

impl From<std::io::Error> for Error {
    fn from(err: std::io::Error) -> Self {
        Error::IO { source: err }
    }
}

impl From<std::ffi::NulError> for Error {
    fn from(err: std::ffi::NulError) -> Self {
        Error::NullByte { source: err }
    }
}