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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/// a close message, with a websocket close code
/// https://www.iana.org/assignments/websocket/websocket.xml#close-code-number
#[derive(Debug, Clone)]
pub struct CloseData {
    pub code: u16,
    pub reason: String,
}

/// enumerates the different websocket message types
#[derive(Debug, Clone)]
pub enum WsFrame {
    /// utf8 text
    Text(String),
    /// a string of bytes
    Binary(Vec<u8>),
    /// a ping message, content must be < 125 bytes
    Ping(Vec<u8>),
    /// a pong message, content must be < 125 bytes
    Pong(Vec<u8>),
    /// a close message, with a websocket close code
    /// https://www.iana.org/assignments/websocket/websocket.xml#close-code-number
    Close(CloseData),
}

impl Default for WsFrame {
    fn default() -> Self {
        WsFrame::Text(String::new())
    }
}

impl From<String> for WsFrame {
    fn from(s: String) -> Self {
        WsFrame::Text(s)
    }
}

impl From<&str> for WsFrame {
    fn from(s: &str) -> Self {
        WsFrame::Text(s.to_string())
    }
}

impl From<Vec<u8>> for WsFrame {
    fn from(b: Vec<u8>) -> Self {
        WsFrame::Binary(b)
    }
}

impl From<&[u8]> for WsFrame {
    fn from(b: &[u8]) -> Self {
        WsFrame::Binary(b.to_vec())
    }
}

impl From<tungstenite::protocol::Message> for WsFrame {
    fn from(m: tungstenite::protocol::Message) -> Self {
        match m {
            tungstenite::protocol::Message::Text(s) => WsFrame::Text(s),
            tungstenite::protocol::Message::Binary(b) => WsFrame::Binary(b),
            tungstenite::protocol::Message::Ping(b) => WsFrame::Ping(b),
            tungstenite::protocol::Message::Pong(b) => WsFrame::Pong(b),
            tungstenite::protocol::Message::Close(c) => {
                match c {
                    None => WsFrame::Close(CloseData {
                        code: 1005, // no status received
                        reason: String::new(),
                    }),
                    Some(c) => WsFrame::Close(CloseData {
                        code: c.code.into(),
                        reason: c.reason.to_string(),
                    }),
                }
            }
        }
    }
}

impl From<WsFrame> for tungstenite::protocol::Message {
    fn from(m: WsFrame) -> Self {
        match m {
            WsFrame::Text(s) => tungstenite::protocol::Message::Text(s),
            WsFrame::Binary(b) => tungstenite::protocol::Message::Binary(b),
            WsFrame::Ping(b) => tungstenite::protocol::Message::Ping(b),
            WsFrame::Pong(b) => tungstenite::protocol::Message::Pong(b),
            WsFrame::Close(c) => tungstenite::protocol::Message::Close(Some(
                tungstenite::protocol::CloseFrame {
                    code: c.code.into(),
                    reason: std::borrow::Cow::from(&c.reason),
                }
                .into_owned(),
            )),
        }
    }
}

impl WsFrame {
    /// consume another frame-type instance
    pub fn assume<O: Into<Self>>(&mut self, oth: O) {
        *self = oth.into();
    }

    /// get frame data out as byte slice
    pub fn as_bytes(&self) -> &[u8] {
        match self {
            WsFrame::Text(s) => s.as_bytes(),
            WsFrame::Binary(b) => b,
            WsFrame::Ping(b) => b,
            WsFrame::Pong(b) => b,
            WsFrame::Close(c) => c.reason.as_bytes(),
        }
    }

    /// get frame data out as a string slice
    pub fn as_str(&self) -> std::borrow::Cow<'_, str> {
        match self {
            WsFrame::Text(s) => std::borrow::Cow::from(s),
            WsFrame::Binary(b) => String::from_utf8_lossy(b),
            WsFrame::Ping(b) => String::from_utf8_lossy(b),
            WsFrame::Pong(b) => String::from_utf8_lossy(b),
            WsFrame::Close(c) => std::borrow::Cow::from(&c.reason),
        }
    }

    /// true if frame type is WsFrame::Text
    pub fn is_text(&self) -> bool {
        if let WsFrame::Text(_) = self {
            true
        } else {
            false
        }
    }

    /// true if frame type is WsFrame::Binary
    pub fn is_binary(&self) -> bool {
        if let WsFrame::Binary(_) = self {
            true
        } else {
            false
        }
    }

    /// true if frame type is WsFrame::Ping
    pub fn is_ping(&self) -> bool {
        if let WsFrame::Ping(_) = self {
            true
        } else {
            false
        }
    }

    /// true if frame type is WsFrame::Pong
    pub fn is_pong(&self) -> bool {
        if let WsFrame::Pong(_) = self {
            true
        } else {
            false
        }
    }

    /// true if frame type is WsFrame::Close
    pub fn is_close(&self) -> bool {
        if let WsFrame::Close(_) = self {
            true
        } else {
            false
        }
    }
}