refac: add connect with start command

This commit is contained in:
Dmitriy Pleshevskiy 2020-07-23 10:44:02 +03:00
parent 4a267f66b9
commit ed0fef1956
5 changed files with 51 additions and 41 deletions

View file

@ -1,6 +1,6 @@
[package]
name = "sonic-channel"
version = "0.1.0-rc3"
version = "0.1.0"
authors = ["Dmitriy Pleshevskiy <dmitriy@ideascup.me>"]
description = "Rust client for sonic search backend"
categories = ["api-bindings"]

View file

@ -18,7 +18,7 @@ version = "0.1.0"
authors = ["Me <user@rust-lang.org>"]
[dependencies]
sonic-channel = { version = "0.1.0-rc3" }
sonic-channel = { version = "0.1" }
```
@ -30,9 +30,12 @@ sonic-channel = { version = "0.1.0-rc3" }
use sonic_channel::*;
fn main() -> result::Result<()> {
let mut channel = SonicChannel::connect("localhost:1491")?;
let channel = SonicChannel::connect_with_start(
"localhost:1491",
ChannelMode::Search,
"SecretPassword",
)?;
channel.start(ChannelMode::Search, "SecretPassword")?;
let objects = channel.query("collection", "bucket", "recipe")?;
dbg!(objects);
@ -46,10 +49,15 @@ fn main() -> result::Result<()> {
use sonic_channel::*;
fn main() -> result::Result<()> {
let mut channel = SonicChannel::connect("localhost:1491")?;
let mut channel = SonicChannel::connect_with_start(
"localhost:1491",
ChannelMode::Ingest,
"SecretPassword",
)?;
channel.start(ChannelMode::Ingest, "SecretPassword")?;
let pushed = channel.push("collection", "bucket", "user:1", "my best recipe")?;
// or
// let pushed = channel.push_with_locale("collection", "bucket", "user:1", "Мой лучший рецепт", Some("rus"))?;
dbg!(pushed);
Ok(())

View file

@ -49,7 +49,6 @@ pub enum ChannelMode {
impl ChannelMode {
pub fn to_str(&self) -> &str {
#[cfg(any(feature = "ingest", feature = "search", feature = "control"))]
match self {
#[cfg(feature = "search")]
ChannelMode::Search => "search",
@ -60,15 +59,6 @@ impl ChannelMode {
#[cfg(feature = "control")]
ChannelMode::Control => "control",
}
// Actually we'll not see this text because we cannot call this function for enum
// without enum value, but Rust compiler want this case.
#[cfg(all(
not(feature = "ingest"),
not(feature = "search"),
not(feature = "control")
))]
"unitialized"
}
}
@ -87,26 +77,6 @@ pub struct SonicChannel {
}
impl SonicChannel {
pub fn connect<A: ToSocketAddrs>(addr: A) -> Result<Self> {
let stream = TcpStream::connect(addr).map_err(|_| Error::new(ErrorKind::ConnectToServer))?;
let channel = SonicChannel {
stream,
mode: None,
max_buffer_size: UNINITIALIZED_MODE_MAX_BUFFER_SIZE,
protocol_version: DEFAULT_SONIC_PROTOCOL_VERSION,
};
let message = channel.read(1)?;
dbg!(&message);
// TODO: need to add support for versions
if message.starts_with("CONNECTED") {
Ok(channel)
} else {
Err(Error::new(ErrorKind::ConnectToServer))
}
}
fn write<SC: StreamCommand>(&self, command: &SC) -> Result<()> {
let mut writer = BufWriter::with_capacity(self.max_buffer_size, &self.stream);
let message = command.message();
@ -138,8 +108,28 @@ impl SonicChannel {
command.receive(message)
}
#[cfg(any(feature = "ingest", feature = "search", feature = "control"))]
pub fn start<S: ToString>(&mut self, mode: ChannelMode, password: S) -> Result<()> {
fn connect<A: ToSocketAddrs>(addr: A) -> Result<Self> {
let stream =
TcpStream::connect(addr).map_err(|_| Error::new(ErrorKind::ConnectToServer))?;
let channel = SonicChannel {
stream,
mode: None,
max_buffer_size: UNINITIALIZED_MODE_MAX_BUFFER_SIZE,
protocol_version: DEFAULT_SONIC_PROTOCOL_VERSION,
};
let message = channel.read(1)?;
dbg!(&message);
// TODO: need to add support for versions
if message.starts_with("CONNECTED") {
Ok(channel)
} else {
Err(Error::new(ErrorKind::ConnectToServer))
}
}
fn start<S: ToString>(&mut self, mode: ChannelMode, password: S) -> Result<()> {
if self.mode.is_some() {
return Err(Error::new(ErrorKind::RunCommand));
}
@ -157,12 +147,20 @@ impl SonicChannel {
Ok(())
}
init_commands! {
use QuitCommand for fn quit();
// 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. 🤔
pub fn connect_with_start<A, S>(mode: ChannelMode, addr: A, password: S) -> Result<Self>
where
A: ToSocketAddrs,
S: ToString,
{
let mut channel = Self::connect(addr)?;
channel.start(mode, password)?;
Ok(channel)
}
#[cfg(any(feature = "ingest", feature = "search", feature = "control"))]
init_commands! {
use QuitCommand for fn quit();
use PingCommand for fn ping();
}

View file

@ -19,6 +19,7 @@ pub struct StartCommand {
pub password: String,
}
#[derive(Debug)]
pub struct StartCommandResponse {
pub protocol_version: usize,
pub max_buffer_size: usize,

View file

@ -1,5 +1,8 @@
#![allow(dead_code)]
#[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"#);
mod channel;
mod commands;
pub mod result;