style: cosmetic changes

This commit is contained in:
Dmitriy Pleshevskiy 2020-10-16 10:49:05 +03:00
parent f750de076e
commit f2f49be0ec
7 changed files with 60 additions and 55 deletions

View file

@ -51,7 +51,7 @@ macro_rules! init_commands {
if mode != Some($condition) { if mode != Some($condition) {
return Err(Error::new( return Err(Error::new(
ErrorKind::UnsupportedCommand(( ErrorKind::UnsupportedCommand((
stringify!($fn_name), stringify!($fn_name),
mode, mode,
)) ))
)); ));

View file

@ -40,7 +40,7 @@ pub(crate) use query::QueryCommand;
pub(crate) use suggest::SuggestCommand; pub(crate) use suggest::SuggestCommand;
#[cfg(feature = "control")] #[cfg(feature = "control")]
pub(crate) use trigger::{TriggerCommand, TriggerAction}; pub(crate) use trigger::{TriggerAction, TriggerCommand};
use crate::result::Result; use crate::result::Result;

View file

@ -24,12 +24,11 @@ impl StreamCommand for PopCommand<'_> {
fn receive(&self, message: String) -> Result<Self::Response> { fn receive(&self, message: String) -> Result<Self::Response> {
if message.starts_with("RESULT ") { if message.starts_with("RESULT ") {
let count = message.split_whitespace().last().unwrap_or_default(); let count = message.split_whitespace().last().unwrap_or_default();
count count.parse().map_err(|_| {
.parse() Error::new(ErrorKind::QueryResponseError(
.map_err(|_| Error::new(ErrorKind::QueryResponseError(
"Cannot parse count of pop method response to usize", "Cannot parse count of pop method response to usize",
))) ))
})
} else { } else {
Err(Error::new(ErrorKind::WrongSonicResponse)) Err(Error::new(ErrorKind::WrongSonicResponse))
} }

View file

@ -43,19 +43,21 @@ impl StreamCommand for QueryCommand<'_> {
dbg!(&message); dbg!(&message);
match RE.captures(&message) { if let Some(caps) = RE.captures(&message) {
None => Err(Error::new(ErrorKind::WrongSonicResponse)), if caps["pending_query_id"] != caps["event_query_id"] {
Some(caps) => { Err(Error::new(ErrorKind::QueryResponseError(
if caps["pending_query_id"] != caps["event_query_id"] { "Pending id and event id don't match",
Err(Error::new(ErrorKind::QueryResponseError( )))
"Pending id and event id don't match", } else if caps["objects"].is_empty() {
))) Ok(vec![])
} else if caps["objects"].is_empty() { } else {
Ok(vec![]) Ok(caps["objects"]
} else { .split_whitespace()
Ok(caps["objects"].split_whitespace().map(str::to_owned).collect()) .map(str::to_owned)
} .collect())
} }
} else {
Err(Error::new(ErrorKind::WrongSonicResponse))
} }
} }
} }

View file

@ -40,24 +40,23 @@ impl StreamCommand for StartCommand {
dbg!(&message); dbg!(&message);
match RE.captures(&message) { if let Some(caps) = RE.captures(&message) {
None => Err(Error::new(ErrorKind::SwitchMode)), if self.mode.to_str() != &caps["mode"] {
Some(caps) => { Err(Error::new(ErrorKind::SwitchMode))
if self.mode.to_str() != &caps["mode"] { } else {
Err(Error::new(ErrorKind::SwitchMode)) let protocol_version: usize =
} else { caps["protocol"].parse().expect("Must be digit by regex");
let protocol_version: usize = let max_buffer_size: usize =
caps["protocol"].parse().expect("Must be digit by regex"); caps["buffer_size"].parse().expect("Must be digit by regex");
let max_buffer_size: usize =
caps["buffer_size"].parse().expect("Must be digit by regex");
Ok(StartCommandResponse { Ok(StartCommandResponse {
protocol_version, protocol_version,
max_buffer_size, max_buffer_size,
mode: self.mode, mode: self.mode,
}) })
}
} }
} else {
Err(Error::new(ErrorKind::SwitchMode))
} }
} }
} }

View file

