fix tests
This commit is contained in:
parent
9d62d66987
commit
fd51a04e6a
@ -660,6 +660,37 @@ impl<'raw_data> PusTmCreator<'raw_data> {
|
|||||||
self.update_ccsds_data_len();
|
self.update_ccsds_data_len();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Write the raw PUS byte representation to a provided buffer.
|
||||||
|
pub fn write_to_bytes(&self, slice: &mut [u8]) -> Result<usize, ByteConversionError> {
|
||||||
|
let mut curr_idx = 0;
|
||||||
|
let total_size = self.len_written();
|
||||||
|
if total_size > slice.len() {
|
||||||
|
return Err(ByteConversionError::ToSliceTooSmall {
|
||||||
|
found: slice.len(),
|
||||||
|
expected: total_size,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
self.sp_header
|
||||||
|
.write_to_be_bytes(&mut slice[0..CCSDS_HEADER_LEN])?;
|
||||||
|
curr_idx += CCSDS_HEADER_LEN;
|
||||||
|
let sec_header_len = size_of::<zc::PusTmSecHeaderWithoutTimestamp>();
|
||||||
|
let sec_header = zc::PusTmSecHeaderWithoutTimestamp::try_from(self.sec_header).unwrap();
|
||||||
|
sec_header
|
||||||
|
.write_to_bytes(&mut slice[curr_idx..curr_idx + sec_header_len])
|
||||||
|
.ok_or(ByteConversionError::ZeroCopyToError)?;
|
||||||
|
curr_idx += sec_header_len;
|
||||||
|
slice[curr_idx..curr_idx + self.sec_header.timestamp.len()]
|
||||||
|
.copy_from_slice(self.sec_header.timestamp);
|
||||||
|
curr_idx += self.sec_header.timestamp.len();
|
||||||
|
slice[curr_idx..curr_idx + self.source_data.len()].copy_from_slice(self.source_data);
|
||||||
|
curr_idx += self.source_data.len();
|
||||||
|
let mut digest = CRC_CCITT_FALSE.digest();
|
||||||
|
digest.update(&slice[0..curr_idx]);
|
||||||
|
slice[curr_idx..curr_idx + 2].copy_from_slice(&digest.finalize().to_be_bytes());
|
||||||
|
curr_idx += 2;
|
||||||
|
Ok(curr_idx)
|
||||||
|
}
|
||||||
|
|
||||||
/// Append the raw PUS byte representation to a provided [alloc::vec::Vec]
|
/// Append the raw PUS byte representation to a provided [alloc::vec::Vec]
|
||||||
#[cfg(feature = "alloc")]
|
#[cfg(feature = "alloc")]
|
||||||
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
|
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
|
||||||
@ -689,34 +720,7 @@ impl WritablePusPacket for PusTmCreator<'_> {
|
|||||||
}
|
}
|
||||||
/// Write the raw PUS byte representation to a provided buffer.
|
/// Write the raw PUS byte representation to a provided buffer.
|
||||||
fn write_to_bytes(&self, slice: &mut [u8]) -> Result<usize, PusError> {
|
fn write_to_bytes(&self, slice: &mut [u8]) -> Result<usize, PusError> {
|
||||||
let mut curr_idx = 0;
|
Ok(Self::write_to_bytes(self, slice)?)
|
||||||
let total_size = self.len_written();
|
|
||||||
if total_size > slice.len() {
|
|
||||||
return Err(ByteConversionError::ToSliceTooSmall {
|
|
||||||
found: slice.len(),
|
|
||||||
expected: total_size,
|
|
||||||
}
|
|
||||||
.into());
|
|
||||||
}
|
|
||||||
self.sp_header
|
|
||||||
.write_to_be_bytes(&mut slice[0..CCSDS_HEADER_LEN])?;
|
|
||||||
curr_idx += CCSDS_HEADER_LEN;
|
|
||||||
let sec_header_len = size_of::<zc::PusTmSecHeaderWithoutTimestamp>();
|
|
||||||
let sec_header = zc::PusTmSecHeaderWithoutTimestamp::try_from(self.sec_header).unwrap();
|
|
||||||
sec_header
|
|
||||||
.write_to_bytes(&mut slice[curr_idx..curr_idx + sec_header_len])
|
|
||||||
.ok_or(ByteConversionError::ZeroCopyToError)?;
|
|
||||||
curr_idx += sec_header_len;
|
|
||||||
slice[curr_idx..curr_idx + self.sec_header.timestamp.len()]
|
|
||||||
.copy_from_slice(self.sec_header.timestamp);
|
|
||||||
curr_idx += self.sec_header.timestamp.len();
|
|
||||||
slice[curr_idx..curr_idx + self.source_data.len()].copy_from_slice(self.source_data);
|
|
||||||
curr_idx += self.source_data.len();
|
|
||||||
let mut digest = CRC_CCITT_FALSE.digest();
|
|
||||||
digest.update(&slice[0..curr_idx]);
|
|
||||||
slice[curr_idx..curr_idx + 2].copy_from_slice(&digest.finalize().to_be_bytes());
|
|
||||||
curr_idx += 2;
|
|
||||||
Ok(curr_idx)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1263,20 +1267,13 @@ mod tests {
|
|||||||
let res = pus_tm.write_to_bytes(&mut buf);
|
let res = pus_tm.write_to_bytes(&mut buf);
|
||||||
assert!(res.is_err());
|
assert!(res.is_err());
|
||||||
let error = res.unwrap_err();
|
let error = res.unwrap_err();
|
||||||
assert!(matches!(error, PusError::ByteConversion { .. }));
|
if let ByteConversionError::ToSliceTooSmall { found, expected } = error {
|
||||||
match error {
|
|
||||||
PusError::ByteConversion(err) => match err {
|
|
||||||
ByteConversionError::ToSliceTooSmall { found, expected } => {
|
|
||||||
assert_eq!(expected, 22);
|
assert_eq!(expected, 22);
|
||||||
assert_eq!(found, 16);
|
assert_eq!(found, 16);
|
||||||
}
|
} else {
|
||||||
_ => panic!("Invalid PUS error {:?}", err),
|
|
||||||
},
|
|
||||||
_ => {
|
|
||||||
panic!("Invalid error {:?}", error);
|
panic!("Invalid error {:?}", error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(feature = "alloc")]
|
#[cfg(feature = "alloc")]
|
||||||
|
@ -1183,10 +1183,7 @@ impl TryFrom<chrono::DateTime<chrono::Utc>> for CdsTime<DaysLen16Bits> {
|
|||||||
|
|
||||||
fn try_from(dt: chrono::DateTime<chrono::Utc>) -> Result<Self, Self::Error> {
|
fn try_from(dt: chrono::DateTime<chrono::Utc>) -> Result<Self, Self::Error> {
|
||||||
let conversion = ConversionFromChronoDatetime::new(&dt)?;
|
let conversion = ConversionFromChronoDatetime::new(&dt)?;
|
||||||
Self::generic_from_conversion(
|
Self::generic_from_conversion(LengthOfDaySegment::Short16Bits, conversion)
|
||||||
LengthOfDaySegment::Short16Bits,
|
|
||||||
conversion,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1195,10 +1192,7 @@ impl TryFrom<chrono::DateTime<chrono::Utc>> for CdsTime<DaysLen24Bits> {
|
|||||||
type Error = CdsError;
|
type Error = CdsError;
|
||||||
fn try_from(dt: chrono::DateTime<chrono::Utc>) -> Result<Self, Self::Error> {
|
fn try_from(dt: chrono::DateTime<chrono::Utc>) -> Result<Self, Self::Error> {
|
||||||
let conversion = ConversionFromChronoDatetime::new(&dt)?;
|
let conversion = ConversionFromChronoDatetime::new(&dt)?;
|
||||||
Self::generic_from_conversion(
|
Self::generic_from_conversion(LengthOfDaySegment::Long24Bits, conversion)
|
||||||
LengthOfDaySegment::Long24Bits,
|
|
||||||
conversion,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2265,7 +2259,9 @@ mod tests {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
let time_provider = CdsTime::from_dt_with_u24_days(&datetime_utc);
|
let time_provider = CdsTime::from_dt_with_u24_days(&datetime_utc);
|
||||||
assert!(time_provider.is_err());
|
assert!(time_provider.is_err());
|
||||||
if let CdsError::DateBeforeCcsdsEpoch(DateBeforeCcsdsEpochError(dt)) = time_provider.unwrap_err() {
|
if let CdsError::DateBeforeCcsdsEpoch(DateBeforeCcsdsEpochError(dt)) =
|
||||||
|
time_provider.unwrap_err()
|
||||||
|
{
|
||||||
assert_eq!(dt, datetime_utc.into());
|
assert_eq!(dt, datetime_utc.into());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user