Skip to main content

psdk_fruit_emapi/
error.rs

1use std::{error::Error, fmt};
2
3pub type Result<T> = std::result::Result<T, EmapiError>;
4
5#[derive(Debug, Clone, PartialEq, Eq)]
6pub enum EmapiError {
7  Range {
8    name: &'static str,
9    min: u64,
10    max: u64,
11    value: u64,
12  },
13  NotEnoughData {
14    offset: usize,
15    length: usize,
16    data_len: usize,
17  },
18  FrameTooShort,
19  MissingFrameHead,
20  MissingFrameTail,
21  FrameLengthMismatch {
22    expected: usize,
23    actual: usize,
24  },
25  CommandDataTooShort,
26  BadFrameCrc {
27    expected: u32,
28    actual: u32,
29  },
30  PayloadLengthMismatch {
31    expected: usize,
32    actual: usize,
33  },
34  InvalidFrameDataLength,
35  TruncatedTlvHeader,
36  TruncatedTlvValue,
37  InvalidTlvType {
38    tag: u8,
39    expected: &'static str,
40    actual_len: usize,
41  },
42  InvalidUtf8,
43  InvalidCapacity {
44    name: &'static str,
45  },
46  Timeout {
47    message: String,
48  },
49  Connection {
50    message: String,
51  },
52  Protocol {
53    message: String,
54  },
55}
56
57impl fmt::Display for EmapiError {
58  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59    match self {
60      Self::Range {
61        name,
62        min,
63        max,
64        value,
65      } => write!(f, "{name} out of range: {value} is not in {min}..={max}"),
66      Self::NotEnoughData {
67        offset,
68        length,
69        data_len,
70      } => write!(
71        f,
72        "not enough data: offset {offset}, length {length}, data length {data_len}"
73      ),
74      Self::FrameTooShort => f.write_str("frame is too short"),
75      Self::MissingFrameHead => f.write_str("missing frame head"),
76      Self::MissingFrameTail => f.write_str("missing frame tail"),
77      Self::FrameLengthMismatch { expected, actual } => {
78        write!(
79          f,
80          "frame length mismatch: expected {expected}, got {actual}"
81        )
82      }
83      Self::CommandDataTooShort => f.write_str("command data is too short"),
84      Self::BadFrameCrc { .. } => f.write_str("bad frame CRC"),
85      Self::PayloadLengthMismatch { expected, actual } => {
86        write!(
87          f,
88          "payload length mismatch: expected {expected}, got {actual}"
89        )
90      }
91      Self::InvalidFrameDataLength => f.write_str("invalid frame data length"),
92      Self::TruncatedTlvHeader => f.write_str("truncated TLV header"),
93      Self::TruncatedTlvValue => f.write_str("truncated TLV value"),
94      Self::InvalidTlvType {
95        tag,
96        expected,
97        actual_len,
98      } => write!(f, "tag 0x{tag:x} is not {expected}: length {actual_len}"),
99      Self::InvalidUtf8 => f.write_str("invalid UTF-8 string"),
100      Self::InvalidCapacity { name } => write!(f, "invalid {name} capacity"),
101      Self::Timeout { message } => f.write_str(message),
102      Self::Connection { message } => write!(f, "connection error: {message}"),
103      Self::Protocol { message } => write!(f, "{message}"),
104    }
105  }
106}
107
108impl Error for EmapiError {}