From da201a91e5837dea68a0afc5ce785ddf206d0cca Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Sun, 3 Dec 2023 13:05:55 +0100 Subject: [PATCH] well this is annoying work --- src/ecss/hk.rs | 30 ++++++++++++++++++++++++++++++ src/ecss/mod.rs | 35 ++++++++++++++++++++++++++++++++--- 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/src/ecss/hk.rs b/src/ecss/hk.rs index 5bf7ce5..5af3547 100644 --- a/src/ecss/hk.rs +++ b/src/ecss/hk.rs @@ -29,3 +29,33 @@ pub enum Subservice { TcGenerateOneShotDiag = 28, TcModifyDiagCollectionInterval = 32, } + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn test_try_from_u8() { + let hk_report_subservice_raw = 25; + let hk_report: Subservice = Subservice::try_from(hk_report_subservice_raw).unwrap(); + assert_eq!(hk_report, Subservice::TmHkPacket); + } + + #[test] + fn test_into_u8() { + let hk_report_raw: u8 = Subservice::TmHkPacket.into(); + assert_eq!(hk_report_raw, 25); + } + + #[test] + fn test_partial_eq() { + let hk_report_raw = Subservice::TmHkPacket; + assert_ne!(hk_report_raw, Subservice::TcGenerateOneShotHk); + assert_eq!(hk_report_raw, Subservice::TmHkPacket); + } + #[test] + fn test_copy_clone() { + let hk_report = Subservice::TmHkPacket; + let hk_report_copy = hk_report; + assert_eq!(hk_report, hk_report_copy); + } +} diff --git a/src/ecss/mod.rs b/src/ecss/mod.rs index ab03582..0242e91 100644 --- a/src/ecss/mod.rs +++ b/src/ecss/mod.rs @@ -74,6 +74,7 @@ pub enum PusServiceId { /// All PUS versions. Only PUS C is supported by this library. #[derive(PartialEq, Eq, Copy, Clone, Debug)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[non_exhaustive] pub enum PusVersion { EsaPus = 0, PusA = 1, @@ -95,8 +96,9 @@ impl TryFrom for PusVersion { } /// ECSS Packet Type Codes (PTC)s. -#[derive(Debug, Copy, Clone, PartialEq, Eq)] +#[derive(Debug, Copy, Clone, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[repr(u8)] pub enum PacketTypeCodes { Boolean = 1, Enumerated = 2, @@ -115,8 +117,9 @@ pub enum PacketTypeCodes { pub type Ptc = PacketTypeCodes; /// ECSS Packet Field Codes (PFC)s for the unsigned [Ptc]. -#[derive(Debug, Copy, Clone, PartialEq, Eq)] +#[derive(Debug, Copy, Clone, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[repr(u8)] pub enum UnsignedPfc { OneByte = 4, TwelveBits = 8, @@ -131,8 +134,9 @@ pub enum UnsignedPfc { } /// ECSS Packet Field Codes (PFC)s for the real (floating point) [Ptc]. -#[derive(Debug, Copy, Clone, PartialEq, Eq)] +#[derive(Debug, Copy, Clone, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[repr(u8)] pub enum RealPfc { /// 4 octets simple precision format (IEEE) Float = 1, @@ -376,9 +380,13 @@ pub trait WritablePusPacket { #[cfg(test)] mod tests { + use alloc::string::ToString; + use crate::ecss::{EcssEnumU16, EcssEnumU32, EcssEnumU8, UnsignedEnum}; use crate::ByteConversionError; + use super::*; + #[test] fn test_enum_u8() { let mut buf = [0, 0, 0]; @@ -448,4 +456,25 @@ mod tests { } } } + + #[test] + fn test_pus_error_display() { + let unsupport_version = PusError::VersionNotSupported(super::PusVersion::EsaPus); + let write_str = unsupport_version.to_string(); + assert_eq!(write_str, "PUS version EsaPus not supported") + } + + #[test] + fn test_service_id_from_u8() { + let verification_id_raw = 1; + let verification_id = PusServiceId::try_from(verification_id_raw).unwrap(); + assert_eq!(verification_id, PusServiceId::Verification); + } + + #[test] + fn test_ptc_from_u8() { + let ptc_raw = Ptc::AbsoluteTime as u8; + let ptc = Ptc::try_from(ptc_raw).unwrap(); + assert_eq!(ptc, Ptc::AbsoluteTime); + } }