Skip to main content

mayuri/core/
enums.rs

1use std::io;
2use strum::FromRepr;
3
4#[repr(C)]
5#[derive(Debug, Copy, Clone, PartialEq, Eq, FromRepr)]
6pub enum State {
7    CONNECTING = 0,
8    OPEN = 1,
9    CLOSING = 2,
10    CLOSED = 3,
11    ERROR = 4,
12}
13
14impl State {
15    #[must_use]
16    pub const fn as_u8(&self) -> u8 {
17        *self as u8
18    }
19}
20
21#[derive(Copy, Clone, Debug, PartialEq, Eq)]
22pub enum Opcode {
23    Continuation = 0x0,
24    Text = 0x1,
25    Binary = 0x2,
26    Close = 0x8,
27    Ping = 0x9,
28    Pong = 0xA,
29}
30
31impl Opcode {
32    pub fn from_u8(opcode: u8) -> Result<Self, io::Error> {
33        Ok(match opcode {
34            0x0 => Self::Continuation,
35            0x1 => Self::Text,
36            0x2 => Self::Binary,
37            0x8 => Self::Close,
38            0x9 => Self::Ping,
39            0xA => Self::Pong,
40            _ => {
41                return Err(io::Error::other(format!("Bad Opcode {opcode}")));
42            }
43        })
44    }
45}