Skip to main content

sftool_lib/
error.rs

1use thiserror::Error;
2
3/// Convenient result type for `sftool-lib`.
4pub type Result<T> = std::result::Result<T, Error>;
5
6#[derive(Debug, Error)]
7pub enum Error {
8    #[error(transparent)]
9    Io(#[from] std::io::Error),
10
11    #[error(transparent)]
12    Serial(#[from] serialport::Error),
13
14    #[error("probe-rs error: {0}")]
15    ProbeRs(#[from] probe_rs::Error),
16
17    #[error("Intel HEX parse error: {0}")]
18    IntelHex(#[from] ihex::ReaderError),
19
20    #[error("ELF parse error: {0}")]
21    Elf(#[from] goblin::error::Error),
22
23    #[error("integer parse error: {0}")]
24    ParseInt(#[from] std::num::ParseIntError),
25
26    #[error("protocol error: {0}")]
27    Protocol(String),
28
29    #[error("invalid input: {0}")]
30    InvalidInput(String),
31
32    #[error("timeout while {0}")]
33    Timeout(String),
34
35    #[error("operation cancelled")]
36    Cancelled,
37
38    #[error("configuration error: {0}")]
39    Config(String),
40
41    #[error("unsupported chip: {0}")]
42    UnsupportedChip(String),
43
44    #[error("unsupported memory: {0}")]
45    UnsupportedMemory(String),
46
47    #[error("CRC mismatch: expected {expected:#010X}, got {actual:#010X}")]
48    CrcMismatch { expected: u32, actual: u32 },
49
50    #[error("embedded asset `{0}` not found")]
51    MissingEmbeddedAsset(&'static str),
52}
53
54impl Error {
55    pub fn protocol(msg: impl Into<String>) -> Self {
56        Self::Protocol(msg.into())
57    }
58
59    pub fn invalid_input(msg: impl Into<String>) -> Self {
60        Self::InvalidInput(msg.into())
61    }
62
63    pub fn timeout(msg: impl Into<String>) -> Self {
64        Self::Timeout(msg.into())
65    }
66
67    pub fn cancelled() -> Self {
68        Self::Cancelled
69    }
70}