this is a lot better

This commit is contained in:
Robin Müller 2022-12-07 14:42:01 +01:00
parent 878f072e9a
commit 4cd8f143df
No known key found for this signature in database
GPG Key ID: 71B58F8A3CDFA9AC
3 changed files with 192 additions and 137 deletions

View File

@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
Former `new` method now called `new_from_single_fields`.
- `SpHeader`: Renamed `from_bytes` to `from_be_bytes`.
The function now returns the remaining slice as well.
- All CDS specific functionality was moved into the `cds` submodule of the `time`
module. `CdsShortTimeProvider` was renamed to `TimeProvider`.
## Added
@ -24,8 +26,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- `serde` `Serialize` and `Deserialize` added to all types.
- Added `const` constructors for `PacketId`, `PacketSeqCtrl` and
`SpHeader`.
- Added `PartialEq` and `Eq` `derive`s to `CdsShortTimeProvider`.
- Added `PartialEq` and `Eq` `derive`s to `TimeProvider`.
- `SpHeader`: Added serialization function into raw format `write_to_be_bytes`.
- Added 24-bit day field support for CDS short. The bit width is configured at type level
via a generic parameter type passed to the `cds::TimeProvider`
- Added submillisecond precision support for `cds::TimeProvider`
# [v0.3.1] 03.12.2022

View File

@ -66,6 +66,10 @@ pub mod tc;
pub mod time;
pub mod tm;
mod private {
pub trait Sealed {}
}
pub const MAX_APID: u16 = 2u16.pow(11) - 1;
pub const MAX_SEQ_COUNT: u16 = 2u16.pow(14) - 1;

View File

