use custom_debug_derive::Debug as CustomDebug;
use frame::Frame;
pub mod client;
mod frame;
pub(crate) type Result<T> = std::result::Result<T, anyhow::Error>;
#[derive(Debug)]
pub struct Message<T> {
pub content: T,
pub extra_headers: Vec<(Vec<u8>, Vec<u8>)>,
}
fn pretty_bytes(b: &Option<Vec<u8>>, f: &mut std::fmt::Formatter) -> std::fmt::Result {
if let Some(v) = b {
write!(f, "{}", String::from_utf8_lossy(v))
} else {
write!(f, "None")
}
}
#[derive(CustomDebug, Clone)]
pub enum FromServer {
#[doc(hidden)] Connected {
version: String,
session: Option<String>,
server: Option<String>,
heartbeat: Option<String>,
},
Message {
destination: String,
message_id: String,
subscription: String,
headers: Vec<(String, String)>,
#[debug(with = "pretty_bytes")]
body: Option<Vec<u8>>,
},
Receipt {
receipt_id: String,
},
Error {
message: Option<String>,
#[debug(with = "pretty_bytes")]
body: Option<Vec<u8>>,
},
}
impl Message<FromServer> {
fn from_frame(frame: Frame) -> Result<Message<FromServer>> {
frame.to_server_msg()
}
}
#[derive(Debug, Clone)]
pub enum ToServer {
#[doc(hidden)] Connect {
accept_version: String,
host: String,
login: Option<String>,
passcode: Option<String>,
heartbeat: Option<(u32, u32)>,
},
Send {
destination: String,
transaction: Option<String>,
headers: Option<Vec<(String, String)>>,
body: Option<Vec<u8>>,
},
Subscribe {
destination: String,
id: String,
ack: Option<AckMode>,
},
Unsubscribe {
id: String,
},
Ack {
id: String,
transaction: Option<String>,
},
Nack {
id: String,
transaction: Option<String>,
},
Begin {
transaction: String,
},
Commit {
transaction: String,
},
Abort {
transaction: String,
},
Disconnect {
receipt: Option<String>,
},
}
#[derive(Debug, Clone, Copy)]
pub enum AckMode {
Auto,
Client,
ClientIndividual,
}
impl Message<ToServer> {
fn to_frame<'a>(&'a self) -> Frame<'a> {
let mut frame = self.content.to_frame();
frame.add_extra_headers(&self.extra_headers);
frame
}
#[allow(dead_code)]
fn from_frame(frame: Frame) -> Result<Message<ToServer>> {
frame.to_client_msg()
}
}
impl From<ToServer> for Message<ToServer> {
fn from(content: ToServer) -> Message<ToServer> {
Message {
content,
extra_headers: vec![],
}
}
}