diff --git a/src/channel.rs b/src/channel.rs index f784a68..edee493 100644 --- a/src/channel.rs +++ b/src/channel.rs @@ -51,7 +51,7 @@ macro_rules! init_commands { if mode != Some($condition) { return Err(Error::new( ErrorKind::UnsupportedCommand(( - stringify!($fn_name), + stringify!($fn_name), mode, )) )); diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 811320e..a4a25c3 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -40,7 +40,7 @@ pub(crate) use query::QueryCommand; pub(crate) use suggest::SuggestCommand; #[cfg(feature = "control")] -pub(crate) use trigger::{TriggerCommand, TriggerAction}; +pub(crate) use trigger::{TriggerAction, TriggerCommand}; use crate::result::Result; diff --git a/src/commands/pop.rs b/src/commands/pop.rs index 0cecad3..878c31f 100644 --- a/src/commands/pop.rs +++ b/src/commands/pop.rs @@ -24,12 +24,11 @@ impl StreamCommand for PopCommand<'_> { 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( + count.parse().map_err(|_| { + Error::new(ErrorKind::QueryResponseError( "Cannot parse count of pop method response to usize", - ))) - + )) + }) } else { Err(Error::new(ErrorKind::WrongSonicResponse)) } diff --git a/src/commands/query.rs b/src/commands/query.rs index f720db6..5df0340 100644 --- a/src/commands/query.rs +++ b/src/commands/query.rs @@ -43,19 +43,21 @@ impl StreamCommand for QueryCommand<'_> { dbg!(&message); - match RE.captures(&message) { - None => Err(Error::new(ErrorKind::WrongSonicResponse)), - Some(caps) => { - if caps["pending_query_id"] != caps["event_query_id"] { - Err(Error::new(ErrorKind::QueryResponseError( - "Pending id and event id don't match", - ))) - } else if caps["objects"].is_empty() { - Ok(vec![]) - } else { - Ok(caps["objects"].split_whitespace().map(str::to_owned).collect()) - } + if let Some(caps) = RE.captures(&message) { + if caps["pending_query_id"] != caps["event_query_id"] { + Err(Error::new(ErrorKind::QueryResponseError( + "Pending id and event id don't match", + ))) + } else if caps["objects"].is_empty() { + Ok(vec![]) + } else { + Ok(caps["objects"] + .split_whitespace() + .map(str::to_owned) + .collect()) } + } else { + Err(Error::new(ErrorKind::WrongSonicResponse)) } } } diff --git a/src/commands/start.rs b/src/commands/start.rs index e4d069f..b74fa6c 100644 --- a/src/commands/start.rs +++ b/src/commands/start.rs @@ -40,24 +40,23 @@ impl StreamCommand for StartCommand { dbg!(&message); - match RE.captures(&message) { - None => Err(Error::new(ErrorKind::SwitchMode)), - Some(caps) => { - if self.mode.to_str() != &caps["mode"] { - Err(Error::new(ErrorKind::SwitchMode)) - } else { - let protocol_version: usize = - caps["protocol"].parse().expect("Must be digit by regex"); - let max_buffer_size: usize = - caps["buffer_size"].parse().expect("Must be digit by regex"); + if let Some(caps) = RE.captures(&message) { + if self.mode.to_str() != &caps["mode"] { + Err(Error::new(ErrorKind::SwitchMode)) + } else { + let protocol_version: usize = + caps["protocol"].parse().expect("Must be digit by regex"); + let max_buffer_size: usize = + caps["buffer_size"].parse().expect("Must be digit by regex"); - Ok(StartCommandResponse { - protocol_version, - max_buffer_size, - mode: self.mode, - }) - } + Ok(StartCommandResponse { + protocol_version, + max_buffer_size, + mode: self.mode, + }) } + } else { + Err(Error::new(ErrorKind::SwitchMode)) } } } diff --git a/src/commands/suggest.rs b/src/commands/suggest.rs index 0b92e1f..1db0aac 100644 --- a/src/commands/suggest.rs +++ b/src/commands/suggest.rs @@ -49,7 +49,10 @@ impl StreamCommand for SuggestCommand<'_> { } else if caps["words"].is_empty() { Ok(vec![]) } else { - Ok(caps["words"].split_whitespace().map(str::to_owned).collect()) + Ok(caps["words"] + .split_whitespace() + .map(str::to_owned) + .collect()) } } } diff --git a/src/lib.rs b/src/lib.rs index ae929a1..c2ad2d8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,74 +1,74 @@ //! # Sonic Channel //! Rust client for [sonic] search backend. -//! -//! +//! +//! //! ## Example usage -//! +//! //! ### Search channel //! //! Note: This example requires enabling the `search` feature, enabled by default. -//! +//! //! ```rust,no_run //! use sonic_channel::*; -//! +//! //! fn main() -> result::Result<()> { //! let channel = SonicChannel::connect_with_start( //! ChannelMode::Search, //! "localhost:1491", //! "SecretPassword", //! )?; -//! +//! //! let objects = channel.query("collection", "bucket", "recipe")?; //! dbg!(objects); -//! +//! //! Ok(()) //! } //! ``` -//! +//! //! ### Ingest channel -//! +//! //! Note: This example requires enabling the `ingest` feature. //! //! ```rust,no_run //! use sonic_channel::*; -//! +//! //! fn main() -> result::Result<()> { //! let mut channel = SonicChannel::connect_with_start( //! ChannelMode::Ingest, //! "localhost:1491", //! "SecretPassword", //! )?; -//! +//! //! let pushed = channel.push("collection", "bucket", "object:1", "my best recipe")?; //! // or //! // let pushed = channel.push_with_locale("collection", "bucket", "object:1", "Мой лучший рецепт", "rus")?; //! dbg!(pushed); -//! +//! //! Ok(()) //! } //! ``` -//! +//! //! ### Control channel -//! +//! //! Note: This example requires enabling the `control` feature. //! //! ```rust,no_run //! use sonic_channel::*; -//! +//! //! fn main() -> result::Result<()> { //! let mut channel = SonicChannel::connect_with_start( //! ChannelMode::Control, //! "localhost:1491", //! "SecretPassword", //! )?; -//! +//! //! let result = channel.consolidate()?; //! assert_eq!(result, true); -//! +//! //! Ok(()) //! } //! ``` -//! +//! //! [sonic]: https://github.com/valeriansaliou/sonic // Rustc lints. @@ -84,7 +84,9 @@ #![deny(clippy::all)] #[cfg(not(any(feature = "ingest", feature = "search", feature = "control")))] -compile_error!(r#"Either features "ingest" or "search" or "control" must be enabled for "sonic-channel" crate"#); +compile_error!( + r#"Either features "ingest" or "search" or "control" must be enabled for "sonic-channel" crate"# +); mod channel; mod commands;