-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathspacepacket.rs
More file actions
36 lines (34 loc) · 1.33 KB
/
spacepacket.rs
File metadata and controls
36 lines (34 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Implementation of the CCSDS spacepacket with an optional secondary header
use crate::PrimaryHeader;
use crate::types;
use deku::prelude::*;
use std::marker::PhantomData;
#[derive(Eq, Debug, PartialEq, Clone)]
pub struct SpacePacket<'a,S: deku::DekuContainerRead<'a> + deku::DekuWrite> {
pub primary_header: PrimaryHeader,
pub secondary_header: Option<S>,
pub payload: Vec<u8>,
phantom: PhantomData<&'a S>,
}
impl <'a, S: deku::DekuContainerRead<'a> + deku::DekuWrite> SpacePacket<'a,S> {
pub fn parse(raw: &'a Vec<u8>) -> Self {
let (rest,primary_header) = PrimaryHeader::from_bytes((raw,0)).expect("failed to parse primary header");
match primary_header.sec_header_flag {
types::SecondaryHeaderFlag::Present => {
let (rest,secondary_header) = S::from_bytes((rest.0,0)).expect("failed to parse secondary header");
Self {
primary_header,
secondary_header: Some(secondary_header),
payload: rest.0.to_vec(),
phantom: PhantomData::<&S>,
}
}
_ => Self {
primary_header,
secondary_header: None,
payload: rest.0.to_vec(),
phantom: PhantomData::<&S>,
}
}
}
}