Skip to content

Commit

Permalink
Various minor code cleanups (#47)
Browse files Browse the repository at this point in the history
* Various minor code cleanups
  - Fix wrong identation
  - Make error handling more idiomatic

* Remove an unneeded if let
  • Loading branch information
nicokoch committed Jun 18, 2020
1 parent 189c388 commit acff1bd
Showing 1 changed file with 9 additions and 20 deletions.
29 changes: 9 additions & 20 deletions src/lib.rs
Expand Up @@ -107,19 +107,11 @@ mod unix {
Source::Stdin(ref mut stdin) => stdin.read_line(&mut password),
};

// Check the response.
match input {
Ok(_) => {}
Err(err) => {
// Reset the terminal and quit.
io_result(unsafe { tcsetattr(tty_fd, TCSANOW, &term_orig) })?;

return Err(err);
}
};

// Reset the terminal.
io_result(unsafe { tcsetattr(tty_fd, TCSANOW, &term_orig) })?;

// Return if we have an error
input?;
} else {
// If we don't have a TTY, the input was piped so we bypass
// terminal hiding code
Expand Down Expand Up @@ -204,7 +196,7 @@ mod windows {
let handle = source.as_raw_handle();

// Check the response.
let _ = input?;
input?;

if handle_type != FILE_TYPE_PIPE {
// Set the the mode back to normal
Expand All @@ -215,8 +207,8 @@ mod windows {

super::fixes_newline(&mut password);

// Newline for windows which otherwise prints on the same line.
println!();
// Newline for windows which otherwise prints on the same line.
println!();

Ok(password.into_inner())
}
Expand Down Expand Up @@ -255,12 +247,9 @@ pub fn read_password_with_reader<T>(source: Option<T>) -> ::std::io::Result<Stri
match source {
Some(mut reader) => {
let mut password = ZeroOnDrop::new();
if let Err(err) = reader.read_line(&mut password) {
Err(err)
} else {
fixes_newline(&mut password);
Ok(password.into_inner())
}
reader.read_line(&mut password)?;
fixes_newline(&mut password);
Ok(password.into_inner())
},
None => read_password_from_stdin(false),
}
Expand Down

0 comments on commit acff1bd

Please sign in to comment.