diff --git a/async-nats/src/jetstream/stream.rs b/async-nats/src/jetstream/stream.rs index 1ecb5c055..1697f5e30 100644 --- a/async-nats/src/jetstream/stream.rs +++ b/async-nats/src/jetstream/stream.rs @@ -552,11 +552,14 @@ impl Stream { /// let jetstream = async_nats::jetstream::new(client); /// /// let stream = jetstream.get_stream("events").await?; - /// stream.send_purge(async_nats::jetstream::stream::Purge::with_keep(100).filter("events")).await?; + /// stream.send_purge(async_nats::jetstream::stream::Purge::build().keep(100).filter("events")).await?; /// # Ok(()) /// # } /// ``` - pub async fn send_purge(&self, purge: Purge) -> Result { + pub async fn send_purge( + &self, + purge: Purge, + ) -> Result { let request_subject = format!("STREAM.PURGE.{}", self.info.config.name); let response: Response = @@ -1262,39 +1265,78 @@ pub struct External { pub delivery_prefix: Option, } +use std::marker::PhantomData; + +#[derive(Debug, Default)] +pub struct Yes; +#[derive(Debug, Default)] +pub struct No; + +pub trait ToAssign: Debug {} + +impl ToAssign for Yes {} +impl ToAssign for No {} + #[derive(Debug, Default)] -pub struct Purge { +pub struct Purge +where + SEQUENCE: ToAssign, + KEEP: ToAssign, +{ inner: PurgeRequest, + sequence_set: PhantomData, + keep_set: PhantomData, +} + +impl Purge +where + SEQUENCE: ToAssign, + KEEP: ToAssign, +{ + /// Adds subject filter to [Purge] + pub fn filter>(mut self, filter: T) -> Purge { + self.inner.filter = Some(filter.into()); + self + } } -impl Purge { - /// Creates new [Purge]. - pub fn build() -> Self { +impl Purge { + pub fn build() -> Purge { Default::default() } +} + +impl Purge +where + KEEP: ToAssign, +{ /// Creates a new [Purge]. - /// `keep` and `sequence` are exclusive. - pub fn with_keep(keep: u64) -> Self { - Self { + /// `keep` and `sequence` are exclusive, enforced compile time by generics. + pub fn keep(self, keep: u64) -> Purge { + Purge { + sequence_set: PhantomData {}, + keep_set: PhantomData {}, inner: PurgeRequest { keep: Some(keep), - ..Default::default() + ..self.inner }, } } +} +impl Purge +where + SEQUENCE: ToAssign, +{ /// Creates a new [Purge]. - /// `keep` and `sequence` are exclusive. - pub fn with_sequence(sequence: u64) -> Self { - Self { + /// `keep` and `sequence` are exclusive, enforces compile time by generics. + pub fn sequence(self, sequence: u64) -> Purge { + Purge { + sequence_set: PhantomData {}, + keep_set: PhantomData {}, inner: PurgeRequest { sequence: Some(sequence), - ..Default::default() + ..self.inner }, } } - /// Adds subject filter to [Purge] - pub fn filter>(mut self, filter: T) -> Self { - self.inner.filter = Some(filter.into()); - self - } } diff --git a/async-nats/tests/jetstream_tests.rs b/async-nats/tests/jetstream_tests.rs index 62a25ffd4..1d5e89e52 100644 --- a/async-nats/tests/jetstream_tests.rs +++ b/async-nats/tests/jetstream_tests.rs @@ -493,9 +493,12 @@ mod jetstream { } let mut stream = context.get_stream("events").await.unwrap(); - stream.send_purge(Purge::with_sequence(90)).await.unwrap(); + stream + .send_purge(Purge::build().sequence(90)) + .await + .unwrap(); assert_eq!(stream.info().await.unwrap().state.messages, 11); - stream.send_purge(Purge::with_keep(5)).await.unwrap(); + stream.send_purge(Purge::build().keep(5)).await.unwrap(); assert_eq!(stream.info().await.unwrap().state.messages, 5); }