1pub type Result<T> = core::result::Result<T, Error>;
3
4pub const EINVAL: i32 = 22;
5pub const NEINVAL: i32 = -EINVAL;
6pub const EILSEQ: i32 = 84;
7pub const NEILSEQ: i32 = -EILSEQ;
8pub const ETIMEDOUT: i32 = 110;
9pub const NETIMEDOUT: i32 = -ETIMEDOUT;
10pub const ENOMEDIUM: i32 = 123;
11pub const NENOMEDIUM: i32 = -ENOMEDIUM;
12
13#[repr(i32)]
15#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
16pub enum Error {
17 #[default]
18 None = 0,
19 Invalid = 22,
21 IllegalSequence = 84,
23 TimedOut = 110,
25 NoMedium = 123,
27 InvalidVariant(usize),
29 InvalidFieldVariant { field: &'static str, value: usize },
31 InvalidValue {
33 value: usize,
34 min: usize,
35 max: usize,
36 },
37 InvalidFieldValue {
39 field: &'static str,
40 value: usize,
41 min: usize,
42 max: usize,
43 },
44 InvalidCrc7 { invalid: u8, calculated: u8 },
46 InvalidLength { len: usize, expected: usize },
48 Unimplemented,
50}
51
52impl Error {
53 pub const fn invalid_field_variant(field: &'static str, value: usize) -> Self {
55 Self::InvalidFieldVariant { field, value }
56 }
57
58 pub const fn invalid_field_value(
60 field: &'static str,
61 value: usize,
62 min: usize,
63 max: usize,
64 ) -> Self {
65 Self::InvalidFieldValue {
66 field,
67 value,
68 min,
69 max,
70 }
71 }
72
73 pub const fn invalid_crc7(invalid: u8, calculated: u8) -> Self {
75 Self::InvalidCrc7 {
76 invalid,
77 calculated,
78 }
79 }
80
81 pub const fn invalid_length(len: usize, expected: usize) -> Self {
83 Self::InvalidLength { len, expected }
84 }
85
86 pub const fn unimplemented() -> Self {
88 Self::Unimplemented
89 }
90
91 pub const fn from_i32(val: i32) -> Option<Self> {
93 match val {
94 EINVAL | NEINVAL => Some(Self::Invalid),
95 EILSEQ | NEILSEQ => Some(Self::IllegalSequence),
96 ETIMEDOUT | NETIMEDOUT => Some(Self::TimedOut),
97 ENOMEDIUM | NENOMEDIUM => Some(Self::NoMedium),
98 _ => None,
99 }
100 }
101}
102
103impl From<core::array::TryFromSliceError> for Error {
104 fn from(err: core::array::TryFromSliceError) -> Self {
105 Self::Invalid
106 }
107}
108
109impl core::fmt::Display for Error {
110 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
111 match self {
112 Self::None => write!(f, "unknown error"),
113 Self::Invalid => write!(f, "invalid error"),
114 Self::IllegalSequence => write!(f, "illegal command sequence"),
115 Self::TimedOut => write!(f, "timed out"),
116 Self::NoMedium => write!(f, "no medium"),
117 Self::InvalidVariant(err) => write!(f, "invalid enum variant: {err}"),
118 Self::InvalidFieldVariant { field, value } => {
119 write!(f, "invalid field: {field}, enum variant: {value}")
120 }
121 Self::InvalidValue { value, min, max } => {
122 write!(f, "invalid value: {value}, min: {min}, max: {max}")
123 }
124 Self::InvalidFieldValue {
125 field,
126 value,
127 min,
128 max,
129 } => {
130 write!(
131 f,
132 "invalid field: {field}, value: {value}, min: {min}, max: {max}"
133 )
134 }
135 Self::InvalidCrc7 {
136 invalid,
137 calculated,
138 } => write!(f, "invalid CRC-7: {invalid}, expected: {calculated}"),
139 Self::InvalidLength { len, expected } => {
140 write!(f, "invalid structure length: {len}, expected: {expected}")
141 }
142 Self::Unimplemented => write!(f, "unimplemented"),
143 }
144 }
145}
146
147impl core::error::Error for Error {}