doc: add docs for commands and channel

This commit is contained in:
Dmitriy Pleshevskiy 2020-07-26 23:36:07 +03:00
parent 3e36f9b3ef
commit e1e9e803eb
4 changed files with 72 additions and 4 deletions

View file

@ -12,6 +12,7 @@ const BUFFER_LINE_SEPARATOR: u8 = '\n' as u8;
macro_rules! init_commands { macro_rules! init_commands {
( (
$( $(
$(#[$outer:meta])*
use $cmd_name:ident use $cmd_name:ident
for fn $fn_name:ident for fn $fn_name:ident
$(<$($lt:lifetime)+>)? ( $(<$($lt:lifetime)+>)? (
@ -19,15 +20,25 @@ macro_rules! init_commands {
); );
)* )*
) => { ) => {
$(init_commands!(use $cmd_name for fn $fn_name $(<$($lt)+>)? ($($args)*));)* $(
init_commands!(
$(#[$outer])*
use $cmd_name
for fn $fn_name $(<$($lt)+>)? (
$($args)*
)
);
)*
}; };
( (
$(#[$outer:meta])*
use $cmd_name:ident use $cmd_name:ident
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)?,)*
) )
) => { ) => {
$(#[$outer])*
pub fn $fn_name $(<$($lt)+>)? ( pub fn $fn_name $(<$($lt)+>)? (
&self, &self,
$($arg_name: $arg_type),* $($arg_name: $arg_type),*
@ -40,19 +51,24 @@ macro_rules! init_commands {
}; };
} }
/// Channel modes supported by sonic search backend.
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum ChannelMode { pub enum ChannelMode {
/// Search mode
#[cfg(feature = "search")] #[cfg(feature = "search")]
Search, Search,
/// Ingest mode
#[cfg(feature = "ingest")] #[cfg(feature = "ingest")]
Ingest, Ingest,
/// Control mode
#[cfg(feature = "control")] #[cfg(feature = "control")]
Control, Control,
} }
impl ChannelMode { impl ChannelMode {
/// Converts enum to &str
pub fn to_str(&self) -> &str { pub fn to_str(&self) -> &str {
match self { match self {
#[cfg(feature = "search")] #[cfg(feature = "search")]
@ -73,6 +89,10 @@ impl fmt::Display for ChannelMode {
} }
} }
/// Root and Heart of this library.
///
/// You can connect to the sonic search backend and run all supported protocol methods.
///
#[derive(Debug)] #[derive(Debug)]
pub struct SonicChannel { pub struct SonicChannel {
stream: TcpStream, stream: TcpStream,
@ -152,8 +172,16 @@ impl SonicChannel {
Ok(()) Ok(())
} }
// I think we shouldn't separate commands connect and start because we haven't /// Connect to the search backend in chosen mode.
// possibility to change channel in sonic server, if we already chosen one of them. 🤔 ///
/// I think we shouldn't separate commands connect and start because we haven't
/// possibility to change channel in sonic server, if we already chosen one of them. 🤔
///
/// ```rust
/// use sonic_channel::*;
///
/// SonicChannel::connect_with_start(ChannelMode::Search, "localhost:1491", "SecretPassword");
/// ```
pub fn connect_with_start<A, S>(mode: ChannelMode, addr: A, password: S) -> Result<Self> pub fn connect_with_start<A, S>(mode: ChannelMode, addr: A, password: S) -> Result<Self>
where where
A: ToSocketAddrs, A: ToSocketAddrs,
@ -164,13 +192,18 @@ impl SonicChannel {
Ok(channel) Ok(channel)
} }
// TODO: add examples for commands.
init_commands! { init_commands! {
#[doc="Stop connection."]
use QuitCommand for fn quit(); use QuitCommand for fn quit();
#[doc="Ping server."]
use PingCommand for fn ping(); use PingCommand for fn ping();
} }
#[cfg(feature = "ingest")] #[cfg(feature = "ingest")]
init_commands! { init_commands! {
#[doc="Push search data in the index."]
use PushCommand for fn push<'a>( use PushCommand for fn push<'a>(
collection: &'a str, collection: &'a str,
bucket: &'a str, bucket: &'a str,
@ -178,6 +211,7 @@ impl SonicChannel {
text: &'a str, text: &'a str,
); );
#[doc="Push search data in the index with locale parameter in ISO 639-3 code."]
use PushCommand for fn push_with_locale<'a>( use PushCommand for fn push_with_locale<'a>(
collection: &'a str, collection: &'a str,
bucket: &'a str, bucket: &'a str,
@ -186,15 +220,18 @@ impl SonicChannel {
locale: &'a str => Some(locale), locale: &'a str => Some(locale),
); );
#[doc="Flush all indexed data from collections."]
use FlushCommand for fn flushc<'a>( use FlushCommand for fn flushc<'a>(
collection: &'a str, collection: &'a str,
); );
#[doc="Flush all indexed data from bucket in a collection."]
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),
); );
#[doc="Flush all indexed data from an object in a bucket in collection."]
use FlushCommand for fn flusho<'a>( use FlushCommand for fn flusho<'a>(
collection: &'a str, collection: &'a str,
bucket: &'a str => Some(bucket), bucket: &'a str => Some(bucket),
@ -204,12 +241,14 @@ impl SonicChannel {
#[cfg(feature = "search")] #[cfg(feature = "search")]
init_commands! { init_commands! {
#[doc="Query objects in database."]
use QueryCommand for fn query<'a>( use QueryCommand for fn query<'a>(
collection: &'a str, collection: &'a str,
bucket: &'a str, bucket: &'a str,
terms: &'a str, terms: &'a str,
); );
#[doc="Query limited objects in database."]
use QueryCommand for fn query_with_limit<'a>( use QueryCommand for fn query_with_limit<'a>(
collection: &'a str, collection: &'a str,
bucket: &'a str, bucket: &'a str,
@ -217,6 +256,7 @@ impl SonicChannel {
limit: usize => Some(limit), limit: usize => Some(limit),
); );
#[doc="Query limited objects with offset in database."]
use QueryCommand for fn query_with_limit_and_offset<'a>( use QueryCommand for fn query_with_limit_and_offset<'a>(
collection: &'a str, collection: &'a str,
bucket: &'a str, bucket: &'a str,
@ -225,12 +265,14 @@ impl SonicChannel {
offset: usize => Some(offset), offset: usize => Some(offset),
); );
#[doc="Suggest auto-completes words."]
use SuggestCommand for fn suggest<'a>( use SuggestCommand for fn suggest<'a>(
collection: &'a str, collection: &'a str,
bucket: &'a str, bucket: &'a str,
word: &'a str, word: &'a str,
); );
#[doc="Suggest auto-completes words with limit."]
use SuggestCommand for fn suggest_with_limit<'a>( use SuggestCommand for fn suggest_with_limit<'a>(
collection: &'a str, collection: &'a str,
bucket: &'a str, bucket: &'a str,

View file

@ -3,6 +3,7 @@ use crate::result::{Error, ErrorKind, Result};
use regex::Regex; use regex::Regex;
const RE_QUERY_RECEIVED_MESSAGE: &str = r"^RESULT (?P<flush_count>\d+)\r\n$"; const RE_QUERY_RECEIVED_MESSAGE: &str = r"^RESULT (?P<flush_count>\d+)\r\n$";
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct FlushCommand<'a> { pub struct FlushCommand<'a> {
pub collection: &'a str, pub collection: &'a str,

View file

@ -62,6 +62,8 @@ compile_error!(r#"Either features "ingest" or "search" or "control" must be enab
mod channel; mod channel;
mod commands; mod commands;
/// Contains sonic channel error type and custom Result type for easy configure your functions.
pub mod result; pub mod result;
pub use channel::*; pub use channel::*;

View file

@ -1,8 +1,12 @@
use std::error::Error as StdError; use std::error::Error as StdError;
use std::fmt; use std::fmt;
/// Sugar if you expect only sonic-channel error type in result
pub type Result<T> = std::result::Result<T, Error>; pub type Result<T> = std::result::Result<T, Error>;
/// Wrap for sonic channel error kind. This type has std::error::Error
/// implementation and you can use boxed trait for catch other errors
/// like this.
#[derive(Debug)] #[derive(Debug)]
pub struct Error { pub struct Error {
kind: ErrorKind, kind: ErrorKind,
@ -11,18 +15,37 @@ pub struct Error {
impl StdError for Error {} impl StdError for Error {}
impl Error { impl Error {
/// Creates new Error with sonic channel error kind
///
/// ```rust
/// use sonic_channel::result::*;
///
/// let err = Error::new(ErrorKind::ConnectToServer);
/// ```
pub fn new(kind: ErrorKind) -> Self { pub fn new(kind: ErrorKind) -> Self {
Error { kind } Error { kind }
} }
} }
/// All error kinds that you can see in sonic-channel crate.
#[derive(Debug)] #[derive(Debug)]
pub enum ErrorKind { pub enum ErrorKind {
/// Cannot connect to the sonic search backend.
ConnectToServer, ConnectToServer,
/// Cannot write message to stream.
WriteToStream, WriteToStream,
/// Cannot read message in stream.
ReadStream, ReadStream,
/// Cannot switch channel mode from uninitialized.
SwitchMode, SwitchMode,
/// Cannot run command in current mode.
RunCommand, RunCommand,
/// Error in query response with additional message.
QueryResponseError(&'static str), QueryResponseError(&'static str),
} }