Skip to main content

http_request/request/socket/message/
impl.rs

1use crate::*;
2
3impl WebSocketMessage {
4    pub fn text<T: ToString>(text: T) -> Self {
5        Self::Text(text.to_string())
6    }
7
8    pub fn binary<T: Into<Vec<u8>>>(data: T) -> Self {
9        Self::Binary(data.into())
10    }
11
12    pub fn ping<T: Into<Vec<u8>>>(data: T) -> Self {
13        Self::Ping(data.into())
14    }
15
16    pub fn pong<T: Into<Vec<u8>>>(data: T) -> Self {
17        Self::Pong(data.into())
18    }
19
20    pub fn close() -> Self {
21        Self::Close
22    }
23
24    pub fn is_text(&self) -> bool {
25        matches!(self, Self::Text(_))
26    }
27
28    pub fn is_binary(&self) -> bool {
29        matches!(self, Self::Binary(_))
30    }
31
32    pub fn is_ping(&self) -> bool {
33        matches!(self, Self::Ping(_))
34    }
35
36    pub fn is_pong(&self) -> bool {
37        matches!(self, Self::Pong(_))
38    }
39
40    pub fn is_close(&self) -> bool {
41        matches!(self, Self::Close)
42    }
43
44    pub fn as_text(&self) -> Option<&str> {
45        match self {
46            Self::Text(text) => Some(text),
47            _ => None,
48        }
49    }
50
51    pub fn as_binary(&self) -> Option<&[u8]> {
52        match self {
53            Self::Binary(data) => Some(data),
54            _ => None,
55        }
56    }
57
58    pub fn into_text(self) -> Option<String> {
59        match self {
60            Self::Text(text) => Some(text),
61            _ => None,
62        }
63    }
64
65    pub fn into_binary(self) -> Option<Vec<u8>> {
66        match self {
67            Self::Binary(data) => Some(data),
68            _ => None,
69        }
70    }
71}