1use crate::bytes;
2use core::fmt::{Display, Formatter};
3#[cfg(feature = "std")]
4use std::error::Error;
5
6#[derive(Debug)]
8pub enum ParserError {
9 InvalidFlag { flag_type: &'static str, value: u64 },
11 InvalidEnum { enum_type: &'static str, value: u64 },
13 UnknownMessage { id: u32 },
15 BytesError(bytes::Error),
17}
18
19impl From<bytes::Error> for ParserError {
20 fn from(error: bytes::Error) -> Self {
21 Self::BytesError(error)
22 }
23}
24
25impl Display for ParserError {
26 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
27 match self {
28 Self::InvalidFlag { flag_type, value } => write!(
29 f,
30 "Invalid flag value for flag type {flag_type:?}, got {value:?}"
31 ),
32 Self::InvalidEnum { enum_type, value } => write!(
33 f,
34 "Invalid enum value for enum type {enum_type:?}, got {value:?}"
35 ),
36 Self::UnknownMessage { id } => write!(f, "Unknown message with ID {id:?}"),
37 Self::BytesError(error) => write!(f, "{error}"),
38 }
39 }
40}
41
42#[cfg(feature = "std")]
43impl Error for ParserError {}
44
45#[derive(Debug)]
47pub enum MessageReadError {
48 #[cfg(feature = "std")]
50 Io(std::io::Error),
51 #[cfg(any(feature = "embedded", feature = "embedded-hal-02"))]
53 Io,
54 Parse(ParserError),
56}
57
58impl MessageReadError {
59 pub fn eof() -> Self {
60 #[cfg(feature = "std")]
61 return Self::Io(std::io::ErrorKind::UnexpectedEof.into());
62 #[cfg(any(feature = "embedded", feature = "embedded-hal-02"))]
63 return Self::Io;
64 }
65}
66
67impl Display for MessageReadError {
68 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
69 match self {
70 #[cfg(feature = "std")]
71 Self::Io(e) => write!(f, "Failed to read message: {e:#?}"),
72 #[cfg(any(feature = "embedded", feature = "embedded-hal-02"))]
73 Self::Io => write!(f, "Failed to read message"),
74 Self::Parse(e) => write!(f, "Failed to read message: {e:#?}"),
75 }
76 }
77}
78
79#[cfg(feature = "std")]
80impl Error for MessageReadError {}
81
82#[cfg(feature = "std")]
83impl From<std::io::Error> for MessageReadError {
84 fn from(e: std::io::Error) -> Self {
85 Self::Io(e)
86 }
87}
88
89impl From<ParserError> for MessageReadError {
90 fn from(e: ParserError) -> Self {
91 Self::Parse(e)
92 }
93}
94
95#[derive(Debug)]
97pub enum MessageWriteError {
98 #[cfg(feature = "std")]
100 Io(std::io::Error),
101 #[cfg(any(feature = "embedded", feature = "embedded-hal-02"))]
103 Io,
104 MAVLink2Only,
106}
107
108impl Display for MessageWriteError {
109 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
110 match self {
111 #[cfg(feature = "std")]
112 Self::Io(e) => write!(f, "Failed to write message: {e:#?}"),
113 #[cfg(any(feature = "embedded", feature = "embedded-hal-02"))]
114 Self::Io => write!(f, "Failed to write message"),
115 Self::MAVLink2Only => write!(f, "Message is not supported in MAVLink 1"),
116 }
117 }
118}
119
120#[cfg(feature = "std")]
121impl Error for MessageWriteError {}
122
123#[cfg(feature = "std")]
124impl From<std::io::Error> for MessageWriteError {
125 fn from(e: std::io::Error) -> Self {
126 Self::Io(e)
127 }
128}