From 25695b39ea75969342d3f689965e3fcc627f765d Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Wed, 7 Dec 2022 08:14:55 +0100 Subject: [PATCH] add error impls for feature std --- src/ecss.rs | 43 ++++++++++++++++++++++++++++++++++++++++++- src/lib.rs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/src/ecss.rs b/src/ecss.rs index 06e40f4..f06b4c7 100644 --- a/src/ecss.rs +++ b/src/ecss.rs @@ -1,11 +1,13 @@ //! Common definitions and helpers required to create PUS TMTC packets according to //! [ECSS-E-ST-70-41C](https://ecss.nl/standard/ecss-e-st-70-41c-space-engineering-telemetry-and-telecommand-packet-utilization-15-april-2016/) use crate::{ByteConversionError, CcsdsPacket, SizeMissmatch}; -use core::fmt::Debug; +use core::fmt::{Debug, Display, Formatter}; use core::mem::size_of; use crc::{Crc, CRC_16_IBM_3740}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; +#[cfg(feature = "std")] +use std::error::Error; pub type CrcType = u16; @@ -65,6 +67,45 @@ pub enum PusError { ByteConversionError(ByteConversionError), } +impl Display for PusError { + fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { + match self { + PusError::VersionNotSupported(v) => { + write!(f, "PUS version {:?} not supported", v) + } + PusError::IncorrectCrc(crc) => { + write!(f, "crc16 {:#04x} is incorrect", crc) + } + PusError::RawDataTooShort(size) => { + write!( + f, + "deserialization error, provided raw data with size {} too short", + size + ) + } + PusError::NoRawData => { + write!(f, "no raw data provided") + } + PusError::CrcCalculationMissing => { + write!(f, "crc16 was not calculated") + } + PusError::ByteConversionError(e) => { + write!(f, "low level byte conversion error: {}", e) + } + } + } +} + +#[cfg(feature = "std")] +impl Error for PusError { + fn source(&self) -> Option<&(dyn Error + 'static)> { + if let PusError::ByteConversionError(e) = self { + return Some(e); + } + None + } +} + impl From for PusError { fn from(e: ByteConversionError) -> Self { PusError::ByteConversionError(e) diff --git a/src/lib.rs b/src/lib.rs index 9aa7955..1ca6fdd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -53,7 +53,10 @@ extern crate alloc; extern crate std; use crate::ecss::CCSDS_HEADER_LEN; +use core::fmt::{Display, Formatter}; use delegate::delegate; +#[cfg(feature = "std")] +use std::error::Error; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -85,6 +88,36 @@ pub enum ByteConversionError { ZeroCopyFromError, } +impl Display for ByteConversionError { + fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { + match self { + ByteConversionError::ToSliceTooSmall(missmatch) => { + write!( + f, + "target slice with size {} is too small, expected size of at least {}", + missmatch.found, missmatch.expected + ) + } + ByteConversionError::FromSliceTooSmall(missmatch) => { + write!( + f, + "source slice with size {} too small, expected at least {} bytes", + missmatch.found, missmatch.expected + ) + } + ByteConversionError::ZeroCopyToError => { + write!(f, "zerocopy serialization error") + } + ByteConversionError::ZeroCopyFromError => { + write!(f, "zerocopy deserialization error") + } + } + } +} + +#[cfg(feature = "std")] +impl Error for ByteConversionError {} + #[derive(Debug, PartialEq, Eq, Copy, Clone)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum PacketType {