From 155a5dc1f15156d393a6e643ec0c97cfd5b6a3e0 Mon Sep 17 00:00:00 2001 From: Laurent Pelecq Date: Sun, 3 Apr 2022 19:11:29 +0200 Subject: [PATCH] Add history commands --- README.md | 2 +- src/client.rs | 76 ++++++++++++++++++++++++++++++---------- tests/fifo_sync_tests.rs | 35 ++++++++++++++++-- 3 files changed, 92 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 93012b6..5471197 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ with [mio](https://github.com/tokio-rs/mio). - [x] List, set voices. - [x] Set rate, pitch, volume. - [x] Notifications. -- [ ] Message history. +- [x] Message history. Getting Started --------------- diff --git a/src/client.rs b/src/client.rs index 1ce21ee..428036a 100644 --- a/src/client.rs +++ b/src/client.rs @@ -88,7 +88,7 @@ pub enum Request { HistorySpeak(MessageId), HistorySort(SortDirection, SortKey), HistorySetShortMsgLength(u32), - HistorySetMsgTypeOrdering(Ordering), + HistorySetMsgTypeOrdering(Vec), HistorySearch(ClientScope, String), // Misc. Quit, @@ -300,17 +300,43 @@ impl Client { Request::End => send_one_line!(self, "BLOCK END"), Request::HistoryGetClients => send_one_line!(self, "HISTORY GET CLIENT_LIST"), Request::HistoryGetClientId => send_one_line!(self, "HISTORY GET CLIENT_ID"), - Request::HistoryGetClientMsgs(_scope, _start, _number) => panic!("not implemented"), - Request::HistoryGetLastMsgId => panic!("not implemented"), - Request::HistoryGetMsg(_id) => panic!("not implemented"), - Request::HistoryCursorGet => panic!("not implemented"), - Request::HistoryCursorSet(_scope, _pos) => panic!("not implemented"), - Request::HistoryCursorMove(_direction) => panic!("not implemented"), - Request::HistorySpeak(_id) => panic!("not implemented"), - Request::HistorySort(_direction, _key) => panic!("not implemented"), - Request::HistorySetShortMsgLength(_length) => panic!("not implemented"), - Request::HistorySetMsgTypeOrdering(_ordering) => panic!("not implemented"), - Request::HistorySearch(_scope, _condition) => panic!("not implemented"), + Request::HistoryGetClientMsgs(scope, start, number) => send_one_line!( + self, + "HISTORY GET CLIENT_MESSAGES {} {}_{}", + scope, + start, + number + ), + Request::HistoryGetLastMsgId => send_one_line!(self, "HISTORY GET LAST"), + Request::HistoryGetMsg(id) => send_one_line!(self, "HISTORY GET MESSAGE {}", id), + Request::HistoryCursorGet => send_one_line!(self, "HISTORY CURSOR GET"), + Request::HistoryCursorSet(scope, pos) => { + send_one_line!(self, "HISTORY CURSOR SET {} {}", scope, pos) + } + Request::HistoryCursorMove(direction) => { + send_one_line!(self, "HISTORY CURSOR {}", direction) + } + Request::HistorySpeak(id) => send_one_line!(self, "HISTORY SAY {}", id), + Request::HistorySort(direction, key) => { + send_one_line!(self, "HISTORY SORT {} {}", direction, key) + } + Request::HistorySetShortMsgLength(length) => { + send_one_line!(self, "HISTORY SET SHORT_MESSAGE_LENGTH {}", length) + } + Request::HistorySetMsgTypeOrdering(ordering) => { + send_one_line!( + self, + "HISTORY SET MESSAGE_TYPE_ORDERING \"{}\"", + ordering + .iter() + .map(|x| x.to_string()) + .collect::>() + .join(" ") + ) + } + Request::HistorySearch(scope, condition) => { + send_one_line!(self, "HISTORY SEARCH {} \"{}\"", scope, condition) + } Request::Quit => send_one_line!(self, "QUIT"), }?; Ok(self) @@ -521,6 +547,11 @@ impl Client { self.send(Request::HistoryGetClientId) } + /// Get last message said. + pub fn history_get_last(&mut self) -> ClientResult<&mut Self> { + self.send(Request::HistoryGetLastMsgId) + } + /// Get a range of client messages. pub fn history_get_client_messages( &mut self, @@ -580,7 +611,7 @@ impl Client { } /// Set the ordering of the message types, from the minimum to the maximum. - pub fn history_set_ordering(&mut self, ordering: Ordering) -> ClientResult<&mut Self> { + pub fn history_set_ordering(&mut self, ordering: Vec) -> ClientResult<&mut Self> { self.send(Request::HistorySetMsgTypeOrdering(ordering)) } @@ -722,18 +753,27 @@ impl Client { pub fn receive_u8(&mut self) -> ClientResult { self.receive_string(OK_GET).and_then(|s| { s.parse() - .map_err(|_| ClientError::invalid_data("invalid unsigned integer")) + .map_err(|_| ClientError::invalid_data("invalid unsigned 8-bit integer")) }) } - /// Receive message id - pub fn receive_message_id(&mut self) -> ClientResult { - self.receive_string(OK_MESSAGE_QUEUED).and_then(|s| { + /// Receive cursor pos + pub fn receive_cursor_pos(&mut self) -> ClientResult { + self.receive_string(OK_CUR_POS_RET).and_then(|s| { s.parse() - .map_err(|_| ClientError::invalid_data("invalid message id")) + .map_err(|_| ClientError::invalid_data("invalid unsigned 16-bit integer")) }) } + /// Receive message id + pub fn receive_message_id(&mut self) -> ClientResult { + let mut lines = Vec::new(); + match self.receive_answer(&mut lines)?.code { + OK_MESSAGE_QUEUED | OK_LAST_MSG => Ok(parse_single_integer(&lines)?), + _ => Err(ClientError::invalid_data("not a message id")), + } + } + /// Receive client id pub fn receive_client_id(&mut self) -> ClientResult { self.receive_string(OK_CLIENT_ID_SENT).and_then(|s| { diff --git a/tests/fifo_sync_tests.rs b/tests/fifo_sync_tests.rs index c0bf8f9..c22a25a 100644 --- a/tests/fifo_sync_tests.rs +++ b/tests/fifo_sync_tests.rs @@ -114,20 +114,23 @@ macro_rules! test_setter { } macro_rules! test_getter { - ($getter:ident, $receive:ident, $arg:tt, $question:expr, $answer:expr, $value:expr) => { + ($getter:ident, $get_args:tt, $receive:ident, $recv_arg:tt, $question:expr, $answer:expr, $value:expr) => { #[test] #[cfg(not(feature = "async-mio"))] fn $getter() -> ClientResult<()> { test_client( &[SET_CLIENT_COMMUNICATION, ($question, $answer)], |client| { - let value = client.$getter().unwrap().$receive $arg.unwrap(); + let value = client.$getter $get_args.unwrap().$receive $recv_arg.unwrap(); assert_eq!($value, value); Ok(()) }, ) } }; + ($getter:ident, $receive:ident, $arg:tt, $question:expr, $answer:expr, $value:expr) => { + test_getter!($getter, (), $receive, $arg, $question, $answer, $value); + }; ($getter:ident, $question:expr, $answer:expr, $value:expr) => { test_getter!($getter, receive_string, (OK_GET), $question, $answer, $value); }; @@ -431,3 +434,31 @@ test_getter!( "245-123\r\n245 OK CLIENT ID SENT\r\n", 123 ); + +test_getter!( + history_get_last, + receive_message_id, + (), + "HISTORY GET LAST\r\n", + "242-123\r\n242 OK LAST MSG SAID\r\n", + 123 +); + +test_getter!( + history_get_message, + (123), + receive_string, + (OK_MSG_TEXT_SENT), + "HISTORY GET MESSAGE 123\r\n", + "246-Hello, world!\r\n246 OK MESSAGE SENT\r\n", + "Hello, world!" +); + +test_getter!( + history_get_cursor, + receive_cursor_pos, + (), + "HISTORY CURSOR GET", + "243-42\r\n243 OK CURSOR POSITION RETURNED\r\n", + 42 +);