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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use core::str::FromStr;

use nom::AsBytes;
use semver::Version;
use serde::{Deserialize, Serialize};

use crate::hyper::Knock;
use crate::UniErr;
use crate::wave::{Ping, Pong, UltraWave};

pub struct PrimitiveFrame {
    pub data: Vec<u8>,
}

impl PrimitiveFrame {
    pub fn size(&self) -> u32 {
        self.data.len() as u32
    }
}

impl From<Vec<u8>> for PrimitiveFrame {
    fn from(value: Vec<u8>) -> Self {
        Self { data: value }
    }
}

impl From<String> for PrimitiveFrame {
    fn from(value: String) -> Self {
        let bytes = value.as_bytes();
        Self {
            data: bytes.to_vec(),
        }
    }
}

impl TryInto<String> for PrimitiveFrame {
    type Error = UniErr;

    fn try_into(self) -> Result<String, Self::Error> {
        Ok(String::from_utf8(self.data)?)
    }
}

impl TryInto<semver::Version> for PrimitiveFrame {
    type Error = UniErr;

    fn try_into(self) -> Result<semver::Version, Self::Error> {
        let data = String::from_utf8(self.data)?;
        Ok(semver::Version::from_str(data.as_str())?)
    }
}

impl From<semver::Version> for PrimitiveFrame {
    fn from(version: Version) -> Self {
        let data = version.to_string();
        PrimitiveFrame::from(data)
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, strum_macros::Display)]
pub enum CloseReason {
    Done,
    Error(String),
}

impl TryInto<PrimitiveFrame> for Ping {
    type Error = UniErr;

    fn try_into(self) -> Result<PrimitiveFrame, Self::Error> {
        let data = bincode::serialize(&self)?;
        Ok(PrimitiveFrame::from(data))
    }
}

impl TryInto<Ping> for PrimitiveFrame {
    type Error = UniErr;

    fn try_into(self) -> Result<Ping, Self::Error> {
        Ok(bincode::deserialize(self.data.as_bytes())?)
    }
}

impl TryInto<PrimitiveFrame> for Pong {
    type Error = UniErr;

    fn try_into(self) -> Result<PrimitiveFrame, Self::Error> {
        let data = bincode::serialize(&self)?;
        Ok(PrimitiveFrame::from(data))
    }
}

impl TryInto<Pong> for PrimitiveFrame {
    type Error = UniErr;

    fn try_into(self) -> Result<Pong, Self::Error> {
        Ok(bincode::deserialize(self.data.as_bytes())?)
    }
}

impl TryInto<PrimitiveFrame> for UltraWave {
    type Error = UniErr;

    fn try_into(self) -> Result<PrimitiveFrame, Self::Error> {
        let data = bincode::serialize(&self)?;
        Ok(PrimitiveFrame::from(data))
    }
}

impl TryInto<UltraWave> for PrimitiveFrame {
    type Error = UniErr;

    fn try_into(self) -> Result<UltraWave, Self::Error> {
        Ok(bincode::deserialize(self.data.as_bytes())?)
    }
}

impl TryInto<PrimitiveFrame> for Knock {
    type Error = UniErr;

    fn try_into(self) -> Result<PrimitiveFrame, Self::Error> {
        let data = bincode::serialize(&self)?;
        Ok(PrimitiveFrame::from(data))
    }
}

impl TryInto<Knock> for PrimitiveFrame {
    type Error = UniErr;

    fn try_into(self) -> Result<Knock, Self::Error> {
        Ok(bincode::deserialize(self.data.as_bytes())?)
    }
}