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