Skip to content

Commit

Permalink
added additional ctors which only set the APID
Browse files Browse the repository at this point in the history
  • Loading branch information
robamu committed Apr 3, 2024
1 parent ca90393 commit 0115461
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 41 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
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].
///
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 0115461

Please sign in to comment.