|
1 | 1 | // Internal crates |
2 | 2 | use crate::error::{SystemError, WorgenXError}; |
3 | 3 |
|
4 | | -// Extern crates |
| 4 | +// External crates |
5 | 5 | use blake2::{Blake2b512, Blake2s256}; |
6 | 6 | use digest::Digest; |
7 | 7 | use indicatif::{ProgressBar, ProgressStyle}; |
@@ -66,9 +66,10 @@ pub fn get_user_choice_yn() -> String { |
66 | 66 | #[cfg(feature = "gui")] |
67 | 67 | pub fn get_user_choice() -> String { |
68 | 68 | let mut buffer: String = String::new(); |
69 | | - match stdin().read_line(&mut buffer) { |
70 | | - Ok(_) => buffer.trim().to_string(), |
71 | | - Err(_) => String::new(), |
| 69 | + if stdin().read_line(&mut buffer).is_ok() { |
| 70 | + buffer.trim().to_string() |
| 71 | + } else { |
| 72 | + String::new() |
72 | 73 | } |
73 | 74 | } |
74 | 75 |
|
@@ -278,22 +279,20 @@ pub fn get_elapsed_time(start_time: Instant) -> String { |
278 | 279 | /// Ok if the passwords have been written to the file, WorgenXError otherwise. |
279 | 280 | /// |
280 | 281 | pub fn save_passwd_to_file(file: Arc<Mutex<File>>, passwords: String) -> Result<(), WorgenXError> { |
281 | | - let mut file = match file.lock() { |
282 | | - Ok(file) => file, |
283 | | - Err(_) => { |
284 | | - return Err(WorgenXError::SystemError(SystemError::UnableToWriteToFile( |
285 | | - "output file".to_string(), |
286 | | - "Please check the path, the permissions and try again".to_string(), |
287 | | - ))) |
288 | | - } |
289 | | - }; |
290 | | - match file.write_all(format!("{}\n", passwords).as_bytes()) { |
291 | | - Ok(_) => Ok(()), |
292 | | - Err(_) => Err(WorgenXError::SystemError(SystemError::UnableToWriteToFile( |
| 282 | + let mut file = file.lock().map_err(|_| { |
| 283 | + WorgenXError::SystemError(SystemError::UnableToWriteToFile( |
293 | 284 | "output file".to_string(), |
294 | 285 | "Please check the path, the permissions and try again".to_string(), |
295 | | - ))), |
296 | | - } |
| 286 | + )) |
| 287 | + })?; |
| 288 | + |
| 289 | + file.write_all(format!("{}\n", passwords).as_bytes()) |
| 290 | + .map_err(|_| { |
| 291 | + WorgenXError::SystemError(SystemError::UnableToWriteToFile( |
| 292 | + "output file".to_string(), |
| 293 | + "Please check the path, the permissions and try again".to_string(), |
| 294 | + )) |
| 295 | + }) |
297 | 296 | } |
298 | 297 |
|
299 | 298 | /// This function is charged to return the progress used by the program. |
@@ -510,7 +509,7 @@ mod tests { |
510 | 509 | assert_eq!(manage_hash(password.clone(), "sha3-512").unwrap(), "e9a75486736a550af4fea861e2378305c4a555a05094dee1dca2f68afea49cc3a50e8de6ea131ea521311f4d6fb054a146e8282f8e35ff2e6368c1a62e909716"); |
511 | 510 | assert_eq!(manage_hash(password.clone(), "blake2s-256").unwrap(), "4c81099df884bd6e14a639d648bccd808512e48af211ae4f44d545ea6d5e5f2b"); |
512 | 511 | assert_eq!(manage_hash(password.clone(), "blake2b-512").unwrap(), "7c863950ac93c93692995e4732ce1e1466ad74a775352ffbaaf2a4a4ce9b549d0b414a1f3150452be6c7c72c694a7cb46f76452917298d33e67611f0a42addb8"); |
513 | | - assert_eq!(manage_hash(password.clone(), "whirlpool").unwrap(), "74dfc2b27acfa364da55f93a5caee29ccad3557247eda238831b3e9bd931b01d77fe994e4f12b9d4cfa92a124461d2065197d8cf7f33fc88566da2db2a4d6eae"); |
| 512 | + assert_eq!(manage_hash(password.clone(), "whirlpool").unwrap(), "74dfc2b27acfa364da55f93a5caee29ccad3557247eda238831b3e9bd931b01d77fe994e4f12b9d4cfa92a124461d2065197d8cf7f33fc88566da2db2a4d6eae"); |
514 | 513 | assert!(manage_hash(password, "sha999").is_err()); |
515 | 514 | } |
516 | 515 | } |
0 commit comments