From 0115461bb5e6c8e876de1e3b50726218532ba00a Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Wed, 3 Apr 2024 21:30:23 +0200 Subject: [PATCH] added additional ctors which only set the APID --- CHANGELOG.md | 4 ++++ src/lib.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a0c0eae..b52a3d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). to `new_for_tc_checked`, `new_for_tm_checked`, `new_for_unseg_tc_checked` and `new_for_unseg_tm_checked`. +## Added + +- `SpHeader::new_from_apid` and `SpHeader::new_from_apid_checked` constructor. + # [v0.11.0-rc.1] 2024-04-03 Major API changes for the time API. If you are using the time API, it is strongly recommended diff --git a/src/lib.rs b/src/lib.rs index 18acc46..2de734a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -512,6 +512,35 @@ impl SpHeader { } } + /// This constructor sets the sequence flag field to [SequenceFlags::Unsegmented]. The data + /// length field is set to 1, which denotes an empty space packets. + /// + /// This constructor will panic if the APID exceeds [MAX_APID]. + pub const fn new_from_apid(apid: u16) -> Self { + SpHeader { + version: 0, + packet_id: PacketId::new(PacketType::Tm, false, apid), + psc: PacketSequenceCtrl { + seq_flags: SequenceFlags::Unsegmented, + seq_count: 0, + }, + data_len: 1, + } + } + + /// Checked variant of [Self::new_from_apid]. + pub fn new_from_apid_checked(apid: u16) -> Option { + Some(SpHeader { + version: 0, + packet_id: PacketId::new_checked(PacketType::Tm, false, apid)?, + psc: PacketSequenceCtrl { + seq_flags: SequenceFlags::Unsegmented, + seq_count: 0, + }, + data_len: 1, + }) + } + /// This constructor panics if the passed APID exceeds [MAX_APID] or the passed packet sequence /// count exceeds [MAX_SEQ_COUNT]. /// @@ -1180,4 +1209,16 @@ pub(crate) mod tests { let mut id_set = HashSet::new(); id_set.insert(PacketId::from(1_u16)); } + + #[test] + fn sp_header_from_apid() { + let sp_header = SpHeader::new_from_apid(0x03); + assert_eq!(sp_header.apid(), 0x03); + } + + #[test] + fn sp_header_from_apid_checked() { + let sp_header = SpHeader::new_from_apid_checked(0x03).unwrap(); + assert_eq!(sp_header.apid(), 0x03); + } }