From 3b920cbdd8b70a3caad8693ff53b086f35870f5e Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Fri, 8 Nov 2024 14:17:36 +0100 Subject: [PATCH] switch to thiserror completely --- src/ecss/mod.rs | 49 +++++++------------------------------------ src/lib.rs | 55 ++++++++----------------------------------------- src/time/cds.rs | 44 ++++++--------------------------------- src/time/mod.rs | 12 ++--------- 4 files changed, 25 insertions(+), 135 deletions(-) diff --git a/src/ecss/mod.rs b/src/ecss/mod.rs index cfac598..20ce84d 100644 --- a/src/ecss/mod.rs +++ b/src/ecss/mod.rs @@ -6,13 +6,11 @@ use crate::{ByteConversionError, CcsdsPacket, CRC_CCITT_FALSE}; #[cfg(feature = "alloc")] use alloc::vec::Vec; -use core::fmt::{Debug, Display, Formatter}; +use core::fmt::Debug; use core::mem::size_of; use num_enum::{IntoPrimitive, TryFromPrimitive}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; -#[cfg(feature = "std")] -use std::error::Error; pub mod event; pub mod hk; @@ -148,50 +146,19 @@ pub enum PfcReal { DoubleMilStd = 4, } -#[derive(Debug, Copy, Clone, PartialEq, Eq)] +#[derive(Debug, Copy, Clone, PartialEq, Eq, thiserror::Error)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum PusError { + #[error("PUS version {0:?} not supported")] VersionNotSupported(PusVersion), + #[error("checksum verification for crc16 {0:#06x} failed")] ChecksumFailure(u16), /// CRC16 needs to be calculated first - CrcCalculationMissing, - ByteConversion(ByteConversionError), -} - -impl Display for PusError { - fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { - match self { - PusError::VersionNotSupported(v) => { - write!(f, "PUS version {v:?} not supported") - } - PusError::ChecksumFailure(crc) => { - write!(f, "checksum verification for crc16 {crc:#06x} failed") - } - PusError::CrcCalculationMissing => { - write!(f, "crc16 was not calculated") - } - PusError::ByteConversion(e) => { - write!(f, "pus error: {e}") - } - } - } -} - -#[cfg(feature = "std")] -impl Error for PusError { - fn source(&self) -> Option<&(dyn Error + 'static)> { - if let PusError::ByteConversion(e) = self { - return Some(e); - } - None - } -} - -impl From for PusError { - fn from(e: ByteConversionError) -> Self { - PusError::ByteConversion(e) - } + //#[error("crc16 was not calculated")] + //CrcCalculationMissing, + #[error("pus error: {0}")] + ByteConversion(#[from] ByteConversionError), } /// Generic trait to describe common attributes for both PUS Telecommands (TC) and PUS Telemetry diff --git a/src/lib.rs b/src/lib.rs index bdb1fd9..b824379 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -61,17 +61,11 @@ extern crate alloc; #[cfg(any(feature = "std", test))] extern crate std; -use core::{ - fmt::{Debug, Display, Formatter}, - hash::Hash, -}; +use core::{fmt::Debug, hash::Hash}; use crc::{Crc, CRC_16_IBM_3740}; use delegate::delegate; use zerocopy::{FromBytes, IntoBytes}; -#[cfg(feature = "std")] -use std::error::Error; - #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -94,55 +88,24 @@ pub const MAX_APID: u16 = 2u16.pow(11) - 1; pub const MAX_SEQ_COUNT: u16 = 2u16.pow(14) - 1; /// Generic error type when converting to and from raw byte slices. -#[derive(Debug, Copy, Clone, PartialEq, Eq)] +#[derive(Debug, Copy, Clone, PartialEq, Eq, thiserror::Error)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum ByteConversionError { /// The passed slice is too small. Returns the passed slice length and expected minimum size - ToSliceTooSmall { - found: usize, - expected: usize, - }, + #[error("target slice with size {found} is too small, expected size of at least {expected}")] + ToSliceTooSmall { found: usize, expected: usize }, /// The provider buffer is too small. Returns the passed slice length and expected minimum size - FromSliceTooSmall { - found: usize, - expected: usize, - }, + #[error("source slice with size {found} too small, expected at least {expected} bytes")] + FromSliceTooSmall { found: usize, expected: usize }, /// The [zerocopy] library failed to write to bytes + #[error("zerocopy serialization error")] ZeroCopyToError, + /// The [zerocopy] library failed to read from bytes + #[error("zerocopy deserialization error")] ZeroCopyFromError, } -impl Display for ByteConversionError { - fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { - match self { - ByteConversionError::ToSliceTooSmall { found, expected } => { - write!( - f, - "target slice with size {} is too small, expected size of at least {}", - found, expected - ) - } - ByteConversionError::FromSliceTooSmall { found, expected } => { - write!( - f, - "source slice with size {} too small, expected at least {} bytes", - found, expected - ) - } - ByteConversionError::ZeroCopyToError => { - write!(f, "zerocopy serialization error") - } - ByteConversionError::ZeroCopyFromError => { - write!(f, "zerocopy deserialization error") - } - } - } -} - -#[cfg(feature = "std")] -impl Error for ByteConversionError {} - /// CCSDS packet type enumeration. #[derive(Debug, PartialEq, Eq, Copy, Clone)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] diff --git a/src/time/cds.rs b/src/time/cds.rs index 9b7eb94..8e1ee00 100644 --- a/src/time/cds.rs +++ b/src/time/cds.rs @@ -7,15 +7,13 @@ use crate::private::Sealed; use crate::ByteConversionError; use core::cmp::Ordering; -use core::fmt::{Debug, Display, Formatter}; +use core::fmt::Debug; use core::ops::{Add, AddAssign}; use core::time::Duration; #[cfg(feature = "std")] use super::StdTimestampError; #[cfg(feature = "std")] -use std::error::Error; -#[cfg(feature = "std")] use std::time::{SystemTime, SystemTimeError}; #[cfg(feature = "chrono")] @@ -91,49 +89,19 @@ pub enum SubmillisPrecision { Reserved = 0b11, } -#[derive(Debug, PartialEq, Eq, Copy, Clone)] +#[derive(Debug, PartialEq, Eq, Copy, Clone, thiserror::Error)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum CdsError { /// CCSDS days value exceeds maximum allowed size or is negative + #[error("invalid ccsds days {0}")] InvalidCcsdsDays(i64), /// There are distinct constructors depending on the days field width detected in the preamble /// field. This error will be returned if there is a missmatch. + #[error("wrong constructor for length of day {0:?} detected in preamble")] InvalidCtorForDaysOfLenInPreamble(LengthOfDaySegment), - DateBeforeCcsdsEpoch(DateBeforeCcsdsEpochError), -} - -impl Display for CdsError { - fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { - match self { - CdsError::InvalidCcsdsDays(days) => { - write!(f, "invalid ccsds days {days}") - } - CdsError::InvalidCtorForDaysOfLenInPreamble(length_of_day) => { - write!( - f, - "wrong constructor for length of day {length_of_day:?} detected in preamble", - ) - } - CdsError::DateBeforeCcsdsEpoch(e) => write!(f, "date before CCSDS epoch: {e}"), - } - } -} - -#[cfg(feature = "std")] -impl Error for CdsError { - fn source(&self) -> Option<&(dyn Error + 'static)> { - match self { - CdsError::DateBeforeCcsdsEpoch(e) => Some(e), - _ => None, - } - } -} - -impl From for CdsError { - fn from(value: DateBeforeCcsdsEpochError) -> Self { - Self::DateBeforeCcsdsEpoch(value) - } + #[error("date before CCSDS epoch: {0}")] + DateBeforeCcsdsEpoch(#[from] DateBeforeCcsdsEpochError), } pub fn length_of_day_segment_from_pfield(pfield: u8) -> LengthOfDaySegment { diff --git a/src/time/mod.rs b/src/time/mod.rs index 63cb5af..044208e 100644 --- a/src/time/mod.rs +++ b/src/time/mod.rs @@ -63,20 +63,12 @@ pub fn ccsds_time_code_from_p_field(pfield: u8) -> Result { CcsdsTimeCode::try_from(raw_bits).map_err(|_| raw_bits) } -#[derive(Debug, PartialEq, Eq, Copy, Clone)] +#[derive(Debug, PartialEq, Eq, Copy, Clone, thiserror::Error)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "defmt", derive(defmt::Format))] +#[error("date before ccsds epoch: {0:?}")] pub struct DateBeforeCcsdsEpochError(UnixTime); -impl Display for DateBeforeCcsdsEpochError { - fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { - write!(f, "date before ccsds epoch: {:?}", self.0) - } -} - -#[cfg(feature = "std")] -impl Error for DateBeforeCcsdsEpochError {} - #[derive(Debug, PartialEq, Eq, Copy, Clone)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "defmt", derive(defmt::Format))]