feat: add mode condition for channel commands

This commit is contained in:
Dmitriy Pleshevskiy 2020-08-07 14:49:27 +03:00
parent be6a395e1b
commit 8a0d3f9772
2 changed files with 56 additions and 22 deletions

View file

@ -16,7 +16,8 @@ macro_rules! init_commands {
for fn $fn_name:ident for fn $fn_name:ident
$(<$($lt:lifetime)+>)? ( $(<$($lt:lifetime)+>)? (
$($args:tt)* $($args:tt)*
); )
$(where mode is $condition:expr)?;
)* )*
) => { ) => {
$( $(
@ -26,6 +27,7 @@ macro_rules! init_commands {
for fn $fn_name $(<$($lt)+>)? ( for fn $fn_name $(<$($lt)+>)? (
$($args)* $($args)*
) )
$(where $condition)?
); );
)* )*
}; };
@ -36,6 +38,7 @@ macro_rules! init_commands {
for fn $fn_name:ident $(<$($lt:lifetime)+>)? ( for fn $fn_name:ident $(<$($lt:lifetime)+>)? (
$($arg_name:ident : $arg_type:ty $( => $arg_value:expr)?,)* $($arg_name:ident : $arg_type:ty $( => $arg_value:expr)?,)*
) )
$(where $condition:expr)?
) => { ) => {
$(#[$outer])* $(#[$outer])*
pub fn $fn_name $(<$($lt)+>)? ( pub fn $fn_name $(<$($lt)+>)? (
@ -44,6 +47,17 @@ macro_rules! init_commands {
) -> $crate::result::Result< ) -> $crate::result::Result<
<$cmd_name as $crate::commands::StreamCommand>::Response, <$cmd_name as $crate::commands::StreamCommand>::Response,
> { > {
$(
let mode = self.mode.clone();
if mode != Some($condition) {
return Err(Error::new(
ErrorKind::UnsupportedCommand((
stringify!($fn_name),
mode,
))
));
}
)?
let command = $cmd_name { $($arg_name $(: $arg_value)?,)* ..Default::default() }; let command = $cmd_name { $($arg_name $(: $arg_value)?,)* ..Default::default() };
self.run_command(command) self.run_command(command)
} }
@ -51,7 +65,7 @@ macro_rules! init_commands {
} }
/// Channel modes supported by sonic search backend. /// Channel modes supported by sonic search backend.
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy, PartialEq)]
pub enum ChannelMode { pub enum ChannelMode {
/// Sonic server search channel mode. /// Sonic server search channel mode.
/// ///
@ -287,7 +301,7 @@ impl SonicChannel {
bucket: &'a str, bucket: &'a str,
object: &'a str, object: &'a str,
text: &'a str, text: &'a str,
); ) where mode is ChannelMode::Ingest;
#[doc=r#" #[doc=r#"
Push search data in the index with locale parameter in ISO 639-3 code. Push search data in the index with locale parameter in ISO 639-3 code.
@ -322,7 +336,7 @@ impl SonicChannel {
object: &'a str, object: &'a str,
text: &'a str, text: &'a str,
locale: &'a str => Some(locale), locale: &'a str => Some(locale),
); ) where mode is ChannelMode::Ingest;
#[doc=r#" #[doc=r#"
Pop search data from the index. Returns removed words count as usize type. Pop search data from the index. Returns removed words count as usize type.
@ -350,7 +364,7 @@ impl SonicChannel {
bucket: &'a str, bucket: &'a str,
object: &'a str, object: &'a str,
text: &'a str, text: &'a str,
); ) where mode is ChannelMode::Ingest;
#[doc=r#" #[doc=r#"
Flush all indexed data from collections. Flush all indexed data from collections.
@ -375,7 +389,7 @@ impl SonicChannel {
"#] "#]
use FlushCommand for fn flushc<'a>( use FlushCommand for fn flushc<'a>(
collection: &'a str, collection: &'a str,
); ) where mode is ChannelMode::Ingest;
#[doc=r#" #[doc=r#"
Flush all indexed data from bucket in a collection. Flush all indexed data from bucket in a collection.
@ -401,7 +415,7 @@ impl SonicChannel {
use FlushCommand for fn flushb<'a>( use FlushCommand for fn flushb<'a>(
collection: &'a str, collection: &'a str,
bucket: &'a str => Some(bucket), bucket: &'a str => Some(bucket),
); ) where mode is ChannelMode::Ingest;
#[doc=r#" #[doc=r#"
Flush all indexed data from an object in a bucket in collection. Flush all indexed data from an object in a bucket in collection.
@ -428,7 +442,7 @@ impl SonicChannel {
collection: &'a str, collection: &'a str,
bucket: &'a str => Some(bucket), bucket: &'a str => Some(bucket),
object: &'a str => Some(object), object: &'a str => Some(object),
); ) where mode is ChannelMode::Ingest;
#[doc=r#" #[doc=r#"
Bucket count in indexed search data of your collection. Bucket count in indexed search data of your collection.
@ -453,7 +467,7 @@ impl SonicChannel {
"#] "#]
use CountCommand for fn bucket_count<'a>( use CountCommand for fn bucket_count<'a>(
collection: &'a str, collection: &'a str,
); ) where mode is ChannelMode::Ingest;
#[doc=r#" #[doc=r#"
Object count of bucket in indexed search data. Object count of bucket in indexed search data.
@ -479,7 +493,7 @@ impl SonicChannel {
use CountCommand for fn object_count<'a>( use CountCommand for fn object_count<'a>(
collection: &'a str, collection: &'a str,
bucket: &'a str => Some(bucket), bucket: &'a str => Some(bucket),
); ) where mode is ChannelMode::Ingest;
#[doc=r#" #[doc=r#"
Object word count in indexed bucket search data. Object word count in indexed bucket search data.
@ -506,7 +520,7 @@ impl SonicChannel {
collection: &'a str, collection: &'a str,
bucket: &'a str => Some(bucket), bucket: &'a str => Some(bucket),
object: &'a str => Some(object), object: &'a str => Some(object),
); ) where mode is ChannelMode::Ingest;
} }
#[cfg(feature = "search")] #[cfg(feature = "search")]
@ -536,7 +550,7 @@ impl SonicChannel {
collection: &'a str, collection: &'a str,
bucket: &'a str, bucket: &'a str,
terms: &'a str, terms: &'a str,
); ) where mode is ChannelMode::Search;
#[doc=r#" #[doc=r#"
Query limited objects in database. This method similar query but Query limited objects in database. This method similar query but
@ -570,7 +584,7 @@ impl SonicChannel {
bucket: &'a str, bucket: &'a str,
terms: &'a str, terms: &'a str,
limit: usize => Some(limit), limit: usize => Some(limit),
); ) where mode is ChannelMode::Search;
#[doc=r#" #[doc=r#"
Query limited objects in database. This method similar Query limited objects in database. This method similar
@ -606,7 +620,7 @@ impl SonicChannel {
terms: &'a str, terms: &'a str,
limit: usize => Some(limit), limit: usize => Some(limit),
offset: usize => Some(offset), offset: usize => Some(offset),
); ) where mode is ChannelMode::Search;
#[doc=r#" #[doc=r#"
Suggest auto-completes words. Suggest auto-completes words.
@ -633,7 +647,7 @@ impl SonicChannel {
collection: &'a str, collection: &'a str,
bucket: &'a str, bucket: &'a str,
word: &'a str, word: &'a str,
); ) where mode is ChannelMode::Search;
#[doc=r#" #[doc=r#"
Suggest auto-completes words with limit. Suggest auto-completes words with limit.
@ -661,7 +675,7 @@ impl SonicChannel {
bucket: &'a str, bucket: &'a str,
word: &'a str, word: &'a str,
limit: usize => Some(limit), limit: usize => Some(limit),
); ) where mode is ChannelMode::Search;
} }
#[cfg(feature = "control")] #[cfg(feature = "control")]
@ -688,7 +702,8 @@ impl SonicChannel {
# } # }
``` ```
"#] "#]
use TriggerCommand for fn consolidate(); use TriggerCommand for fn consolidate()
where mode is ChannelMode::Control;
#[doc=r#" #[doc=r#"
Backup KV + FST to <path>/<BACKUP_{KV/FST}_PATH> Backup KV + FST to <path>/<BACKUP_{KV/FST}_PATH>
@ -717,7 +732,7 @@ impl SonicChannel {
// It's not action, but my macro cannot support alias for custom argument. // It's not action, but my macro cannot support alias for custom argument.
// TODO: Add alias to macro and rename argument of this function. // TODO: Add alias to macro and rename argument of this function.
action: &'a str => TriggerAction::Backup(action), action: &'a str => TriggerAction::Backup(action),
); ) where mode is ChannelMode::Control;
#[doc=r#" #[doc=r#"
Restore KV + FST from <path> if you already have backup with the same name. Restore KV + FST from <path> if you already have backup with the same name.
@ -744,6 +759,6 @@ impl SonicChannel {
// It's not action, but my macro cannot support alias for custom argument. // It's not action, but my macro cannot support alias for custom argument.
// TODO: Add alias to macro and rename argument of this function. // TODO: Add alias to macro and rename argument of this function.
action: &'a str => TriggerAction::Restore(action), action: &'a str => TriggerAction::Restore(action),
); ) where mode is ChannelMode::Control;
} }
} }

