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
use std::io::{Error, ErrorKind};

#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum Command {
    Connect,
    Bind,
    Associate,
}

impl Command {
    const CMD_CONNECT: u8 = 0x01;
    const CMD_BIND: u8 = 0x02;
    const CMD_ASSOCIATE: u8 = 0x03;
}

impl TryFrom<u8> for Command {
    type Error = Error;

    fn try_from(code: u8) -> Result<Self, Self::Error> {
        match code {
            Self::CMD_CONNECT => Ok(Command::Connect),
            Self::CMD_BIND => Ok(Command::Bind),
            Self::CMD_ASSOCIATE => Ok(Command::Associate),
            code => Err(Error::new(
                ErrorKind::Unsupported,
                format!("Unsupported command {0:#x}", code),
            )),
        }
    }
}

impl From<Command> for u8 {
    fn from(cmd: Command) -> Self {
        match cmd {
            Command::Connect => Command::CMD_CONNECT,
            Command::Bind => Command::CMD_BIND,
            Command::Associate => Command::CMD_ASSOCIATE,
        }
    }
}