mavwire 0.1.0

no_std, no_alloc parser and encoder for MAVLink v1/v2 telemetry messages
Documentation
//! Little-endian field reads that treat any offset past the end of the
//! slice as zero.
//!
//! A `MAVLink` v2 sender strips trailing zero bytes off the wire (see
//! `frame.rs`), so a frame's payload can be shorter than a message's full
//! defined length; reading a trailing field must reconstruct the zero it
//! was encoding, not panic or silently read garbage.

fn bytes_at<const N: usize>(payload: &[u8], offset: usize) -> [u8; N] {
    let mut buf = [0u8; N];
    if offset < payload.len() {
        let end = (offset + N).min(payload.len());
        buf[..end - offset].copy_from_slice(&payload[offset..end]);
    }
    buf
}

pub(crate) fn u8_at(payload: &[u8], offset: usize) -> u8 {
    payload.get(offset).copied().unwrap_or(0)
}

pub(crate) fn i8_at(payload: &[u8], offset: usize) -> i8 {
    u8_at(payload, offset).cast_signed()
}

pub(crate) fn u16_at(payload: &[u8], offset: usize) -> u16 {
    u16::from_le_bytes(bytes_at(payload, offset))
}

pub(crate) fn i16_at(payload: &[u8], offset: usize) -> i16 {
    i16::from_le_bytes(bytes_at(payload, offset))
}

pub(crate) fn u32_at(payload: &[u8], offset: usize) -> u32 {
    u32::from_le_bytes(bytes_at(payload, offset))
}

pub(crate) fn i32_at(payload: &[u8], offset: usize) -> i32 {
    i32::from_le_bytes(bytes_at(payload, offset))
}

pub(crate) fn u64_at(payload: &[u8], offset: usize) -> u64 {
    u64::from_le_bytes(bytes_at(payload, offset))
}

pub(crate) fn f32_at(payload: &[u8], offset: usize) -> f32 {
    f32::from_bits(u32_at(payload, offset))
}

/// Copies up to `N` bytes starting at `offset` into a fixed-size array,
/// zero-filling anything past the end of `payload` (see the module docs).
/// Used to read fixed-width char-array fields like `PARAM_VALUE`'s
/// `param_id` (see [`super::text::FixedText`]).
pub(crate) fn array_at<const N: usize>(payload: &[u8], offset: usize) -> [u8; N] {
    bytes_at(payload, offset)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn reads_fields_within_bounds() {
        let payload = [0x01, 0x02, 0x03, 0x04, 0x05];
        assert_eq!(u8_at(&payload, 4), 0x05);
        assert_eq!(u32_at(&payload, 0), 0x0403_0201);
        assert_eq!(i16_at(&payload, 0), 0x0201);
        assert_eq!(i8_at(&payload, 0), 1);
    }

    #[test]
    fn zero_extends_past_a_truncated_payload() {
        let payload = [0x01, 0x02];
        assert_eq!(u8_at(&payload, 5), 0);
        assert_eq!(u32_at(&payload, 0), 0x0000_0201);
        assert_eq!(u32_at(&payload, 10), 0);
        assert_eq!(u16_at(&payload, 1), 0x0002);
        assert_eq!(u64_at(&payload, 10), 0);
    }

    #[test]
    fn f32_at_reads_ieee754_bits() {
        let payload = 1.5f32.to_le_bytes();
        assert!((f32_at(&payload, 0) - 1.5).abs() < f32::EPSILON);
    }

    #[test]
    fn array_at_zero_extends_partial_overlap() {
        let payload = [0xAA, 0xBB];
        let arr: [u8; 4] = array_at(&payload, 1);
        assert_eq!(arr, [0xBB, 0, 0, 0]);
    }
}