View file

@ -1,3 +1,4 @@
use crate::channel::ChannelMode;
use std::error::Error as StdError; use std::error::Error as StdError;
use std::fmt; use std::fmt;
@ -51,6 +52,9 @@ pub enum ErrorKind {
/// Response from sonic server are wrong! Actually it may happen if you use /// Response from sonic server are wrong! Actually it may happen if you use
/// unsupported sonic backend version. Please write issue to the github repo. /// unsupported sonic backend version. Please write issue to the github repo.
WrongSonicResponse, WrongSonicResponse,
/// You cannot run the command in current channel.
UnsupportedCommand((&'static str, Option<ChannelMode>)),
} }
impl fmt::Display for Error { impl fmt::Display for Error {
@ -67,6 +71,21 @@ impl fmt::Display for Error {
ErrorKind::WrongSonicResponse => { ErrorKind::WrongSonicResponse => {
write!(f, "Sonic response are wrong. Please write issue to github.") write!(f, "Sonic response are wrong. Please write issue to github.")
} }
ErrorKind::UnsupportedCommand((command_name, channel_mode)) => {
if let Some(channel_mode) = channel_mode {
write!(
f,
"You cannot use `{}` command in {} sonic channel mode",
command_name, channel_mode
)
} else {
write!(
f,
"You need to connect to sonic channel before use {} command",
command_name
)
}
}
} }
} }
} }