refac: add connect with start command
This commit is contained in:
parent
4a267f66b9
commit
ed0fef1956
5 changed files with 51 additions and 41 deletions
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "sonic-channel"
|
name = "sonic-channel"
|
||||||
version = "0.1.0-rc3"
|
version = "0.1.0"
|
||||||
authors = ["Dmitriy Pleshevskiy <dmitriy@ideascup.me>"]
|
authors = ["Dmitriy Pleshevskiy <dmitriy@ideascup.me>"]
|
||||||
description = "Rust client for sonic search backend"
|
description = "Rust client for sonic search backend"
|
||||||
categories = ["api-bindings"]
|
categories = ["api-bindings"]
|
||||||
|
|
18
README.md
18
README.md
|
@ -18,7 +18,7 @@ version = "0.1.0"
|
||||||
authors = ["Me <user@rust-lang.org>"]
|
authors = ["Me <user@rust-lang.org>"]
|
||||||
|
|
||||||
[dependencies]
|
[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::*;
|
use sonic_channel::*;
|
||||||
|
|
||||||
fn main() -> result::Result<()> {
|
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")?;
|
let objects = channel.query("collection", "bucket", "recipe")?;
|
||||||
dbg!(objects);
|
dbg!(objects);
|
||||||
|
|
||||||
|
@ -46,10 +49,15 @@ fn main() -> result::Result<()> {
|
||||||
use sonic_channel::*;
|
use sonic_channel::*;
|
||||||
|
|
||||||
fn main() -> result::Result<()> {
|
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")?;
|
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);
|
dbg!(pushed);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -49,7 +49,6 @@ pub enum ChannelMode {
|
||||||
|
|
||||||
impl ChannelMode {
|
impl ChannelMode {
|
||||||
pub fn to_str(&self) -> &str {
|
pub fn to_str(&self) -> &str {
|
||||||
#[cfg(any(feature = "ingest", feature = "search", feature = "control"))]
|
|
||||||
match self {
|
match self {
|
||||||
#[cfg(feature = "search")]
|
#[cfg(feature = "search")]
|
||||||
ChannelMode::Search => "search",
|
ChannelMode::Search => "search",
|
||||||
|
@ -60,15 +59,6 @@ impl ChannelMode {
|
||||||
#[cfg(feature = "control")]
|
#[cfg(feature = "control")]
|
||||||
ChannelMode::Control => "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 {
|
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<()> {
|
fn write<SC: StreamCommand>(&self, command: &SC) -> Result<()> {
|
||||||
let mut writer = BufWriter::with_capacity(self.max_buffer_size, &self.stream);
|
let mut writer = BufWriter::with_capacity(self.max_buffer_size, &self.stream);
|
||||||
let message = command.message();
|
let message = command.message();
|
||||||
|
@ -138,8 +108,28 @@ impl SonicChannel {
|
||||||
command.receive(message)
|
command.receive(message)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "ingest", feature = "search", feature = "control"))]
|
fn connect<A: ToSocketAddrs>(addr: A) -> Result<Self> {
|
||||||
pub fn start<S: ToString>(&mut self, mode: ChannelMode, password: S) -> Result<()> {
|
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() {
|
if self.mode.is_some() {
|
||||||
return Err(Error::new(ErrorKind::RunCommand));
|
return Err(Error::new(ErrorKind::RunCommand));
|
||||||
}
|
}
|
||||||
|
@ -157,12 +147,20 @@ impl SonicChannel {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
init_commands! {
|
// I think we shouldn't separate commands connect and start because we haven't
|
||||||
use QuitCommand for fn quit();
|
// 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! {
|
init_commands! {
|
||||||
|
use QuitCommand for fn quit();
|
||||||
use PingCommand for fn ping();
|
use PingCommand for fn ping();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,7 @@ pub struct StartCommand {
|
||||||
pub password: String,
|
pub password: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct StartCommandResponse {
|
pub struct StartCommandResponse {
|
||||||
pub protocol_version: usize,
|
pub protocol_version: usize,
|
||||||
pub max_buffer_size: usize,
|
pub max_buffer_size: usize,
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
#![allow(dead_code)]
|
#![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 channel;
|
||||||
mod commands;
|
mod commands;
|
||||||
pub mod result;
|
pub mod result;
|
||||||
|
|
Loading…
Reference in a new issue