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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use std::{
    mem,
    ops::{Deref, DerefMut},
};

use zerocopy::{ByteSlice, ByteSliceMut, LayoutVerified};

use crate::ccsds::tc::segment::*;

pub struct Builder<B> {
    header: LayoutVerified<B, Header>,
    body: B,
}

impl<B> Builder<B>
where
    B: ByteSlice,
{
    pub fn new(bytes: B) -> Option<Self> {
        let (header, body) = LayoutVerified::new_unaligned_from_prefix(bytes)?;
        Some(Self { header, body })
    }
}

impl<B> Builder<B>
where
    B: ByteSliceMut,
{
    pub fn use_default(&mut self) {
        self.set_map_id(0b10);
        self.set_sequence_flag(SequenceFlag::NoSegmentation);
    }

    pub fn body_mut(&mut self) -> &mut B {
        &mut self.body
    }

    pub fn finish(self, body_len: usize) -> usize {
        mem::size_of::<Header>() + body_len
    }
}

impl<B> Deref for Builder<B>
where
    B: ByteSlice,
{
    type Target = Header;

    fn deref(&self) -> &Self::Target {
        &self.header
    }
}

impl<B> DerefMut for Builder<B>
where
    B: ByteSliceMut,
{
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.header
    }
}