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
/// Message structure contained in a frame
pub enum Message {
/// Text message
Text(String),
/// Binary message
Binary(Vec<u8>),
/// Ping message
Ping(Vec<u8>),
/// Pong message
Pong(Vec<u8>),
/// Close message
Close
}
impl Message {
/// Geneates an instances of the [Message::Text](Message::Text) variant
pub fn text<A: Into<String>>(text: A) -> Message {
Message::Text(text.into())
}
/// Geneates an instances of the [Message::Binary](Message::Binary) variant
pub fn binary<A: Into<Vec<u8>>>(bytes: A) -> Message {
Message::Binary(bytes.into())
}
/// Geneates an instances of the [Message::Binary](Message::Binary) variant
pub fn ping<A: Into<Vec<u8>>>(payload: A) -> Message {
Message::Ping(payload.into())
}
/// Geneates an instances of the [Message::Binary](Message::Binary) variant
pub fn pong<A: Into<Vec<u8>>>(payload: A) -> Message {
Message::Pong(payload.into())
}
/// Indicates if the variant equates de [Message::Close](Message::Close) variant
pub fn is_close(&self) -> bool {
matches!(&self, Message::Close)
}
/// Indicates if the variant equates de [Message::Ping](Message::Ping) variant
pub fn is_ping(&self) -> bool {
matches!(&self, Message::Ping(_))
}
/// Indicates if the variant equates de [Message::Pong](Message::Pong) variant
pub fn is_pong(&self) -> bool {
matches!(&self, Message::Pong(_))
}
}
impl From<Message> for Vec<u8> {
fn from(source: Message) -> Vec<u8> {
match source {
Message::Text(content) => content.into(),
Message::Binary(content) => content,
Message::Ping(content) => content,
Message::Pong(content) => content,
Message::Close => vec![]
}
}
}