From e78f196a425dbc3f944eb22ae815576614cd212b Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Mon, 28 Aug 2023 17:10:45 +0200 Subject: [PATCH 1/3] invalid time code struct variant --- src/time/cds.rs | 29 ++++++++++++++++------------- src/time/cuc.rs | 16 ++++++++-------- src/time/mod.rs | 9 ++++----- 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/src/time/cds.rs b/src/time/cds.rs index 3db55bd..de4cc40 100644 --- a/src/time/cds.rs +++ b/src/time/cds.rs @@ -428,14 +428,17 @@ pub fn get_dyn_time_provider_from_bytes( ) -> Result, TimestampError> { let time_code = ccsds_time_code_from_p_field(buf[0]); if let Err(e) = time_code { - return Err(TimestampError::InvalidTimeCode(CcsdsTimeCodes::Cds, e)); + return Err(TimestampError::InvalidTimeCode { + expected: CcsdsTimeCodes::Cds, + found: e, + }); } let time_code = time_code.unwrap(); if time_code != CcsdsTimeCodes::Cds { - return Err(TimestampError::InvalidTimeCode( - CcsdsTimeCodes::Cds, - time_code as u8, - )); + return Err(TimestampError::InvalidTimeCode { + expected: CcsdsTimeCodes::Cds, + found: time_code as u8, + }); } if length_of_day_segment_from_pfield(buf[0]) == LengthOfDaySegment::Short16Bits { Ok(Box::new(TimeProvider::from_bytes_with_u16_days(buf)?)) @@ -523,17 +526,17 @@ impl TimeProvider { Ok(cds_type) => match cds_type { CcsdsTimeCodes::Cds => (), _ => { - return Err(TimestampError::InvalidTimeCode( - CcsdsTimeCodes::Cds, - cds_type as u8, - )) + return Err(TimestampError::InvalidTimeCode { + expected: CcsdsTimeCodes::Cds, + found: cds_type as u8, + }) } }, _ => { - return Err(TimestampError::InvalidTimeCode( - CcsdsTimeCodes::Cds, - pfield >> 4 & 0b111, - )) + return Err(TimestampError::InvalidTimeCode { + expected: CcsdsTimeCodes::Cds, + found: pfield >> 4 & 0b111, + }); } }; if ((pfield >> 3) & 0b1) == 1 { diff --git a/src/time/cuc.rs b/src/time/cuc.rs index 22d3b6f..e7a090b 100644 --- a/src/time/cuc.rs +++ b/src/time/cuc.rs @@ -463,17 +463,17 @@ impl TimeReader for TimeProviderCcsdsEpoch { match ccsds_time_code_from_p_field(buf[0]) { Ok(code) => { if code != CcsdsTimeCodes::CucCcsdsEpoch { - return Err(TimestampError::InvalidTimeCode( - CcsdsTimeCodes::CucCcsdsEpoch, - code as u8, - )); + return Err(TimestampError::InvalidTimeCode { + expected: CcsdsTimeCodes::CucCcsdsEpoch, + found: code as u8, + }); } } Err(raw) => { - return Err(TimestampError::InvalidTimeCode( - CcsdsTimeCodes::CucCcsdsEpoch, - raw, - )) + return Err(TimestampError::InvalidTimeCode { + expected: CcsdsTimeCodes::CucCcsdsEpoch, + found: raw, + }); } } let (cntr_len, fractions_len, total_len) = diff --git a/src/time/mod.rs b/src/time/mod.rs index dbfe995..9350499 100644 --- a/src/time/mod.rs +++ b/src/time/mod.rs @@ -5,6 +5,7 @@ use core::cmp::Ordering; use core::fmt::{Display, Formatter}; use core::ops::{Add, AddAssign}; use core::time::Duration; +use core::u8; #[allow(unused_imports)] #[cfg(not(feature = "std"))] @@ -63,9 +64,7 @@ pub fn ccsds_time_code_from_p_field(pfield: u8) -> Result { #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[non_exhaustive] pub enum TimestampError { - /// Contains tuple where first value is the expected time code and the second - /// value is the found raw value - InvalidTimeCode(CcsdsTimeCodes, u8), + InvalidTimeCode { expected: CcsdsTimeCodes, found: u8 }, ByteConversion(ByteConversionError), Cds(cds::CdsError), Cuc(cuc::CucError), @@ -76,10 +75,10 @@ pub enum TimestampError { impl Display for TimestampError { fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { match self { - TimestampError::InvalidTimeCode(time_code, raw_val) => { + TimestampError::InvalidTimeCode { expected, found } => { write!( f, - "invalid raw time code value {raw_val} for time code {time_code:?}" + "invalid raw time code value {found} for time code {expected:?}" ) } TimestampError::Cds(e) => { -- 2.43.0 From 0b5a384743c7ebbf8f07aa2b73d9512392f781e2 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Mon, 28 Aug 2023 17:24:54 +0200 Subject: [PATCH 2/3] fix tests --- src/time/cds.rs | 6 +++--- src/time/cuc.rs | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/time/cds.rs b/src/time/cds.rs index 4e88e27..e9ef8d7 100644 --- a/src/time/cds.rs +++ b/src/time/cds.rs @@ -1493,9 +1493,9 @@ mod tests { assert!(res.is_err()); let err = res.unwrap_err(); match err { - InvalidTimeCode(code, raw) => { - assert_eq!(code, CcsdsTimeCodes::Cds); - assert_eq!(raw, 0); + InvalidTimeCode { expected, found } => { + assert_eq!(expected, CcsdsTimeCodes::Cds); + assert_eq!(found, 0); } _ => {} } diff --git a/src/time/cuc.rs b/src/time/cuc.rs index b97067b..252faea 100644 --- a/src/time/cuc.rs +++ b/src/time/cuc.rs @@ -909,9 +909,9 @@ mod tests { let res = TimeProviderCcsdsEpoch::from_bytes(&buf); assert!(res.is_err()); let err = res.unwrap_err(); - if let TimestampError::InvalidTimeCode(code, raw) = err { - assert_eq!(code, CcsdsTimeCodes::CucCcsdsEpoch); - assert_eq!(raw, CcsdsTimeCodes::CucAgencyEpoch as u8); + if let TimestampError::InvalidTimeCode { expected, found } = err { + assert_eq!(expected, CcsdsTimeCodes::CucCcsdsEpoch); + assert_eq!(found, CcsdsTimeCodes::CucAgencyEpoch as u8); } else { panic!("unexpected error: {}", err); } -- 2.43.0 From e090beedde68911e5e6d41ea0896626f4c720ec1 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Mon, 28 Aug 2023 17:30:26 +0200 Subject: [PATCH 3/3] CHANGELOG --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c3f4fd..4d5166e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - The `Tlv` and `Lv` API return `&[u8]` instead of `Option<&[u8]>`. - `ByteConversionError` error variants `ToSliceTooSmall` and `FromSliceTooSmall` are struct variants now. `SizeMissmatch` was removed appropriately. +- `UnsignedByteFieldError` error variants `ValueTooLargeForWidth` and `InvalidWidth` are struct + variants now. +- `TimestampError` error variant `InvalidTimeCode` is struct variant now. ## Added -- 2.43.0