1use core::fmt::{Display, Formatter};
2#[cfg(feature = "std")]
3use std::error::Error;
4
5#[derive(Debug)]
6pub enum ParserError {
7 InvalidFlag { flag_type: &'static str, value: u32 },
8 InvalidEnum { enum_type: &'static str, value: u32 },
9 UnknownMessage { id: u32 },
10}
11
12impl Display for ParserError {
13 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
14 match self {
15 Self::InvalidFlag { flag_type, value } => write!(
16 f,
17 "Invalid flag value for flag type {flag_type:?}, got {value:?}"
18 ),
19 Self::InvalidEnum { enum_type, value } => write!(
20 f,
21 "Invalid enum value for enum type {enum_type:?}, got {value:?}"
22 ),
23 Self::UnknownMessage { id } => write!(f, "Unknown message with ID {id:?}"),
24 }
25 }
26}
27
28#[cfg(feature = "std")]
29impl Error for ParserError {}
30
31#[derive(Debug)]
32pub enum MessageReadError {
33 #[cfg(feature = "std")]
34 Io(std::io::Error),
35 #[cfg(any(feature = "embedded", feature = "embedded-hal-02"))]
36 Io,
37 Parse(ParserError),
38}
39
40impl MessageReadError {
41 pub fn eof() -> Self {
42 #[cfg(feature = "std")]
43 return Self::Io(std::io::ErrorKind::UnexpectedEof.into());
44 #[cfg(any(feature = "embedded", feature = "embedded-hal-02"))]
45 return Self::Io;
46 }
47}
48
49impl Display for MessageReadError {
50 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
51 match self {
52 #[cfg(feature = "std")]
53 Self::Io(e) => write!(f, "Failed to read message: {e:#?}"),
54 #[cfg(any(feature = "embedded", feature = "embedded-hal-02"))]
55 Self::Io => write!(f, "Failed to read message"),
56 Self::Parse(e) => write!(f, "Failed to read message: {e:#?}"),
57 }
58 }
59}
60
61#[cfg(feature = "std")]
62impl Error for MessageReadError {}
63
64#[cfg(feature = "std")]
65impl From<std::io::Error> for MessageReadError {
66 fn from(e: std::io::Error) -> Self {
67 Self::Io(e)
68 }
69}
70
71impl From<ParserError> for MessageReadError {
72 fn from(e: ParserError) -> Self {
73 Self::Parse(e)
74 }
75}
76
77#[derive(Debug)]
78pub enum MessageWriteError {
79 #[cfg(feature = "std")]
80 Io(std::io::Error),
81 #[cfg(any(feature = "embedded", feature = "embedded-hal-02"))]
82 Io,
83}
84
85impl Display for MessageWriteError {
86 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
87 match self {
88 #[cfg(feature = "std")]
89 Self::Io(e) => write!(f, "Failed to write message: {e:#?}"),
90 #[cfg(any(feature = "embedded", feature = "embedded-hal-02"))]
91 Self::Io => write!(f, "Failed to write message"),
92 }
93 }
94}
95
96#[cfg(feature = "std")]
97impl Error for MessageWriteError {}
98
99#[cfg(feature = "std")]
100impl From<std::io::Error> for MessageWriteError {
101 fn from(e: std::io::Error) -> Self {
102 Self::Io(e)
103 }
104}