aboutsummaryrefslogtreecommitdiff
path: root/crates/whirl_server/src/cmd/commands/text.rs
blob: dac13b68b010f97649ac9fb6d5c93462935fa451 (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
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (C) 2021-2021 The Whirlsplash Collective
// SPDX-License-Identifier: GPL-3.0-only

use {
  crate::cmd::{
    constants::Command,
    extendable::{Creatable, ParsableWithArguments},
  },
  bytes::{BufMut, BytesMut},
  std::str::from_utf8,
};

pub struct Text {
  pub sender:  String,
  pub content: String,
}
impl Creatable for Text {
  fn create(&self) -> Vec<u8> {
    let mut command = BytesMut::new();

    // Header
    command.put_u8(0x01);
    #[allow(clippy::cast_possible_truncation)]
    command.put_i8(Command::Text as i32 as i8);

    // Content
    // TODO: Find a way to parse ObjIds.
    //
    // The below byte is suspected to be the sender's short ObjId.
    command.put_i8(0x00);

    command.put_u8(self.sender.len() as u8);
    command.put_slice(self.sender.as_bytes());
    command.put_u8(self.content.len() as u8);
    command.put_slice(self.content.as_bytes());

    // Convert to vector and insert the length
    let mut command_as_vec = command.to_vec();
    command_as_vec.insert(0, command.len() as u8 + 1);

    // Return bytes
    command_as_vec
  }
}
impl ParsableWithArguments for Text {
  /// The first and only element of `args` *should* be the username of the
  /// sender.
  ///
  /// There isn't anything currently stopping someone from passing some other
  /// value so that might be annoying at times.
  ///
  /// Realistically, this method is mostly static so the username will *always*
  /// be passed properly unless someone intentionally commits breaking changes
  /// on purpose regarding what is passed to to this method where called.
  ///
  /// It would be neat to have some sort of ability to statically check if the
  /// `args` argument contains x number of elements at compile time or
  /// something of the sort but the Rust RFC is probably not focused on that.
  ///
  /// So, right now, trust is in the developers' hands to make sure to pass the
  /// right — number — of elements to `args`.
  fn parse(data: Vec<u8>, args: &[&str]) -> Self {
    Self {
      sender:  args[0].to_string(),
      content: from_utf8(&data[6..]).unwrap().to_string(),
    }
  }
}