use smallvec::SmallVec;
use crate::CloseCode;
use crate::WebsocketMessage;
pub(crate) enum WebsocketCommand {
Send(WebsocketMessage),
Close(CloseCode, String),
}
pub struct WebsocketCommands {
pub(crate) buffer: SmallVec<[WebsocketCommand; 6]>,
}
impl WebsocketCommands {
pub fn new() -> Self {
Self {
buffer: SmallVec::new(),
}
}
pub fn with_send<T: Into<WebsocketMessage>>(message: T) -> Self {
let mut result = Self::new();
result.send(message);
result
}
pub fn with_close<T: Into<String>>(code: CloseCode, reason: T) -> Self {
let mut result = Self::new();
result.close(code, reason);
result
}
pub fn send<T: Into<WebsocketMessage>>(&mut self, message: T) {
self.buffer.push(WebsocketCommand::Send(message.into()));
}
pub fn close<T: Into<String>>(&mut self, code: CloseCode, reason: T) {
self.buffer
.push(WebsocketCommand::Close(code, reason.into()));
}
}
impl Default for WebsocketCommands {
fn default() -> Self {
Self::new()
}
}