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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use super::*;

const FRAME_SIZE: usize = 10;

#[derive(Default)]
pub struct FrameBuilder {
    bytes: [u8; FRAME_SIZE],
}
impl FrameBuilder {
    #[must_use]
    pub fn new() -> Self {
        let mut builder = Self {
            bytes: [0; FRAME_SIZE],
        };

        builder.version(FrameVersion::Standard);

        builder
    }

    #[must_use]
    pub fn build(&self) -> Frame {
        Frame::new(self.bytes().to_vec())
    }
}
impl FrameBuilderTrait for FrameBuilder {
    fn bytes(&self) -> &[u8] {
        &self.bytes
    }

    fn bytes_mut(&mut self) -> &mut [u8] {
        &mut self.bytes
    }
}

pub trait FrameBuilderTrait {
    fn bytes(&self) -> &[u8];
    fn bytes_mut(&mut self) -> &mut [u8];

    fn version(&mut self, version: FrameVersion) {
        self.bytes_mut()[0] = (self.bytes()[0] & !0b0000_0011) | (0b0000_0011 & version.into_u8());
    }

    fn type_(&mut self, type_: FrameType) {
        self.bytes_mut()[0] =
            (self.bytes()[0] & !0b0000_1100) | (0b0000_1100 & (type_.into_u8() << 2));
    }

    fn subtype(&mut self, subtype: FrameSubtype) {
        self.bytes_mut()[0] =
            (self.bytes()[0] & !0b1111_0000) | (0b1111_0000 & (subtype.into_u8() << 4));
    }

    fn to_ds(&mut self, to_ds: bool) {
        self.bytes_mut()[1] =
            (self.bytes()[1] & !0b0000_0001) | (if to_ds { 0b0000_0001 } else { 0b0000_0000 });
    }

    fn from_ds(&mut self, from_ds: bool) {
        self.bytes_mut()[1] =
            (self.bytes()[1] & !0b0000_0010) | (if from_ds { 0b0000_0010 } else { 0b0000_0000 });
    }

    fn ds_status(&mut self, ds_status: DSStatus) {
        let (from_ds, to_ds) = ds_status.to_bools();
        self.from_ds(from_ds);
        self.to_ds(to_ds);
    }

    fn more_fragments(&mut self, more_fragments: bool) {
        self.bytes_mut()[1] = (self.bytes()[1] & !0b0000_0100)
            | (if more_fragments {
                0b0000_0100
            } else {
                0b0000_0000
            });
    }

    fn retry(&mut self, retry: bool) {
        self.bytes_mut()[1] =
            (self.bytes()[1] & !0b0000_1000) | (if retry { 0b0000_1000 } else { 0b0000_0000 });
    }

    fn pwr_mgt(&mut self, pwr_mgt: bool) {
        self.bytes_mut()[1] =
            (self.bytes()[1] & !0b0001_0000) | (if pwr_mgt { 0b0001_0000 } else { 0b0000_0000 });
    }

    fn more_data(&mut self, more_data: bool) {
        self.bytes_mut()[1] =
            (self.bytes()[1] & !0b0010_0000) | (if more_data { 0b0010_0000 } else { 0b0000_0000 });
    }

    fn protected(&mut self, protected: bool) {
        self.bytes_mut()[1] =
            (self.bytes()[1] & !0b0100_0000) | (if protected { 0b0100_0000 } else { 0b0000_0000 });
    }

    fn order(&mut self, order: bool) {
        self.bytes_mut()[1] =
            (self.bytes()[1] & !0b1000_0000) | (if order { 0b1000_0000 } else { 0b0000_0000 });
    }

    fn duration_or_id(&mut self, _duration_or_id: DurationID) {
        unimplemented!()
    }

    fn addr1(&mut self, mac_address: MacAddress) {
        self.bytes_mut()[4..10].copy_from_slice(mac_address.as_bytes());
    }

    /// Receiver Address
    /// Who this packet is destined for wirelessly.
    /// Address 1
    fn receiver_address(&mut self, mac_address: MacAddress) {
        self.addr1(mac_address)
    }

    /// Destination Address
    /// Who the packet is destined for.
    fn destination_address(&mut self, mac_address: MacAddress) {
        self.receiver_address(mac_address)
    }
}

#[test]
fn test_frame_builder() {
    let mut frame_builder = FrameBuilder::new();

    frame_builder.version(FrameVersion::Standard);
    frame_builder.type_(FrameType::Data);
    frame_builder.subtype(FrameSubtype::Data(DataSubtype::Null));
    frame_builder.ds_status(DSStatus::WDSOrMesh);

    frame_builder.more_fragments(true);
    frame_builder.retry(true);
    frame_builder.pwr_mgt(true);
    frame_builder.more_data(true);
    frame_builder.protected(true);
    frame_builder.order(true);

    frame_builder.receiver_address("11:22:33:44:55:66".parse().unwrap());

    let frame = frame_builder.build();

    assert_eq!(frame.version(), FrameVersion::Standard, "version");
    assert_eq!(frame.type_(), FrameType::Data, "type_");
    assert_eq!(
        frame.subtype(),
        FrameSubtype::Data(DataSubtype::Null),
        "subtype"
    );
    assert_eq!(frame.ds_status(), DSStatus::WDSOrMesh, "ds_status");

    assert_eq!(frame.more_fragments(), true, "more_fragments");
    assert_eq!(frame.retry(), true, "retry");
    assert_eq!(frame.pwr_mgt(), true, "pwr_mgt");
    assert_eq!(frame.more_data(), true, "more_data");
    assert_eq!(frame.protected(), true, "protected");
    assert_eq!(frame.order(), true, "order");

    assert_eq!(
        frame.receiver_address(),
        "11:22:33:44:55:66".parse().unwrap(),
        "receiver_address"
    );
}