mavlink_core/
error.rs

1use crate::bytes;
2use core::fmt::{Display, Formatter};
3#[cfg(feature = "std")]
4use std::error::Error;
5
6/// Error while parsing a MAVLink message
7#[derive(Debug)]
8pub enum ParserError {
9    /// Bit flag for this type is invalid
10    InvalidFlag { flag_type: &'static str, value: u64 },
11    /// Enum value for this enum type does not exist
12    InvalidEnum { enum_type: &'static str, value: u64 },
13    /// Message ID does not exist in this message set
14    UnknownMessage { id: u32 },
15    /// Errors that occurred in the bytes module.
16    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/// Error while reading and parsing a MAVLink message
46#[derive(Debug)]
47pub enum MessageReadError {
48    /// IO Error while reading
49    #[cfg(feature = "std")]
50    Io(std::io::Error),
51    /// IO Error while reading
52    #[cfg(any(feature = "embedded", feature = "embedded-hal-02"))]
53    Io,
54    /// Error while parsing
55    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/// Error while writing a MAVLink message
96#[derive(Debug)]
97pub enum MessageWriteError {
98    /// IO Error while writing
99    #[cfg(feature = "std")]
100    Io(std::io::Error),
101    /// IO Error while writing
102    #[cfg(any(feature = "embedded", feature = "embedded-hal-02"))]
103    Io,
104    /// Message does not support MAVLink 1
105    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}