From b3dd02603a63be760451fa4d936ac47385d627d3 Mon Sep 17 00:00:00 2001 From: Laurent Pelecq Date: Thu, 17 Mar 2022 21:29:24 +0100 Subject: [PATCH] Fix send_char and rename error NoLine to TooFewLines --- src/client.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/client.rs b/src/client.rs index 5beb1db..8549305 100644 --- a/src/client.rs +++ b/src/client.rs @@ -25,12 +25,12 @@ pub enum ClientError { InvalidType, #[error("I/O: {0}")] Io(io::Error), - #[error("No line in result")] - NoLine, #[error("Not ready")] NotReady, #[error("SSIP: {0}")] Ssip(StatusLine), + #[error("Too few lines")] + TooFewLines, #[error("Too many lines")] TooManyLines, #[error("Truncated message")] @@ -56,6 +56,7 @@ pub type ClientResult = Result; pub type ClientStatus = ClientResult; /// Client name +#[derive(Debug)] pub struct ClientName { pub user: String, pub application: String, @@ -177,7 +178,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), + 0 => Err(ClientError::TooFewLines), 1 => Ok(lines[0].to_string()), _ => Err(ClientError::TooManyLines), } @@ -219,7 +220,8 @@ impl Client { /// Send a char pub fn send_char(&mut self, ch: char) -> ClientResult<&mut Client> { - self.send_lines(&[format!("CHAR {}", ch).as_str()]) + send_lines(&mut self.output, &[format!("CHAR {}", ch).as_str()])?; + Ok(self) } /// Send a symbolic key name @@ -540,7 +542,7 @@ impl Client { poll: &mio::Poll, input_token: mio::Token, output_token: mio::Token, - ) -> ClientResult<()> { + ) -> io::Result<()> { poll.registry() .register(self.input.get_mut(), input_token, mio::Interest::READABLE)?; poll.registry() @@ -565,7 +567,7 @@ mod tests { let result = Client::::parse_single_value(&[String::from("one")]).unwrap(); assert_eq!("one", result); let err_empty = Client::::parse_single_value(&[]); - assert!(matches!(err_empty, Err(ClientError::NoLine))); + assert!(matches!(err_empty, Err(ClientError::TooFewLines))); let err_too_many = Client::::parse_single_value(&[String::from("one"), String::from("two")]); assert!(matches!(err_too_many, Err(ClientError::TooManyLines)));