diff --git a/src/channel.rs b/src/channel.rs index a3bc96b..0c1b8c5 100644 --- a/src/channel.rs +++ b/src/channel.rs @@ -220,6 +220,21 @@ impl SonicChannel { locale: &'a str => Some(locale), ); + #[doc=r#" + Pop search data from the index. Returns removed words count as u32 type. + + ```rust + let result = ingest_channel.pop("search", "default", "recipe:295", "cake")?; + dbg!(result); + ``` + "#] + use PopCommand for fn pop<'a>( + collection: &'a str, + bucket: &'a str, + object: &'a str, + text: &'a str, + ); + #[doc="Flush all indexed data from collections."] use FlushCommand for fn flushc<'a>( collection: &'a str, diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 0c81332..191d579 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -6,6 +6,8 @@ mod ping; #[cfg(feature = "ingest")] mod flush; #[cfg(feature = "ingest")] +mod pop; +#[cfg(feature = "ingest")] mod push; #[cfg(feature = "search")] @@ -22,6 +24,8 @@ pub use ping::PingCommand; pub use flush::FlushCommand; #[cfg(feature = "ingest")] pub use push::PushCommand; +#[cfg(feature = "ingest")] +pub use pop::PopCommand; #[cfg(feature = "search")] pub use query::QueryCommand; diff --git a/src/commands/pop.rs b/src/commands/pop.rs new file mode 100644 index 0000000..d920ff0 --- /dev/null +++ b/src/commands/pop.rs @@ -0,0 +1,36 @@ +use super::StreamCommand; +use crate::result::{Error, ErrorKind, Result}; + +#[doc(hidden)] +#[derive(Debug, Default)] +pub struct PopCommand<'a> { + pub collection: &'a str, + pub bucket: &'a str, + pub object: &'a str, + pub text: &'a str, +} + +impl StreamCommand for PopCommand<'_> { + type Response = u32; + + fn message(&self) -> String { + let mut message = format!( + r#"POP {} {} {} "{}""#, + self.collection, self.bucket, self.object, self.text + ); + message.push_str("\r\n"); + message + } + + fn receive(&self, message: String) -> Result { + if message.starts_with("RESULT") { + let count = message.split_whitespace().last().unwrap_or_default(); + count + .parse() + .map_err(|_| Error::new(ErrorKind::QueryResponseError("Cannot parse pop count"))) + + } else { + Err(Error::new(ErrorKind::QueryResponseError("Cannot parse result"))) + } + } +}