use crate::{Counter, Result};
use async_nats::jetstream::{self, stream::Stream};
use std::future::Future;
pub trait CounterExt {
fn get_counter(&self, name: &str) -> impl Future<Output = Result<Counter>> + Send;
fn counter_from_stream(&self, stream: Stream) -> impl Future<Output = Result<Counter>> + Send;
}
impl CounterExt for jetstream::Context {
#[allow(clippy::manual_async_fn)]
fn get_counter(&self, name: &str) -> impl Future<Output = Result<Counter>> + Send {
let context = self.clone();
let name = name.to_string();
async move {
let stream = context.get_stream(&name).await.map_err(|e| {
crate::errors::CounterError::with_source(
crate::errors::CounterErrorKind::GetStream,
e,
)
})?;
Counter::from_stream(context, stream).await
}
}
fn counter_from_stream(&self, stream: Stream) -> impl Future<Output = Result<Counter>> + Send {
Counter::from_stream(self.clone(), stream)
}
}