1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
use bytes::*;

use crate::interface::command::Command;
use crate::interface::controller::Controller;

#[derive(Debug)]
pub struct Request {
    pub opcode: Command,
    pub controller: Controller,
    pub param: Bytes,
}

impl Into<Bytes> for Request {
    fn into(self) -> Bytes {
        let mut buf = BytesMut::with_capacity(6 + self.param.len());

        buf.put_u16_le(self.opcode as u16);
        buf.put_u16_le(self.controller.into());
        buf.put_u16_le(self.param.len() as u16);
        buf.put(self.param);

        buf.freeze()
    }
}