From 25695b39ea75969342d3f689965e3fcc627f765d Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Wed, 7 Dec 2022 08:14:55 +0100 Subject: [PATCH 1/2] 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 { From 3fa43d46f990f47233a73e29658714b512ca45a6 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Wed, 7 Dec 2022 14:42:53 +0100 Subject: [PATCH 2/2] bump changelog --- CHANGELOG.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e47dda1..e1f47a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## Changed - `serde` support is now optional and behind the `serde` feature. -- `PusTcSecondaryHeaderT` trait renamed to `GenericPusTcSecondaryHeader` +- `PusTcSecondaryHeaderT` trait renamed to `GenericPusTcSecondaryHeader`. - `PusTmSecondaryHeaderT` trait renamed to `GenericPusTmSecondaryHeader`. - `SpHeader`: Former `tc` and `tm` methods now named `tc_unseg` and `tm_unseg`. Former `new` method now called `new_from_single_fields`. @@ -20,7 +20,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## Added -- ACII timestamps as specified in CCSDS 301.0-B-4 +- Added `std::error::Error` implementation for all error enumerations if the `std` feature + is enabled. +- ACII timestamps as specified in CCSDS 301.0-B-4. - `serde` `Serialize` and `Deserialize` added to all types. - Added `const` constructors for `PacketId`, `PacketSeqCtrl` and `SpHeader`.