use crate::connection::ConnError;
use crate::connection::Connection;
use crate::frame::Frame;
use futures::stream::Stream;
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::sync::mpsc;
#[derive(Debug, Clone, Default)]
pub struct SubscriptionOptions {
pub headers: Vec<(String, String)>,
pub durable_queue: Option<String>,
}
pub struct Subscription {
id: String,
destination: String,
receiver: mpsc::Receiver<Frame>,
conn: Connection,
}
impl Subscription {
pub(crate) fn new(
id: String,
destination: String,
receiver: mpsc::Receiver<Frame>,
conn: Connection,
) -> Self {
Self {
id,
destination,
receiver,
conn,
}
}
pub fn id(&self) -> &str {
&self.id
}
pub fn destination(&self) -> &str {
&self.destination
}
pub fn into_receiver(self) -> mpsc::Receiver<Frame> {
self.receiver
}
pub async fn ack(&self, message_id: &str) -> Result<(), ConnError> {
self.conn.ack(&self.id, message_id).await
}
pub async fn nack(&self, message_id: &str) -> Result<(), ConnError> {
self.conn.nack(&self.id, message_id).await
}
pub async fn unsubscribe(self) -> Result<(), ConnError> {
self.conn.unsubscribe(&self.id).await
}
}
impl Stream for Subscription {
type Item = Frame;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let this = self.get_mut();
Pin::new(&mut this.receiver).poll_recv(cx)
}
}