@ -219,13 +219,40 @@ pub trait CcsdsTimeProvider {
fn date_time(&self) -> Option<DateTime<Utc>>;
}
/// Module to generate or read CDS timestamps as specified in
/// [CCSDS 301.0-B-4](https://public.ccsds.org/Pubs/301x0b4e1.pdf) section 3.3 .
///
/// The core data structure to do this is the [cds::CdsTimeProvider] struct.
pub mod cds {
use super::*;
use crate::private::Sealed;
use core::fmt::Debug;
const CDS_SHORT_P_FIELD: u8 = (CcsdsTimeCodes::Cds as u8) << 4;
pub const MIN_CDS_FIELD_LEN: usize = 7;
/// Generic trait implemented by token structs to specify the length of day field at type
/// level. This trait is only meant to be implemented in this crate and therefore sealed.
pub trait ProvidesDaysLength: Sealed {
type FieldType: Copy + Clone + TryFrom<i32>;
}
/// Type level token to be used as a generic parameter to [CdsTimeProvider].
#[derive(Debug, PartialEq, Eq)]
pub struct DaysLen16Bits {}
impl Sealed for DaysLen16Bits {}
impl ProvidesDaysLength for DaysLen16Bits {
type FieldType = u16;
}
/// Type level token to be used as a generic parameter to [CdsTimeProvider].
pub struct DaysLen24Bits {}
impl Sealed for DaysLen24Bits {}
impl ProvidesDaysLength for DaysLen24Bits {
type FieldType = u32;
}
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum LengthOfDaySegment {
@ -261,7 +288,8 @@ pub mod cds {
/// This object is the abstraction for the CCSDS Day Segmented Time Code (CDS).
///
/// It has the capability to generate and read timestamps as specified in the CCSDS 301.0-B-4
/// section 3.3
/// section 3.3 . The width of the days field is configured at compile time via the generic
/// [ProvidesDaysLength] trait which is implemented by [DaysLen16Bits] and [DaysLen24Bits].
///
/// Custom epochs are not supported yet.
/// Furthermore, the preamble field (p-field) is explicitly conveyed.
@ -271,19 +299,28 @@ pub mod cds {
/// # Example
///
/// ```
/// use spacepackets::time::cds::CdsTimeProvider;
/// use spacepackets::time::{TimeWriter, CcsdsTimeCodes};
/// let timestamp_now = CdsTimeProvider::from_now_u16_days().unwrap();
/// use spacepackets::time::cds::{TimeProvider, DaysLen16Bits};
/// use spacepackets::time::{TimeWriter, CcsdsTimeCodes, TimeReader, CcsdsTimeProvider};
///
/// let timestamp_now = TimeProvider::from_now_with_u16_days().unwrap();
/// let mut raw_stamp = [0; 7];
/// let written = timestamp_now.write_to_bytes(&mut raw_stamp).unwrap();
/// assert_eq!((raw_stamp[0] >> 4) & 0b111, CcsdsTimeCodes::Cds as u8);
/// assert_eq!(written, 7);
/// {
/// let written = timestamp_now.write_to_bytes(&mut raw_stamp).unwrap();
/// assert_eq!((raw_stamp[0] >> 4) & 0b111, CcsdsTimeCodes::Cds as u8);
/// assert_eq!(written, 7);
/// }
/// {
/// let read_result = TimeProvider::<DaysLen16Bits>::from_bytes(&raw_stamp);
/// assert!(read_result.is_ok());
/// let stamp_deserialized = read_result.unwrap();
/// assert_eq!(stamp_deserialized.len_as_bytes(), 7);
/// }
/// ```
#[derive(Debug, Copy, Clone, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct CdsTimeProvider<DaysWidth = u16> {
pub struct TimeProvider<DaysLen: ProvidesDaysLength = DaysLen16Bits> {
pfield: u8,
ccsds_days: DaysWidth,
ccsds_days: DaysLen::FieldType,
ms_of_day: u32,
submillis_precision: Option<SubmillisPrecision>,
unix_seconds: i64,
@ -341,7 +378,7 @@ pub mod cds {
}
}
impl<DaysWidth: Copy + Clone + TryFrom<i32>> CdsTimeProvider<DaysWidth> {
impl<ProvidesDaysLen: ProvidesDaysLength> TimeProvider<ProvidesDaysLen> {
pub fn set_submillis_precision(&mut self, prec: SubmillisPrecision) {
self.pfield &= !(0b11);
if let SubmillisPrecision::Absent = prec {
@ -365,7 +402,7 @@ pub mod cds {
self.submillis_precision = None;
}
pub fn ccsds_days(&self) -> DaysWidth {
pub fn ccsds_days(&self) -> ProvidesDaysLen::FieldType {
self.ccsds_days
}
@ -445,9 +482,8 @@ pub mod cds {
init_len
}
fn setup(mut self, unix_days_seconds: i64, ms_of_day: u64) -> Self {
fn setup(&mut self, unix_days_seconds: i64, ms_of_day: u64) {
self.calc_unix_seconds(unix_days_seconds, ms_of_day);
self
}
fn calc_unix_seconds(&mut self, unix_days_seconds: i64, ms_of_day: u64) {
@ -485,13 +521,13 @@ pub mod cds {
fn generic_new(
days_len: LengthOfDaySegment,
ccsds_days: DaysWidth,
ccsds_days: ProvidesDaysLen::FieldType,
ms_of_day: u32,
) -> Result<Self, CdsError>
where
i64: From<DaysWidth>,
i64: From<ProvidesDaysLen::FieldType>,
{
let provider = Self {
let mut provider = Self {
pfield: Self::generate_p_field(days_len, None),
ccsds_days,
ms_of_day,
@ -500,7 +536,8 @@ pub mod cds {
};
let unix_days_seconds =
ccsds_to_unix_days(ccsds_days.into()) as i64 * SECONDS_PER_DAY as i64;
Ok(provider.setup(unix_days_seconds as i64, ms_of_day.into()))
provider.setup(unix_days_seconds as i64, ms_of_day.into());
Ok(provider)
}
#[cfg(feature = "std")]
@ -509,25 +546,26 @@ pub mod cds {
conversion_from_now: ConversionFromNow,
) -> Result<Self, StdTimestampError>
where
<DaysWidth as TryFrom<i32>>::Error: Debug,
<ProvidesDaysLen::FieldType as TryFrom<i32>>::Error: Debug,
{
let ccsds_days: DaysWidth =
let ccsds_days: ProvidesDaysLen::FieldType =
conversion_from_now.ccsds_days.try_into().map_err(|_| {
StdTimestampError::TimestampError(
CdsError::InvalidCcsdsDays(conversion_from_now.ccsds_days.into()).into(),
)
})?;
let provider = Self {
let mut provider = Self {
pfield: Self::generate_p_field(days_len, conversion_from_now.submillis_prec),
ccsds_days,
ms_of_day: conversion_from_now.ms_of_day as u32,
unix_seconds: 0,
submillis_precision: conversion_from_now.submillis_prec,
};
Ok(provider.setup(
provider.setup(
conversion_from_now.unix_days_seconds as i64,
conversion_from_now.ms_of_day,
))
);
Ok(provider)
}
#[cfg(feature = "std")]
@ -566,10 +604,10 @@ pub mod cds {
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
pub fn update_from_now(&mut self) -> Result<(), StdTimestampError>
where
<DaysWidth as TryFrom<i32>>::Error: Debug,
<ProvidesDaysLen::FieldType as TryFrom<i32>>::Error: Debug,
{
let conversion_from_now = self.generic_conversion_from_now()?;
let ccsds_days: DaysWidth =
let ccsds_days: ProvidesDaysLen::FieldType =
conversion_from_now.ccsds_days.try_into().map_err(|_| {
StdTimestampError::TimestampError(
CdsError::InvalidCcsdsDays(conversion_from_now.ccsds_days as i64).into(),
@ -585,7 +623,7 @@ pub mod cds {
}
}
impl CdsTimeProvider<u32> {
impl TimeProvider<DaysLen24Bits> {
/// Generate a new timestamp provider with the days field width set to 24 bits
pub fn new_with_u24_days(ccsds_days: u32, ms_of_day: u32) -> Result<Self, CdsError> {
if ccsds_days > 2_u32.pow(24) {
@ -617,89 +655,8 @@ pub mod cds {
let conversion_from_now = ConversionFromNow::new_with_submillis_ps_prec()?;
Self::generic_from_now(LengthOfDaySegment::Long24Bits, conversion_from_now)
}
}
impl CdsTimeProvider<u16> {
/// Generate a new timestamp provider with the days field width set to 16 bits
pub fn new_with_u16_days(ccsds_days: u16, ms_of_day: u32) -> Self {
// This should never fail, type system ensures CCSDS can not be negative or too large
Self::generic_new(LengthOfDaySegment::Short16Bits, ccsds_days, ms_of_day).unwrap()
}
/// Generate a time stamp from the current time using the system clock.
#[cfg(feature = "std")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
pub fn from_now_u16_days() -> Result<Self, StdTimestampError> {
let conversion_from_now = ConversionFromNow::new()?;
Self::generic_from_now(LengthOfDaySegment::Short16Bits, conversion_from_now)
}
/// Like [Self::from_now_with_u16_days] but with microsecond sub-millisecond precision.
#[cfg(feature = "std")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
pub fn from_now_with_u16_days_and_us_prec() -> Result<Self, StdTimestampError> {
let conversion_from_now = ConversionFromNow::new_with_submillis_us_prec()?;
Self::generic_from_now(LengthOfDaySegment::Short16Bits, conversion_from_now)
}
/// Like [Self::from_now_with_u16_days] but with picosecond sub-millisecond precision.
#[cfg(feature = "std")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
pub fn from_now_with_u16_days_and_ps_prec() -> Result<Self, StdTimestampError> {
let conversion_from_now = ConversionFromNow::new_with_submillis_ps_prec()?;
Self::generic_from_now(LengthOfDaySegment::Short16Bits, conversion_from_now)
}
}
impl<DaysWidth: Copy + Clone + TryFrom<i32>> CcsdsTimeProvider for CdsTimeProvider<DaysWidth> {
fn len_as_bytes(&self) -> usize {
Self::calc_stamp_len(self.pfield)
}
fn p_field(&self) -> (usize, [u8; 2]) {
(1, [self.pfield, 0])
}
fn ccdsd_time_code(&self) -> CcsdsTimeCodes {
CcsdsTimeCodes::Cds
}
fn unix_seconds(&self) -> i64 {
self.unix_seconds
}
fn date_time(&self) -> Option<DateTime<Utc>> {
self.calc_date_time((self.ms_of_day % 1000) as u32)
}
}
impl TimeReader for CdsTimeProvider<u16> {
fn from_bytes(buf: &[u8]) -> Result<Self, TimestampError> {
let submillis_precision =
Self::generic_raw_read_checks(buf, LengthOfDaySegment::Short16Bits)?;
let ccsds_days: u16 = u16::from_be_bytes(buf[1..3].try_into().unwrap());
let ms_of_day: u32 = u32::from_be_bytes(buf[3..7].try_into().unwrap());
let mut provider = Self::new_with_u16_days(ccsds_days, ms_of_day);
provider.pfield = buf[0];
match submillis_precision {
SubmillisPrecision::Microseconds(_) => {
provider.set_submillis_precision(SubmillisPrecision::Microseconds(
u16::from_be_bytes(buf[7..9].try_into().unwrap()),
))
}
SubmillisPrecision::Picoseconds(_) => {
provider.set_submillis_precision(SubmillisPrecision::Picoseconds(
u32::from_be_bytes(buf[7..11].try_into().unwrap()),
))
}
_ => (),
}
Ok(provider)
}
}
impl TimeReader for CdsTimeProvider<u32> {
fn from_bytes(buf: &[u8]) -> Result<Self, TimestampError> {
fn from_bytes_24_bit_days(buf: &[u8]) -> Result<Self, TimestampError> {
let submillis_precision =
Self::generic_raw_read_checks(buf, LengthOfDaySegment::Long24Bits)?;
let mut temp_buf: [u8; 4] = [0; 4];
@ -724,7 +681,96 @@ pub mod cds {
}
}
impl TimeWriter for CdsTimeProvider<u16> {
impl TimeProvider<DaysLen16Bits> {
/// Generate a new timestamp provider with the days field width set to 16 bits
pub fn new_with_u16_days(ccsds_days: u16, ms_of_day: u32) -> Self {
// This should never fail, type system ensures CCSDS can not be negative or too large
Self::generic_new(LengthOfDaySegment::Short16Bits, ccsds_days, ms_of_day).unwrap()
}
/// Generate a time stamp from the current time using the system clock.
#[cfg(feature = "std")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
pub fn from_now_with_u16_days() -> Result<Self, StdTimestampError> {
let conversion_from_now = ConversionFromNow::new()?;
Self::generic_from_now(LengthOfDaySegment::Short16Bits, conversion_from_now)
}
/// Like [Self::from_now_with_u16_days] but with microsecond sub-millisecond precision.
#[cfg(feature = "std")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
pub fn from_now_with_u16_days_and_us_prec() -> Result<Self, StdTimestampError> {
let conversion_from_now = ConversionFromNow::new_with_submillis_us_prec()?;
Self::generic_from_now(LengthOfDaySegment::Short16Bits, conversion_from_now)
}
/// Like [Self::from_now_with_u16_days] but with picosecond sub-millisecond precision.
#[cfg(feature = "std")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
pub fn from_now_with_u16_days_and_ps_prec() -> Result<Self, StdTimestampError> {
let conversion_from_now = ConversionFromNow::new_with_submillis_ps_prec()?;
Self::generic_from_now(LengthOfDaySegment::Short16Bits, conversion_from_now)
}
fn from_bytes_16_bit_days(buf: &[u8]) -> Result<Self, TimestampError> {
let submillis_precision =
Self::generic_raw_read_checks(buf, LengthOfDaySegment::Short16Bits)?;
let ccsds_days: u16 = u16::from_be_bytes(buf[1..3].try_into().unwrap());
let ms_of_day: u32 = u32::from_be_bytes(buf[3..7].try_into().unwrap());
let mut provider = Self::new_with_u16_days(ccsds_days, ms_of_day);
provider.pfield = buf[0];
match submillis_precision {
SubmillisPrecision::Microseconds(_) => {
provider.set_submillis_precision(SubmillisPrecision::Microseconds(
u16::from_be_bytes(buf[7..9].try_into().unwrap()),
))
}
SubmillisPrecision::Picoseconds(_) => {
provider.set_submillis_precision(SubmillisPrecision::Picoseconds(
u32::from_be_bytes(buf[7..11].try_into().unwrap()),
))
}
_ => (),
}
Ok(provider)
}
}
impl<ProvidesDaysLen: ProvidesDaysLength> CcsdsTimeProvider for TimeProvider<ProvidesDaysLen> {
fn len_as_bytes(&self) -> usize {
Self::calc_stamp_len(self.pfield)
}
fn p_field(&self) -> (usize, [u8; 2]) {
(1, [self.pfield, 0])
}
fn ccdsd_time_code(&self) -> CcsdsTimeCodes {
CcsdsTimeCodes::Cds
}
fn unix_seconds(&self) -> i64 {
self.unix_seconds
}
fn date_time(&self) -> Option<DateTime<Utc>> {
self.calc_date_time((self.ms_of_day % 1000) as u32)
}
}
impl TimeReader for TimeProvider<DaysLen16Bits> {
fn from_bytes(buf: &[u8]) -> Result<Self, TimestampError> {
Self::from_bytes_16_bit_days(buf)
}
}
impl TimeReader for TimeProvider<DaysLen24Bits> {
fn from_bytes(buf: &[u8]) -> Result<Self, TimestampError> {
Self::from_bytes_24_bit_days(buf)
}
}
impl TimeWriter for TimeProvider<DaysLen16Bits> {
fn write_to_bytes(&self, buf: &mut [u8]) -> Result<usize, TimestampError> {
self.length_check(buf, self.len_as_bytes())?;
buf[0] = self.pfield;
@ -745,7 +791,7 @@ pub mod cds {
}
}
impl TimeWriter for CdsTimeProvider<u32> {
impl TimeWriter for TimeProvider<DaysLen24Bits> {
fn write_to_bytes(&self, buf: &mut [u8]) -> Result<usize, TimestampError> {
self.length_check(buf, self.len_as_bytes())?;
buf[0] = self.pfield;
@ -837,9 +883,9 @@ pub mod ascii {
#[cfg(all(test, feature = "std"))]
mod tests {
use super::cds::CdsTimeProvider;
use super::cds::TimeProvider;
use super::*;
use crate::time::cds::SubmillisPrecision;
use crate::time::cds::{DaysLen16Bits, DaysLen24Bits, SubmillisPrecision};
use crate::time::TimestampError::{ByteConversionError, InvalidTimeCode};
use crate::ByteConversionError::{FromSliceTooSmall, ToSliceTooSmall};
use alloc::format;
@ -917,7 +963,7 @@ mod tests {
#[test]
fn test_time_stamp_zero_args() {
let time_stamper = CdsTimeProvider::new_with_u16_days(0, 0);
let time_stamper = TimeProvider::new_with_u16_days(0, 0);
assert_eq!(
time_stamper.unix_seconds(),
(DAYS_CCSDS_TO_UNIX * SECONDS_PER_DAY as i32) as i64
@ -939,7 +985,7 @@ mod tests {
#[test]
fn test_time_stamp_unix_epoch() {
let time_stamper = CdsTimeProvider::new_with_u16_days((-DAYS_CCSDS_TO_UNIX) as u16, 0);
let time_stamper = TimeProvider::new_with_u16_days((-DAYS_CCSDS_TO_UNIX) as u16, 0);
assert_eq!(time_stamper.unix_seconds(), 0);
assert_eq!(time_stamper.submillis_precision(), None);
let date_time = time_stamper.date_time().unwrap();
@ -953,7 +999,7 @@ mod tests {
#[test]
fn test_large_days_field_write() {
let time_stamper = CdsTimeProvider::new_with_u24_days(0x108020, 0);
let time_stamper = TimeProvider::new_with_u24_days(0x108020, 0);
assert!(time_stamper.is_ok());
let time_stamper = time_stamper.unwrap();
assert_eq!(time_stamper.len_as_bytes(), 8);
@ -972,13 +1018,13 @@ mod tests {
#[test]
fn test_large_days_field_read() {
let time_stamper = CdsTimeProvider::new_with_u24_days(0x108020, 0);
let time_stamper = TimeProvider::new_with_u24_days(0x108020, 0);
assert!(time_stamper.is_ok());
let time_stamper = time_stamper.unwrap();
let mut buf = [0; 16];
let written = time_stamper.write_to_bytes(&mut buf);
assert!(written.is_ok());
let provider = CdsTimeProvider::<u32>::from_bytes(&buf);
let provider = TimeProvider::<DaysLen24Bits>::from_bytes(&buf);
assert!(provider.is_ok());
let provider = provider.unwrap();
assert_eq!(provider.ccsds_days(), 0x108020);
@ -987,13 +1033,13 @@ mod tests {
#[test]
fn test_large_days_field_read_invalid_ctor() {
let time_stamper = CdsTimeProvider::new_with_u24_days(0x108020, 0);
let time_stamper = TimeProvider::new_with_u24_days(0x108020, 0);
assert!(time_stamper.is_ok());
let time_stamper = time_stamper.unwrap();
let mut buf = [0; 16];
let written = time_stamper.write_to_bytes(&mut buf);
assert!(written.is_ok());
let faulty_ctor = CdsTimeProvider::<u16>::from_bytes(&buf);
let faulty_ctor = TimeProvider::<DaysLen16Bits>::from_bytes(&buf);
assert!(faulty_ctor.is_err());
let error = faulty_ctor.unwrap_err();
if let TimestampError::CdsError(CdsError::InvalidCtorForDaysOfLenInPreamble(len_of_day)) =
@ -1008,7 +1054,7 @@ mod tests {
#[test]
fn test_write() {
let mut buf = [0; 16];
let time_stamper_0 = CdsTimeProvider::new_with_u16_days(0, 0);
let time_stamper_0 = TimeProvider::new_with_u16_days(0, 0);
let mut res = time_stamper_0.write_to_bytes(&mut buf);
assert!(res.is_ok());
assert_eq!(buf[0], (CcsdsTimeCodes::Cds as u8) << 4);
@ -1020,7 +1066,7 @@ mod tests {
u32::from_be_bytes(buf[3..7].try_into().expect("Byte conversion failed")),
0
);
let time_stamper_1 = CdsTimeProvider::new_with_u16_days(u16::MAX - 1, u32::MAX - 1);
let time_stamper_1 = TimeProvider::new_with_u16_days(u16::MAX - 1, u32::MAX - 1);
res = time_stamper_1.write_to_bytes(&mut buf);
assert!(res.is_ok());
assert_eq!(buf[0], (CcsdsTimeCodes::Cds as u8) << 4);
@ -1037,7 +1083,7 @@ mod tests {
#[test]
fn test_faulty_write_buf_too_small() {
let mut buf = [0; 7];
let time_stamper = CdsTimeProvider::new_with_u16_days(u16::MAX - 1, u32::MAX - 1);
let time_stamper = TimeProvider::new_with_u16_days(u16::MAX - 1, u32::MAX - 1);
for i in 0..6 {
let res = time_stamper.write_to_bytes(&mut buf[0..i]);
assert!(res.is_err());
@ -1058,7 +1104,7 @@ mod tests {
fn test_faulty_read_buf_too_small() {
let buf = [0; 7];
for i in 0..6 {
let res = CdsTimeProvider::<u16>::from_bytes(&buf[0..i]);
let res = TimeProvider::<DaysLen16Bits>::from_bytes(&buf[0..i]);
assert!(res.is_err());
let err = res.unwrap_err();
match err {
@ -1079,11 +1125,11 @@ mod tests {
#[test]
fn test_faulty_invalid_pfield() {
let mut buf = [0; 16];
let time_stamper_0 = CdsTimeProvider::new_with_u16_days(0, 0);
let time_stamper_0 = TimeProvider::new_with_u16_days(0, 0);
let res = time_stamper_0.write_to_bytes(&mut buf);
assert!(res.is_ok());
buf[0] = 0;
let res = CdsTimeProvider::<u16>::from_bytes(&buf);
let res = TimeProvider::<DaysLen16Bits>::from_bytes(&buf);
assert!(res.is_err());
let err = res.unwrap_err();
match err {
@ -1098,7 +1144,7 @@ mod tests {
#[test]
fn test_reading() {
let mut buf = [0; 16];
let time_stamper = CdsTimeProvider::new_with_u16_days(u16::MAX - 1, u32::MAX - 1);
let time_stamper = TimeProvider::new_with_u16_days(u16::MAX - 1, u32::MAX - 1);
let res = time_stamper.write_to_bytes(&mut buf);
assert!(res.is_ok());
assert_eq!(buf[0], (CcsdsTimeCodes::Cds as u8) << 4);
@ -1111,15 +1157,15 @@ mod tests {
u32::MAX - 1
);
let read_stamp: CdsTimeProvider<u16> =
CdsTimeProvider::from_bytes(&buf).expect("Reading timestamp failed");
let read_stamp: TimeProvider<DaysLen16Bits> =
TimeProvider::from_bytes(&buf).expect("Reading timestamp failed");
assert_eq!(read_stamp.ccsds_days(), u16::MAX - 1);
assert_eq!(read_stamp.ms_of_day(), u32::MAX - 1);
}
#[test]
fn test_time_now() {
let timestamp_now = CdsTimeProvider::from_now_u16_days().unwrap();
let timestamp_now = TimeProvider::from_now_with_u16_days().unwrap();
let compare_stamp = Utc::now();
let dt = timestamp_now.date_time().unwrap();
if compare_stamp.year() > dt.year() {
@ -1144,7 +1190,7 @@ mod tests {
#[test]
fn test_submillis_precision_micros() {
let mut time_stamper = CdsTimeProvider::new_with_u16_days(0, 0);
let mut time_stamper = TimeProvider::new_with_u16_days(0, 0);
time_stamper.set_submillis_precision(SubmillisPrecision::Microseconds(500));
assert!(time_stamper.submillis_precision().is_some());
if let SubmillisPrecision::Microseconds(micros) =
@ -1165,7 +1211,7 @@ mod tests {
#[test]
fn test_submillis_precision_picos() {
let mut time_stamper = CdsTimeProvider::new_with_u16_days(0, 0);
let mut time_stamper = TimeProvider::new_with_u16_days(0, 0);
time_stamper.set_submillis_precision(SubmillisPrecision::Picoseconds(5e8 as u32));
assert!(time_stamper.submillis_precision().is_some());
if let SubmillisPrecision::Picoseconds(ps) = time_stamper.submillis_precision().unwrap() {
@ -1184,14 +1230,14 @@ mod tests {
#[test]
fn read_stamp_with_ps_submillis_precision() {
let mut time_stamper = CdsTimeProvider::new_with_u16_days(0, 0);
let mut time_stamper = TimeProvider::new_with_u16_days(0, 0);
time_stamper.set_submillis_precision(SubmillisPrecision::Picoseconds(5e8 as u32));
let mut write_buf: [u8; 16] = [0; 16];
let written = time_stamper
.write_to_bytes(&mut write_buf)
.expect("Writing timestamp failed");
assert_eq!(written, 11);
let stamp_deserialized = CdsTimeProvider::<u16>::from_bytes(&write_buf);
let stamp_deserialized = TimeProvider::<DaysLen16Bits>::from_bytes(&write_buf);
assert!(stamp_deserialized.is_ok());
let stamp_deserialized = stamp_deserialized.unwrap();
assert_eq!(stamp_deserialized.len_as_bytes(), 11);
@ -1206,14 +1252,14 @@ mod tests {
#[test]
fn read_stamp_with_us_submillis_precision() {
let mut time_stamper = CdsTimeProvider::new_with_u16_days(0, 0);
let mut time_stamper = TimeProvider::new_with_u16_days(0, 0);
time_stamper.set_submillis_precision(SubmillisPrecision::Microseconds(500));
let mut write_buf: [u8; 16] = [0; 16];
let written = time_stamper
.write_to_bytes(&mut write_buf)
.expect("Writing timestamp failed");
assert_eq!(written, 9);
let stamp_deserialized = CdsTimeProvider::<u16>::from_bytes(&write_buf);
let stamp_deserialized = TimeProvider::<DaysLen16Bits>::from_bytes(&write_buf);
assert!(stamp_deserialized.is_ok());
let stamp_deserialized = stamp_deserialized.unwrap();
assert_eq!(stamp_deserialized.len_as_bytes(), 9);
@ -1229,10 +1275,10 @@ mod tests {
#[test]
#[cfg(feature = "serde")]
fn test_serialization() {
let stamp_now = CdsTimeProvider::from_now_u16_days().expect("Error retrieving time");
let stamp_now = TimeProvider::from_now_with_u16_days().expect("Error retrieving time");
let val = to_allocvec(&stamp_now).expect("Serializing timestamp failed");
assert!(val.len() > 0);
let stamp_deser: CdsTimeProvider = from_bytes(&val).expect("Stamp deserialization failed");
let stamp_deser: TimeProvider = from_bytes(&val).expect("Stamp deserialization failed");
assert_eq!(stamp_deser, stamp_now);
}