use std::default::Default;
pub struct Client {
props: lapin::ConnectionProperties,
}
impl Client {
pub fn new() -> Self {
Self {
..Default::default()
}
}
pub async fn connect(&self, uri: &str) -> crate::Result<Connection> {
let c = lapin::Connection::connect(uri, self.props.clone())
.await
.map_err(crate::Error::from)?;
Ok(Connection(c))
}
}
impl Default for Client {
fn default() -> Self {
Self {
props: lapin::ConnectionProperties::default(),
}
}
}
#[derive(Clone)]
pub struct Connection(lapin::Connection);
#[derive(Clone)]
pub struct QueueOptions {
pub kind: lapin::ExchangeKind,
pub ex_opts: lapin::options::ExchangeDeclareOptions,
pub ex_field: lapin::types::FieldTable,
pub queue_opts: lapin::options::QueueDeclareOptions,
pub queue_field: lapin::types::FieldTable,
pub bind_opts: lapin::options::QueueBindOptions,
pub bind_field: lapin::types::FieldTable,
}
impl Connection {
pub fn producer_builder(&self) -> crate::ProducerBuilder {
crate::ProducerBuilder::new(self.clone())
}
pub fn consumer_builder(&self) -> crate::ConsumerBuilder {
crate::ConsumerBuilder::new(self.clone())
}
pub async fn channel(&self) -> crate::Result<lapin::Channel> {
self.0.create_channel().await.map_err(crate::Error::from)
}
pub async fn queue(
&self,
ex: &str,
queue: &str,
opts: QueueOptions,
) -> crate::Result<(lapin::Channel, lapin::Queue)> {
let ch = self.0.create_channel().await.map_err(crate::Error::from)?;
let q = ch
.queue_declare(queue, opts.queue_opts, opts.queue_field)
.await
.map_err(crate::Error::from)?;
if Self::is_default_exchange(ex) {
return Ok((ch, q));
}
ch.exchange_declare(ex, opts.kind, opts.ex_opts, opts.ex_field)
.await
.map_err(crate::Error::from)?;
let routing_key = if Self::is_ephemeral_queue(queue) {
q.name().as_str()
} else {
queue
};
ch.queue_bind(
queue,
ex,
routing_key,
opts.bind_opts.clone(),
opts.bind_field.clone(),
)
.await
.map_err(crate::Error::from)?;
Ok((ch, q))
}
fn is_default_exchange(name: &str) -> bool {
name == crate::DEFAULT_EXCHANGE
}
fn is_ephemeral_queue(name: &str) -> bool {
name == crate::EPHEMERAL_QUEUE
}
}