Skip to main content

rustmeter_beacon_core/protocol/
requests.rs

1use crate::buffer::BufferWriter;
2
3#[derive(Debug, Clone, PartialEq, Eq, Copy)]
4pub enum Request {
5    GetGlobalClockDefinition,
6    GetCoreClockReference { core_id: u8 },
7}
8
9impl Request {
10    pub fn type_id(&self) -> u8 {
11        match self {
12            Request::GetGlobalClockDefinition => 1,
13            Request::GetCoreClockReference { .. } => 2,
14        }
15    }
16
17    /// Serialize the Request into bytes
18    pub fn write_bytes(&self, writer: &mut BufferWriter) {
19        writer.write_byte(self.type_id());
20
21        match self {
22            Request::GetGlobalClockDefinition => {}
23            Request::GetCoreClockReference { core_id } => {
24                writer.write_byte(*core_id);
25            }
26        }
27    }
28
29    /// Deserialize a Request from bytes
30    pub fn from_bytes<'a, T: Iterator<Item = &'a u8>>(mut buffer: T) -> Option<(Self, usize)> {
31        let req_id = buffer.next()?;
32
33        match req_id {
34            1 => Some((Request::GetGlobalClockDefinition, 1)),
35            2 => {
36                let core_id = *buffer.next()?;
37                Some((Request::GetCoreClockReference { core_id }, 2))
38            }
39            _ => None,
40        }
41    }
42}