mavlink-core 0.21.0

Implements the MAVLink data interchange format for UAVs.
Documentation
use crate::bytes;
use core::{
    convert::Infallible,
    fmt::{Display, Formatter},
};
#[cfg(feature = "std")]
use std::error::Error;

/// Error while parsing a MAVLink message
#[derive(Debug)]
pub enum ParserError {
    /// Enum value for this enum type does not exist
    InvalidEnum { enum_type: &'static str, value: u64 },
    /// Message ID does not exist in this message set
    UnknownMessage { id: u32 },
    /// Payload size is larger than the message definition permits.
    InvalidPayloadSize {
        id: u32,
        maximum: usize,
        actual: usize,
    },
    /// Errors that occurred in the bytes module.
    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 {}

/// Error while reading and parsing a MAVLink message
#[derive(Debug)]
pub enum MessageReadError {
    /// IO Error while reading
    #[cfg(feature = "std")]
    Io(std::io::Error),
    /// IO Error while reading
    #[cfg(all(feature = "embedded", not(feature = "std")))]
    Io,
    /// Error while parsing
    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)
    }
}

/// Error while encoding a MAVLink message.
#[derive(Debug)]
pub enum EncoderError {
    /// Message ID does not exist in this dialect.
    UnknownMessage { id: u32 },
    /// Message belongs to a different dialect instance.
    DialectMismatch { id: u32 },
    /// Payload does not fit in the output buffer.
    InvalidPayloadSize {
        id: u32,
        maximum: usize,
        actual: usize,
    },
    /// MAVLink 1 only supports system IDs from 0 to 255
    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 {}
    }
}

/// Error while writing a MAVLink message
#[derive(Debug)]
pub enum MessageWriteError {
    /// Message could not be encoded by its dialect.
    Encode(EncoderError),
    /// IO Error while writing
    #[cfg(feature = "std")]
    Io(std::io::Error),
    /// IO Error while writing
    #[cfg(all(feature = "embedded", not(feature = "std")))]
    Io,
    /// Message does not support MAVLink 1
    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)
    }
}