1#[derive(Debug, Clone)]
5pub struct InvalidId {
6 pub(crate) id: Option<u32>,
7 pub(crate) extended: bool,
8}
9
10impl std::error::Error for InvalidId {}
11impl std::fmt::Display for InvalidId {
12 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
13 match (self.id, self.extended) {
14 (Some(id), false) => write!(f, "invalid standard CAN ID: 0x{:03X}, maximum valid value is 0x7FF", id),
15 (None, false) => write!(f, "invalid standard CAN ID: allowed values are 0 to 0x7FF"),
16 (Some(id), true) => write!(f, "invalid extended CAN ID: 0x{:08X}, maximum valid value is 0x1FFF_FFFF", id),
17 (None, true) => write!(f, "invalid extended CAN ID, allowed values are 0 to 0x1FFF_FFFF"),
18 }
19 }
20}
21
22#[derive(Clone)]
24pub struct ParseIdError {
25 inner: ParseIdErrorInner,
26}
27
28impl ParseIdError {
29 pub(crate) fn invalid_format(error: std::num::ParseIntError, extended: bool) -> Self {
30 let inner = match error.kind() {
31 std::num::IntErrorKind::PosOverflow => ParseIdErrorInner::InvalidValue(InvalidId { id: None, extended }),
32 std::num::IntErrorKind::NegOverflow => ParseIdErrorInner::InvalidValue(InvalidId { id: None, extended }),
33 _ => ParseIdErrorInner::InvalidFormat(error),
34 };
35 Self { inner }
36 }
37 pub(crate) fn invalid_value(error: InvalidId) -> Self {
38 let inner = ParseIdErrorInner::InvalidValue(error);
39 Self { inner }
40 }
41}
42
43#[derive(Debug, Clone)]
44enum ParseIdErrorInner {
45 InvalidFormat(<u16 as std::str::FromStr>::Err),
46 InvalidValue(InvalidId),
47}
48
49impl std::error::Error for ParseIdError {}
50
51impl std::fmt::Display for ParseIdError {
52 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53 match &self.inner {
54 ParseIdErrorInner::InvalidFormat(e) => e.fmt(f),
55 ParseIdErrorInner::InvalidValue(e) => e.fmt(f),
56 }
57 }
58}
59
60impl std::fmt::Debug for ParseIdError {
61 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
62 std::fmt::Debug::fmt(&self.inner, f)
63 }
64}
65
66#[derive(Clone, Debug)]
68pub struct TryIntoCanDataError {
69 pub(crate) len: usize,
70}
71
72impl std::error::Error for TryIntoCanDataError {}
73
74impl std::fmt::Display for TryIntoCanDataError {
75 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
76 write!(f, "data to large for CAN frame, expected at most 8 bytes, got {}", self.len)
77 }
78}
79
80impl From<TryIntoCanDataError> for std::io::Error {
81 fn from(value: TryIntoCanDataError) -> Self {
82 std::io::Error::new(std::io::ErrorKind::InvalidInput, value.to_string())
83 }
84}
85
86#[derive(Clone)]
88pub struct TryNewCanFrameError {
89 inner: TryNewCanFrameErrorInner,
90}
91
92#[derive(Clone, Debug)]
93enum TryNewCanFrameErrorInner {
94 InvalidId(InvalidId),
95 InvalidData(TryIntoCanDataError),
96}
97
98impl std::error::Error for TryNewCanFrameError { }
99
100impl std::fmt::Display for TryNewCanFrameError {
101 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
102 match &self.inner {
103 TryNewCanFrameErrorInner::InvalidId(e) => e.fmt(f),
104 TryNewCanFrameErrorInner::InvalidData(e) => e.fmt(f),
105 }
106 }
107}
108
109impl std::fmt::Debug for TryNewCanFrameError {
110 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
111 std::fmt::Debug::fmt(&self.inner, f)
112 }
113}
114
115impl From<std::convert::Infallible> for TryNewCanFrameError {
116 fn from(_value: std::convert::Infallible) -> Self {
117 unreachable!()
118 }
119}
120
121impl From<InvalidId> for TryNewCanFrameError {
122 fn from(value: InvalidId) -> Self {
123 Self {
124 inner: TryNewCanFrameErrorInner::InvalidId(value),
125 }
126 }
127}
128
129impl From<TryIntoCanDataError> for TryNewCanFrameError {
130 fn from(value: TryIntoCanDataError) -> Self {
131 Self {
132 inner: TryNewCanFrameErrorInner::InvalidData(value),
133 }
134 }
135}
136
137#[derive(Debug, Clone)]
139pub struct InvalidDataLengthCode {
140 pub(crate) value: u8,
141}
142
143impl std::error::Error for InvalidDataLengthCode {}
144
145impl std::fmt::Display for InvalidDataLengthCode {
146 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
147 write!(f, "invalid data length code: {}, maximum allowed value is 15", self.value)
148 }
149}