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