cosmic_space/
frame.rs

1use core::str::FromStr;
2
3use nom::AsBytes;
4use semver::Version;
5use serde::{Deserialize, Serialize};
6
7use crate::hyper::Knock;
8use crate::wave::{Ping, Pong, UltraWave};
9use crate::SpaceErr;
10
11pub struct PrimitiveFrame {
12    pub data: Vec<u8>,
13}
14
15impl PrimitiveFrame {
16    pub fn size(&self) -> u32 {
17        self.data.len() as u32
18    }
19}
20
21impl From<Vec<u8>> for PrimitiveFrame {
22    fn from(value: Vec<u8>) -> Self {
23        Self { data: value }
24    }
25}
26
27impl From<String> for PrimitiveFrame {
28    fn from(value: String) -> Self {
29        let bytes = value.as_bytes();
30        Self {
31            data: bytes.to_vec(),
32        }
33    }
34}
35
36impl TryInto<String> for PrimitiveFrame {
37    type Error = SpaceErr;
38
39    fn try_into(self) -> Result<String, Self::Error> {
40        Ok(String::from_utf8(self.data)?)
41    }
42}
43
44impl TryInto<semver::Version> for PrimitiveFrame {
45    type Error = SpaceErr;
46
47    fn try_into(self) -> Result<semver::Version, Self::Error> {
48        let data = String::from_utf8(self.data)?;
49        Ok(semver::Version::from_str(data.as_str())?)
50    }
51}
52
53impl From<semver::Version> for PrimitiveFrame {
54    fn from(version: Version) -> Self {
55        let data = version.to_string();
56        PrimitiveFrame::from(data)
57    }
58}
59
60#[derive(Debug, Clone, Serialize, Deserialize, strum_macros::Display)]
61pub enum CloseReason {
62    Done,
63    Error(String),
64}
65
66impl TryInto<PrimitiveFrame> for Ping {
67    type Error = SpaceErr;
68
69    fn try_into(self) -> Result<PrimitiveFrame, Self::Error> {
70        let data = bincode::serialize(&self)?;
71        Ok(PrimitiveFrame::from(data))
72    }
73}
74
75impl TryInto<Ping> for PrimitiveFrame {
76    type Error = SpaceErr;
77
78    fn try_into(self) -> Result<Ping, Self::Error> {
79        Ok(bincode::deserialize(self.data.as_bytes())?)
80    }
81}
82
83impl TryInto<PrimitiveFrame> for Pong {
84    type Error = SpaceErr;
85
86    fn try_into(self) -> Result<PrimitiveFrame, Self::Error> {
87        let data = bincode::serialize(&self)?;
88        Ok(PrimitiveFrame::from(data))
89    }
90}
91
92impl TryInto<Pong> for PrimitiveFrame {
93    type Error = SpaceErr;
94
95    fn try_into(self) -> Result<Pong, Self::Error> {
96        Ok(bincode::deserialize(self.data.as_bytes())?)
97    }
98}
99
100impl TryInto<PrimitiveFrame> for UltraWave {
101    type Error = SpaceErr;
102
103    fn try_into(self) -> Result<PrimitiveFrame, Self::Error> {
104        let data = bincode::serialize(&self)?;
105        Ok(PrimitiveFrame::from(data))
106    }
107}
108
109impl TryInto<UltraWave> for PrimitiveFrame {
110    type Error = SpaceErr;
111
112    fn try_into(self) -> Result<UltraWave, Self::Error> {
113        Ok(bincode::deserialize(self.data.as_bytes())?)
114    }
115}
116
117impl TryInto<PrimitiveFrame> for Knock {
118    type Error = SpaceErr;
119
120    fn try_into(self) -> Result<PrimitiveFrame, Self::Error> {
121        let data = bincode::serialize(&self)?;
122        Ok(PrimitiveFrame::from(data))
123    }
124}
125
126impl TryInto<Knock> for PrimitiveFrame {
127    type Error = SpaceErr;
128
129    fn try_into(self) -> Result<Knock, Self::Error> {
130        Ok(bincode::deserialize(self.data.as_bytes())?)
131    }
132}