diff --git a/README.md b/README.md index d54439d..5b0bf1e 100644 --- a/README.md +++ b/README.md @@ -20,9 +20,13 @@ Example ------- ```rust -use ssip_client::{new_default_fifo_client, ClientName}; -let mut client = new_default_fifo_client(&ClientName::new("joe", "hello"), None)?; -let msg_id = client.say_line("hello")?; +use ssip_client::{new_default_fifo_client, ClientName, OK_CLIENT_NAME_SET}; + +let mut client = new_default_fifo_client(None)?; +client + .open(ClientName::new("joe", "hello"))? + .check_status(OK_CLIENT_NAME_SET)?; +let msg_id = client.speak()?.send_line("hello")?.receive_message_id()?; client.quit()?; ``` diff --git a/examples/async-mio.rs b/examples/async-mio.rs index b2790e5..7e2a238 100644 --- a/examples/async-mio.rs +++ b/examples/async-mio.rs @@ -15,7 +15,7 @@ fn main() -> ClientResult<()> { for event in &events { if event.token() == token && event.is_writable() { println!("opening client"); - match client.open(ClientName::new("joe", "hello")) { + match client.set_client_name(ClientName::new("joe", "hello")) { Ok(()) => { is_opened = true; break; diff --git a/examples/hello.rs b/examples/hello.rs index b0a50f7..29c8dbe 100644 --- a/examples/hello.rs +++ b/examples/hello.rs @@ -1,10 +1,10 @@ -use ssip_client::{new_default_fifo_client, ClientName, ClientResult, OK_CLIENT_NAME_SET}; +use ssip_client::{new_default_fifo_client, ClientName, ClientResult}; fn main() -> ClientResult<()> { let mut client = new_default_fifo_client(None)?; client - .open(ClientName::new("joe", "hello"))? - .check_status(OK_CLIENT_NAME_SET)?; + .set_client_name(ClientName::new("joe", "hello"))? + .check_client_name_set()?; let msg_id = client.speak()?.send_line("hello")?.receive_message_id()?; println!("message: {}", msg_id); client.quit()?; diff --git a/examples/list.rs b/examples/list.rs index 55beeda..e1dd921 100644 --- a/examples/list.rs +++ b/examples/list.rs @@ -1,6 +1,5 @@ use ssip_client::{ - ClientName, ClientResult, SynthesisVoice, OK_CLIENT_NAME_SET, OK_OUTPUT_MODULES_LIST_SENT, - OK_VOICES_LIST_SENT, + ClientName, ClientResult, SynthesisVoice, OK_OUTPUT_MODULES_LIST_SENT, OK_VOICES_LIST_SENT, }; fn voice_to_string(voice: &SynthesisVoice) -> String { @@ -23,8 +22,8 @@ fn print_list(title: &str, values: &[String]) { fn main() -> ClientResult<()> { let mut client = ssip_client::new_default_fifo_client(None)?; client - .open(ClientName::new("joe", "list"))? - .check_status(OK_CLIENT_NAME_SET)?; + .set_client_name(ClientName::new("joe", "list"))? + .check_client_name_set()?; const OUTPUT_MODULE_TITLE: &str = "output modules"; let modules = client diff --git a/examples/notifications.rs b/examples/notifications.rs index cce9ff7..09cf9c2 100644 --- a/examples/notifications.rs +++ b/examples/notifications.rs @@ -1,10 +1,10 @@ -use ssip_client::{ClientName, ClientResult, EventType, NotificationType, OK_CLIENT_NAME_SET}; +use ssip_client::{ClientName, ClientResult, EventType, NotificationType}; fn main() -> ClientResult<()> { let mut client = ssip_client::new_default_fifo_client(None)?; client - .open(ClientName::new("joe", "notifications"))? - .check_status(OK_CLIENT_NAME_SET)?; + .set_client_name(ClientName::new("joe", "notifications"))? + .check_client_name_set()?; client.enable_notification(NotificationType::All).unwrap(); let msg_id = client.speak()?.send_line("hello")?.receive_message_id()?; println!("message: {}", msg_id); diff --git a/src/client.rs b/src/client.rs index 7cacea4..1955db9 100644 --- a/src/client.rs +++ b/src/client.rs @@ -11,7 +11,7 @@ use std::io::{self, Read, Write}; use std::str::FromStr; use thiserror::Error as ThisError; -use crate::constants::{OK_MESSAGE_QUEUED, OK_VOICES_LIST_SENT}; +use crate::constants::*; use crate::protocol::{send_lines, write_lines}; use crate::types::{ CapitalLettersRecognitionMode, ClientScope, Event, KeyName, MessageId, MessageScope, @@ -189,6 +189,7 @@ impl Client { }) } + /// Return the only string in the list or an error if there is no line or too many. fn parse_single_value(lines: &[String]) -> ClientResult { match lines.len() { 0 => Err(ClientError::NoLine), @@ -197,7 +198,8 @@ impl Client { } } - pub fn open(&mut self, client_name: ClientName) -> ClientResult<&mut Client> { + /// Set the client name. It must be the first call on startup. + pub fn set_client_name(&mut self, client_name: ClientName) -> ClientResult<&mut Client> { send_lines( &mut self.output, &[format!( @@ -535,6 +537,12 @@ impl Client { }) } + /// Check the result of `set_client_name`. + pub fn check_client_name_set(&mut self) -> ClientResult<&mut Client> { + self.check_status(OK_CLIENT_NAME_SET) + } + + /// Register the socket for polling. #[cfg(feature = "metal-io")] pub fn register(&mut self, poll: &mio::Poll, token: mio::Token) -> ClientResult<()> { poll.registry().register( diff --git a/src/lib.rs b/src/lib.rs index b193490..c7420a2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,8 +19,8 @@ //! use ssip_client::{new_default_fifo_client, ClientName, OK_CLIENT_NAME_SET}; //! let mut client = new_default_fifo_client(None)?; //! client -//! .open(ClientName::new("joe", "hello"))? -//! .check_status(OK_CLIENT_NAME_SET)?; +//! .set_client_name(ClientName::new("joe", "hello"))? +//! .check_client_name_set()?; //! let msg_id = client.speak()?.send_line("hello")?.receive_message_id()?; //! client.quit()?; //! # Ok::<(), ssip_client::ClientError>(()) diff --git a/tests/fifo_client_tests.rs b/tests/fifo_client_tests.rs index 6031834..efbd409 100644 --- a/tests/fifo_client_tests.rs +++ b/tests/fifo_client_tests.rs @@ -111,9 +111,9 @@ where let handle = Server::run(&server_path, communication); let mut client = new_fifo_client(&server_path).unwrap(); client - .open(ClientName::new("test", "test")) + .set_client_name(ClientName::new("test", "test")) .unwrap() - .check_status(OK_CLIENT_NAME_SET) + .check_client_name_set() .unwrap(); process_wrapper(&mut client).unwrap(); handle.join().unwrap()