From cdd7a302b21dc37db9aa5a793857bde6da5e2161 Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Sun, 20 Mar 2016 16:15:05 +0100 Subject: [PATCH] Refactor read_passwd Line-reading logic is now in its own method --- src/input.rs | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/input.rs b/src/input.rs index 754823e..f6f2662 100644 --- a/src/input.rs +++ b/src/input.rs @@ -80,13 +80,23 @@ pub trait TermRead { #[cfg(feature = "nightly")] fn keys(self) -> Keys> where Self: Sized; + /// Read a line. + /// + /// EOT and ETX will abort the prompt, returning `None`. Newline or carriage return will + /// complete the input. + fn read_line(&mut self) -> io::Result>; + /// Read a password. /// /// EOT and ETX will abort the prompt, returning `None`. Newline or carriage return will - /// complete the password input. - fn read_passwd(&mut self, writer: &mut W) -> io::Result>; + /// complete the input. + fn read_passwd(&mut self, writer: &mut W) -> io::Result> { + let _raw = try!(writer.into_raw_mode()); + self.read_line() + } } + impl TermRead for R { #[cfg(feature = "nightly")] fn keys(self) -> Keys> { @@ -95,22 +105,20 @@ impl TermRead for R { } } - fn read_passwd(&mut self, writer: &mut W) -> io::Result> { - let _raw = try!(writer.into_raw_mode()); - let mut passbuf = Vec::with_capacity(30); + fn read_line(&mut self) -> io::Result> { + let mut buf = Vec::with_capacity(30); for c in self.bytes() { match c { Err(e) => return Err(e), Ok(0) | Ok(3) | Ok(4) => return Ok(None), Ok(b'\n') | Ok(b'\r') => break, - Ok(c) => passbuf.push(c), + Ok(c) => buf.push(c), } } - let passwd = try!(String::from_utf8(passbuf).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))); - - Ok(Some(passwd)) + let string = try!(String::from_utf8(buf).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))); + Ok(Some(string)) } }