@ -49,7 +49,10 @@ impl StreamCommand for SuggestCommand<'_> {
} else if caps["words"].is_empty() { } else if caps["words"].is_empty() {
Ok(vec![]) Ok(vec![])
} else { } else {
Ok(caps["words"].split_whitespace().map(str::to_owned).collect()) Ok(caps["words"]
.split_whitespace()
.map(str::to_owned)
.collect())
} }
} }
} }

View file

@ -1,74 +1,74 @@
//! # Sonic Channel //! # Sonic Channel
//! Rust client for [sonic] search backend. //! Rust client for [sonic] search backend.
//! //!
//! //!
//! ## Example usage //! ## Example usage
//! //!
//! ### Search channel //! ### Search channel
//! //!
//! Note: This example requires enabling the `search` feature, enabled by default. //! Note: This example requires enabling the `search` feature, enabled by default.
//! //!
//! ```rust,no_run //! ```rust,no_run
//! use sonic_channel::*; //! use sonic_channel::*;
//! //!
//! fn main() -> result::Result<()> { //! fn main() -> result::Result<()> {
//! let channel = SonicChannel::connect_with_start( //! let channel = SonicChannel::connect_with_start(
//! ChannelMode::Search, //! ChannelMode::Search,
//! "localhost:1491", //! "localhost:1491",
//! "SecretPassword", //! "SecretPassword",
//! )?; //! )?;
//! //!
//! let objects = channel.query("collection", "bucket", "recipe")?; //! let objects = channel.query("collection", "bucket", "recipe")?;
//! dbg!(objects); //! dbg!(objects);
//! //!
//! Ok(()) //! Ok(())
//! } //! }
//! ``` //! ```
//! //!
//! ### Ingest channel //! ### Ingest channel
//! //!
//! Note: This example requires enabling the `ingest` feature. //! Note: This example requires enabling the `ingest` feature.
//! //!
//! ```rust,no_run //! ```rust,no_run
//! use sonic_channel::*; //! use sonic_channel::*;
//! //!
//! fn main() -> result::Result<()> { //! fn main() -> result::Result<()> {
//! let mut channel = SonicChannel::connect_with_start( //! let mut channel = SonicChannel::connect_with_start(
//! ChannelMode::Ingest, //! ChannelMode::Ingest,
//! "localhost:1491", //! "localhost:1491",
//! "SecretPassword", //! "SecretPassword",
//! )?; //! )?;
//! //!
//! let pushed = channel.push("collection", "bucket", "object:1", "my best recipe")?; //! let pushed = channel.push("collection", "bucket", "object:1", "my best recipe")?;
//! // or //! // or
//! // let pushed = channel.push_with_locale("collection", "bucket", "object:1", "Мой лучший рецепт", "rus")?; //! // let pushed = channel.push_with_locale("collection", "bucket", "object:1", "Мой лучший рецепт", "rus")?;
//! dbg!(pushed); //! dbg!(pushed);
//! //!
//! Ok(()) //! Ok(())
//! } //! }
//! ``` //! ```
//! //!
//! ### Control channel //! ### Control channel
//! //!
//! Note: This example requires enabling the `control` feature. //! Note: This example requires enabling the `control` feature.
//! //!
//! ```rust,no_run //! ```rust,no_run
//! use sonic_channel::*; //! use sonic_channel::*;
//! //!
//! fn main() -> result::Result<()> { //! fn main() -> result::Result<()> {
//! let mut channel = SonicChannel::connect_with_start( //! let mut channel = SonicChannel::connect_with_start(
//! ChannelMode::Control, //! ChannelMode::Control,
//! "localhost:1491", //! "localhost:1491",
//! "SecretPassword", //! "SecretPassword",
//! )?; //! )?;
//! //!
//! let result = channel.consolidate()?; //! let result = channel.consolidate()?;
//! assert_eq!(result, true); //! assert_eq!(result, true);
//! //!
//! Ok(()) //! Ok(())
//! } //! }
//! ``` //! ```
//! //!
//! [sonic]: https://github.com/valeriansaliou/sonic //! [sonic]: https://github.com/valeriansaliou/sonic
// Rustc lints. // Rustc lints.
@ -84,7 +84,9 @@
#![deny(clippy::all)] #![deny(clippy::all)]
#[cfg(not(any(feature = "ingest", feature = "search", feature = "control")))] #[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 channel;
mod commands; mod commands;