g_rust/protocol/
hdirection.rs

1use std::convert::Infallible;
2use std::str::FromStr;
3use crate::protocol::vars::packetvariable::PacketVariable;
4
5#[derive(Debug, Clone, PartialEq, Eq, Hash)]
6pub enum HDirection {
7    ToClient,
8    ToServer,
9    None
10}
11
12impl Default for HDirection {
13    fn default() -> Self {
14        HDirection::None
15    }
16}
17
18impl FromStr for HDirection {
19    type Err = Infallible;
20
21    fn from_str(s: &str) -> Result<Self, Self::Err> {
22        Ok(
23            match s {
24                "TOCLIENT" => HDirection::ToClient,
25                "TOSERVER" => HDirection::ToServer,
26                _ => HDirection::None
27            }
28        )
29    }
30}
31
32impl ToString for HDirection {
33    fn to_string(&self) -> String {
34        match self {
35            HDirection::ToClient => "TOCLIENT".to_string(),
36            HDirection::ToServer => "TOSERVER".to_string(),
37            HDirection::None => "NONE".to_string()
38        }
39    }
40}
41
42impl PacketVariable for HDirection {
43    fn from_packet(bytes: Vec<u8>) -> (Self, usize) where Self: Sized {
44        (
45            if bool::from_packet(bytes).0 { HDirection::ToServer } else { HDirection::ToClient },
46            1
47        )
48    }
49
50    fn to_packet(&self) -> Vec<u8> {
51        (self == &HDirection::ToServer).to_packet()
52    }
53}