aboutsummaryrefslogtreecommitdiff
path: root/crates/whirl_server/src/cmd/structure.rs
blob: 82cb22695468fff61b536a6c4e3512f4314e739e (plain) (blame)
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
// Copyright (C) 2021-2021 The Whirlsplash Collective
// SPDX-License-Identifier: GPL-3.0-only

pub struct Command {
  pub length: i32,
  pub obj_id: i32,
  pub id:     i32,
  pub body:   Vec<u8>,
}
impl Command {
  /// Create and return a new `Command` with default values (`0`s and empty).
  pub fn _new() -> Self { Self::default() }

  pub fn _from_byte(mut data: Vec<u8>) -> Vec<Self> {
    let mut command_set = vec![];

    // Iterate over all commands
    loop {
      // Check if any commands are present
      if data.len() <= 2 {
        break;
      }
      if data[0] == 0 {
        break;
      }

      let command_length = data[0];
      let mut command = Self {
        length: i32::from(command_length),
        obj_id: i32::from(data[1]),
        id:     i32::from(data[2]),
        body:   vec![],
      };
      if command.length > 3 {
        command.body = data[3..].to_owned();
      }
      command_set.push(command);

      // Remove current command from the command set
      data = data[command_length as usize..].to_vec();
    }

    // Return the human-readable command set
    command_set
  }
}
impl Default for Command {
  fn default() -> Self {
    Self {
      length: 0,
      obj_id: 0,
      id:     0,
      body:   vec![],
    }
  }
}