diff --git a/Cargo.toml b/Cargo.toml index 4da3f2a..fa441c1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sonic-channel" -version = "0.1.0-rc3" +version = "0.1.0" authors = ["Dmitriy Pleshevskiy "] description = "Rust client for sonic search backend" categories = ["api-bindings"] diff --git a/README.md b/README.md index af14eac..d383e5d 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ version = "0.1.0" authors = ["Me "] [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(()) diff --git a/src/channel.rs b/src/channel.rs index e9a0534..d1f29c5 100644 --- a/src/channel.rs +++ b/src/channel.rs @@ -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(addr: A) -> 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 write(&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(&mut self, mode: ChannelMode, password: S) -> Result<()> { + fn connect(addr: A) -> 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(&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(mode: ChannelMode, addr: A, password: S) -> Result + 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(); } diff --git a/src/commands/start.rs b/src/commands/start.rs index f0e8e71..a8337b8 100644 --- a/src/commands/start.rs +++ b/src/commands/start.rs @@ -19,6 +19,7 @@ pub struct StartCommand { pub password: String, } +#[derive(Debug)] pub struct StartCommandResponse { pub protocol_version: usize, pub max_buffer_size: usize, diff --git a/src/lib.rs b/src/lib.rs index e66a917..98ed97d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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;