rfc2217_rs/
command.rs

1use crate::codes;
2
3pub const SIZE: usize = 2;
4
5// Telnet commands without the ones related to negotiation and subnegotiation,
6// defined here: https://www.rfc-editor.org/rfc/rfc854.txt
7#[derive(Debug, PartialEq, Eq, Clone, Copy)]
8pub enum Command {
9    NoOp,
10    DataMark,
11    Break,
12    InterruptProcess,
13    AbortOutput,
14    AreYouThere,
15    EraseCharacter,
16    EraseLine,
17    GoAhead,
18    Unsupported(u8),
19}
20
21impl Command {
22    pub fn serialize(&self, buf: &mut [u8]) {
23        buf[0] = codes::IAC;
24        buf[1] = match *self {
25            Self::NoOp => 241,
26            Self::DataMark => 242,
27            Self::Break => 243,
28            Self::InterruptProcess => 244,
29            Self::AbortOutput => 245,
30            Self::AreYouThere => 246,
31            Self::EraseCharacter => 247,
32            Self::EraseLine => 248,
33            Self::GoAhead => 249,
34            Self::Unsupported(byte) => byte,
35        }
36    }
37
38    pub const fn deserialize(buf: &[u8]) -> Self {
39        assert!(buf[0] == codes::IAC);
40        match buf[1] {
41            241 => Self::NoOp,
42            242 => Self::DataMark,
43            243 => Self::Break,
44            244 => Self::InterruptProcess,
45            245 => Self::AbortOutput,
46            246 => Self::AreYouThere,
47            247 => Self::EraseCharacter,
48            248 => Self::EraseLine,
49            249 => Self::GoAhead,
50            _ => Self::Unsupported(buf[1]),
51        }
52    }
53}