1pub enum Message {
3 Text(String),
5 Binary(Vec<u8>),
7 Ping(Vec<u8>),
9 Pong(Vec<u8>),
11 Close
13}
14
15impl Message {
16 pub fn text<A: Into<String>>(text: A) -> Message {
18 Message::Text(text.into())
19 }
20
21 pub fn binary<A: Into<Vec<u8>>>(bytes: A) -> Message {
23 Message::Binary(bytes.into())
24 }
25
26 pub fn ping<A: Into<Vec<u8>>>(payload: A) -> Message {
28 Message::Ping(payload.into())
29 }
30
31 pub fn pong<A: Into<Vec<u8>>>(payload: A) -> Message {
33 Message::Pong(payload.into())
34 }
35
36 pub fn is_close(&self) -> bool {
38 matches!(&self, Message::Close)
39 }
40
41 pub fn is_ping(&self) -> bool {
43 matches!(&self, Message::Ping(_))
44 }
45
46 pub fn is_pong(&self) -> bool {
48 matches!(&self, Message::Pong(_))
49 }
50}
51
52impl From<Message> for Vec<u8> {
53 fn from(source: Message) -> Vec<u8> {
54 match source {
55 Message::Text(content) => content.into(),
56 Message::Binary(content) => content,
57 Message::Ping(content) => content,
58 Message::Pong(content) => content,
59 Message::Close => vec![]
60 }
61 }
62}