2016-03-07 21:19:35 +00:00
|
|
|
use std::io::{Read, Write};
|
|
|
|
use IntoRawMode;
|
2016-03-07 17:42:11 +00:00
|
|
|
|
|
|
|
/// Extension to `Read` trait.
|
|
|
|
pub trait ReadExt {
|
|
|
|
/// Read a password.
|
2016-03-07 21:19:35 +00:00
|
|
|
fn read_passwd<W: Write>(&mut self, writer: &mut W) -> Option<String>;
|
2016-03-07 17:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<R: Read> ReadExt for R {
|
2016-03-07 21:19:35 +00:00
|
|
|
fn read_passwd<W: Write>(&mut self, writer: &mut W) -> Option<String> {
|
|
|
|
let _raw = if let Ok(x) = writer.into_raw_mode() {
|
|
|
|
x
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
};
|
2016-03-07 17:42:11 +00:00
|
|
|
let mut string = String::with_capacity(30);
|
|
|
|
|
|
|
|
for c in self.chars() {
|
|
|
|
match if let Ok(c) = c {
|
|
|
|
c
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
} {
|
|
|
|
'\x00' | '\x03' | '\x04' => return None,
|
|
|
|
'\r' => return Some(string),
|
|
|
|
b => string.push(b),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(string)
|
|
|
|
}
|
|
|
|
}
|