hydra_websockets/
websocket_command.rs1use smallvec::SmallVec;
2
3use crate::CloseCode;
4use crate::WebsocketMessage;
5
6pub(crate) enum WebsocketCommand {
8 Send(WebsocketMessage),
9 Close(CloseCode, String),
10}
11
12pub struct WebsocketCommands {
14 pub(crate) buffer: SmallVec<[WebsocketCommand; 6]>,
15}
16
17impl WebsocketCommands {
18 pub fn new() -> Self {
20 Self {
21 buffer: SmallVec::new(),
22 }
23 }
24
25 pub fn with_send<T: Into<WebsocketMessage>>(message: T) -> Self {
27 let mut result = Self::new();
28
29 result.send(message);
30 result
31 }
32
33 pub fn with_close<T: Into<String>>(code: CloseCode, reason: T) -> Self {
35 let mut result = Self::new();
36
37 result.close(code, reason);
38 result
39 }
40
41 pub fn send<T: Into<WebsocketMessage>>(&mut self, message: T) {
43 self.buffer.push(WebsocketCommand::Send(message.into()));
44 }
45
46 pub fn close<T: Into<String>>(&mut self, code: CloseCode, reason: T) {
48 self.buffer
49 .push(WebsocketCommand::Close(code, reason.into()));
50 }
51}
52
53impl Default for WebsocketCommands {
54 fn default() -> Self {
55 Self::new()
56 }
57}