Add history commands

main
Laurent Pelecq 2 years ago
parent c71698bda8
commit 155a5dc1f1

@ -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
---------------

@ -88,7 +88,7 @@ pub enum Request {
HistorySpeak(MessageId),
HistorySort(SortDirection, SortKey),
HistorySetShortMsgLength(u32),
HistorySetMsgTypeOrdering(Ordering),
HistorySetMsgTypeOrdering(Vec<Ordering>),
HistorySearch(ClientScope, String),
// Misc.
Quit,
@ -300,17 +300,43 @@ impl<S: Read + Write + Source> Client<S> {
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::<Vec<String>>()
.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<S: Read + Write + Source> Client<S> {
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<S: Read + Write + Source> Client<S> {
}
/// 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<Ordering>) -> ClientResult<&mut Self> {
self.send(Request::HistorySetMsgTypeOrdering(ordering))
}
@ -722,18 +753,27 @@ impl<S: Read + Write + Source> Client<S> {
pub fn receive_u8(&mut self) -> ClientResult<u8> {
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<MessageId> {
self.receive_string(OK_MESSAGE_QUEUED).and_then(|s| {
/// Receive cursor pos
pub fn receive_cursor_pos(&mut self) -> ClientResult<u16> {
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<MessageId> {
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<ClientId> {
self.receive_string(OK_CLIENT_ID_SENT).and_then(|s| {

@ -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
);

Loading…
Cancel
Save