switch to thiserror completely
This commit is contained in:
parent
c0b4653c01
commit
3b920cbdd8
@ -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<ByteConversionError> 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
|
||||
|
55
src/lib.rs
55
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))]
|
||||
|
@ -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<DateBeforeCcsdsEpochError> 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 {
|
||||
|
@ -63,20 +63,12 @@ pub fn ccsds_time_code_from_p_field(pfield: u8) -> Result<CcsdsTimeCode, u8> {
|
||||
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))]
|
||||
|
Loading…
Reference in New Issue
Block a user