use crate::bytes;
use core::{
convert::Infallible,
fmt::{Display, Formatter},
};
#[cfg(feature = "std")]
use std::error::Error;
#[derive(Debug)]
pub enum ParserError {
InvalidEnum { enum_type: &'static str, value: u64 },
UnknownMessage { id: u32 },
InvalidPayloadSize {
id: u32,
maximum: usize,
actual: usize,
},
BytesError(bytes::Error),
}
impl From<bytes::Error> for ParserError {
fn from(error: bytes::Error) -> Self {
Self::BytesError(error)
}
}
impl Display for ParserError {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
match self {
Self::InvalidEnum { enum_type, value } => write!(
f,
"Invalid enum value for enum type {enum_type:?}, got {value:?}"
),
Self::UnknownMessage { id } => write!(f, "Unknown message with ID {id:?}"),
Self::InvalidPayloadSize {
id,
maximum,
actual,
} => write!(
f,
"Message {id:?} has payload size {actual}, which exceeds the definition maximum of {maximum}"
),
Self::BytesError(error) => write!(f, "{error}"),
}
}
}
#[cfg(feature = "std")]
impl Error for ParserError {}
#[derive(Debug)]
pub enum MessageReadError {
#[cfg(feature = "std")]
Io(std::io::Error),
#[cfg(all(feature = "embedded", not(feature = "std")))]
Io,
Parse(ParserError),
}
impl MessageReadError {
pub fn eof() -> Self {
#[cfg(feature = "std")]
return Self::Io(std::io::ErrorKind::UnexpectedEof.into());
#[cfg(all(feature = "embedded", not(feature = "std")))]
return Self::Io;
}
}
impl Display for MessageReadError {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
match self {
#[cfg(feature = "std")]
Self::Io(e) => write!(f, "Failed to read message: {e:#?}"),
#[cfg(all(feature = "embedded", not(feature = "std")))]
Self::Io => write!(f, "Failed to read message"),
Self::Parse(e) => write!(f, "Failed to read message: {e:#?}"),
}
}
}
#[cfg(feature = "std")]
impl Error for MessageReadError {}
#[cfg(feature = "std")]
impl From<std::io::Error> for MessageReadError {
fn from(e: std::io::Error) -> Self {
Self::Io(e)
}
}
impl From<ParserError> for MessageReadError {
fn from(e: ParserError) -> Self {
Self::Parse(e)
}
}
#[derive(Debug)]
pub enum EncoderError {
UnknownMessage { id: u32 },
DialectMismatch { id: u32 },
InvalidPayloadSize {
id: u32,
maximum: usize,
actual: usize,
},
InvalidMAVLink1SystemId { id: u32 },
}
impl Display for EncoderError {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
match self {
Self::UnknownMessage { id } => write!(f, "Unknown message with ID {id:?}"),
Self::DialectMismatch { id } => {
write!(f, "Message {id:?} belongs to a different dialect instance")
}
Self::InvalidPayloadSize {
id,
maximum,
actual,
} => write!(
f,
"Message {id:?} has payload size {actual}, which exceeds the buffer size of {maximum}"
),
Self::InvalidMAVLink1SystemId { id } => {
write!(f, "System ID {id:?} is not supported by MAVLink 1")
}
}
}
}
#[cfg(feature = "std")]
impl Error for EncoderError {}
impl From<Infallible> for EncoderError {
fn from(error: Infallible) -> Self {
match error {}
}
}
#[derive(Debug)]
pub enum MessageWriteError {
Encode(EncoderError),
#[cfg(feature = "std")]
Io(std::io::Error),
#[cfg(all(feature = "embedded", not(feature = "std")))]
Io,
MAVLink2Only,
}
impl Display for MessageWriteError {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
match self {
Self::Encode(e) => write!(f, "Failed to encode message: {e}"),
#[cfg(feature = "std")]
Self::Io(e) => write!(f, "Failed to write message: {e:#?}"),
#[cfg(all(feature = "embedded", not(feature = "std")))]
Self::Io => write!(f, "Failed to write message"),
Self::MAVLink2Only => write!(f, "Message is not supported in MAVLink 1"),
}
}
}
#[cfg(feature = "std")]
impl Error for MessageWriteError {}
#[cfg(feature = "std")]
impl From<std::io::Error> for MessageWriteError {
fn from(e: std::io::Error) -> Self {
Self::Io(e)
}
}
impl From<EncoderError> for MessageWriteError {
fn from(e: EncoderError) -> Self {
Self::Encode(e)
}
}