use super::compat::LapinConfirmation;
use super::lapin;
use std::fmt;
use std::ops::{Deref, DerefMut};
pub struct LapinChannel {
inner: lapin::Channel,
}
impl LapinChannel {
pub fn new(inner: lapin::Channel) -> Self {
Self { inner }
}
pub fn into_inner(self) -> lapin::Channel {
self.inner
}
fn encode_payload<V>(value: &V) -> Result<Vec<u8>, VldLapinError>
where
V: serde::Serialize + vld::schema::VldParse,
{
let json =
serde_json::to_value(value).map_err(|e| VldLapinError::Serialization(e.to_string()))?;
<V as vld::schema::VldParse>::vld_parse_value(&json).map_err(VldLapinError::Validation)?;
serde_json::to_vec(&json).map_err(|e| VldLapinError::Serialization(e.to_string()))
}
pub fn decode_bytes<T>(&self, payload: &[u8]) -> Result<T, VldLapinError>
where
T: vld::schema::VldParse,
{
decode(payload)
}
#[allow(clippy::useless_conversion)]
pub async fn queue_declare(
&self,
queue: &str,
options: lapin::options::QueueDeclareOptions,
arguments: lapin::types::FieldTable,
) -> Result<lapin::Queue, VldLapinError> {
self.inner
.queue_declare(queue.into(), options, arguments)
.await
.map_err(VldLapinError::Lapin)
}
#[allow(clippy::useless_conversion)]
pub async fn basic_get(
&self,
queue: &str,
options: lapin::options::BasicGetOptions,
) -> Result<Option<lapin::message::BasicGetMessage>, VldLapinError> {
self.inner
.basic_get(queue.into(), options)
.await
.map_err(VldLapinError::Lapin)
}
#[allow(clippy::useless_conversion)]
pub async fn publish<V>(
&self,
exchange: &str,
routing_key: &str,
options: lapin::options::BasicPublishOptions,
properties: lapin::BasicProperties,
value: &V,
) -> Result<LapinConfirmation, VldLapinError>
where
V: serde::Serialize + vld::schema::VldParse,
{
let payload = Self::encode_payload(value)?;
let confirm = self
.inner
.basic_publish(
exchange.into(),
routing_key.into(),
options,
&payload,
properties,
)
.await
.map_err(VldLapinError::Lapin)?;
confirm.await.map_err(VldLapinError::Lapin)
}
pub fn decode_delivery<T>(
&self,
delivery: &lapin::message::Delivery,
) -> Result<T, VldLapinError>
where
T: vld::schema::VldParse,
{
decode(&delivery.data)
}
pub fn decode_get<T>(
&self,
message: &lapin::message::BasicGetMessage,
) -> Result<T, VldLapinError>
where
T: vld::schema::VldParse,
{
decode(&message.data)
}
pub async fn ack_decode<T>(
&self,
delivery: &lapin::message::Delivery,
options: lapin::options::BasicAckOptions,
) -> Result<T, VldLapinError>
where
T: vld::schema::VldParse,
{
let parsed = self.decode_delivery(delivery)?;
settled(delivery.ack(options).await?)?;
Ok(parsed)
}
pub async fn ack_or_nack_decode<T>(
&self,
delivery: &lapin::message::Delivery,
ack: lapin::options::BasicAckOptions,
nack: lapin::options::BasicNackOptions,
) -> Result<T, VldLapinError>
where
T: vld::schema::VldParse,
{
let parsed = decode_or_settle(&delivery.data, || delivery.nack(nack)).await?;
settled(delivery.ack(ack).await?)?;
Ok(parsed)
}
pub async fn nack_decode<T>(
&self,
delivery: &lapin::message::Delivery,
options: lapin::options::BasicNackOptions,
) -> Result<T, VldLapinError>
where
T: vld::schema::VldParse,
{
decode_or_settle(&delivery.data, || delivery.nack(options)).await
}
pub async fn reject_decode<T>(
&self,
delivery: &lapin::message::Delivery,
options: lapin::options::BasicRejectOptions,
) -> Result<T, VldLapinError>
where
T: vld::schema::VldParse,
{
decode_or_settle(&delivery.data, || delivery.reject(options)).await
}
pub async fn ack_decode_get<T>(
&self,
message: &lapin::message::BasicGetMessage,
options: lapin::options::BasicAckOptions,
) -> Result<T, VldLapinError>
where
T: vld::schema::VldParse,
{
self.ack_decode(message, options).await
}
pub async fn nack_decode_get<T>(
&self,
message: &lapin::message::BasicGetMessage,
options: lapin::options::BasicNackOptions,
) -> Result<T, VldLapinError>
where
T: vld::schema::VldParse,
{
self.nack_decode(message, options).await
}
pub async fn reject_decode_get<T>(
&self,
message: &lapin::message::BasicGetMessage,
options: lapin::options::BasicRejectOptions,
) -> Result<T, VldLapinError>
where
T: vld::schema::VldParse,
{
self.reject_decode(message, options).await
}
}
fn decode<T: vld::schema::VldParse>(payload: &[u8]) -> Result<T, VldLapinError> {
let value: serde_json::Value = serde_json::from_slice(payload)
.map_err(|e| VldLapinError::Deserialization(e.to_string()))?;
T::vld_parse_value(&value).map_err(VldLapinError::Validation)
}
trait Settle {
fn sent(&self) -> bool;
}
impl Settle for () {
fn sent(&self) -> bool {
true
}
}
impl Settle for bool {
fn sent(&self) -> bool {
*self
}
}
fn settled(r: impl Settle) -> Result<(), VldLapinError> {
if r.sent() {
Ok(())
} else {
Err(VldLapinError::NotSettled)
}
}
async fn decode_or_settle<T, R, F, Fut>(payload: &[u8], settle: F) -> Result<T, VldLapinError>
where
T: vld::schema::VldParse,
R: Settle,
F: FnOnce() -> Fut,
Fut: std::future::Future<Output = Result<R, lapin::Error>>,
{
match decode(payload) {
Ok(v) => Ok(v),
Err(e) => {
settled(settle().await?)?;
Err(e)
}
}
}
impl Deref for LapinChannel {
type Target = lapin::Channel;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl DerefMut for LapinChannel {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum VldLapinError {
Validation(vld::error::VldError),
Serialization(String),
Deserialization(String),
Lapin(lapin::Error),
NotSettled,
}
impl fmt::Display for VldLapinError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
VldLapinError::Validation(e) => write!(f, "Validation error: {e}"),
VldLapinError::Serialization(e) => write!(f, "Serialization error: {e}"),
VldLapinError::Deserialization(e) => write!(f, "Deserialization error: {e}"),
VldLapinError::Lapin(e) => write!(f, "Lapin error: {e}"),
VldLapinError::NotSettled => {
write!(f, "Delivery not settled: already settled or channel closed")
}
}
}
}
impl std::error::Error for VldLapinError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
VldLapinError::Validation(e) => Some(e),
VldLapinError::Lapin(e) => Some(e),
VldLapinError::Serialization(_)
| VldLapinError::Deserialization(_)
| VldLapinError::NotSettled => None,
}
}
}
impl From<vld::error::VldError> for VldLapinError {
fn from(value: vld::error::VldError) -> Self {
Self::Validation(value)
}
}
impl From<lapin::Error> for VldLapinError {
fn from(value: lapin::Error) -> Self {
Self::Lapin(value)
}
}
pub mod prelude {
pub use super::{LapinChannel, VldLapinError};
pub use vld::prelude::*;
}