From f8ce518cc5ce30b71af01de7a5e4de60336eace8 Mon Sep 17 00:00:00 2001 From: Alan Evans Date: Sun, 29 Oct 2023 20:03:01 -0400 Subject: [PATCH] Unify the types of built-in and custom word lists. Fixes #15 --- src/main.rs | 47 ++++++++++++++--------------------------------- 1 file changed, 14 insertions(+), 33 deletions(-) diff --git a/src/main.rs b/src/main.rs index edba6d3..b710a10 100644 --- a/src/main.rs +++ b/src/main.rs @@ -95,23 +95,16 @@ fn main() -> Result<(), String> { return Err(error_msg.to_string()); } - // We need two different variables here, one for a user-inputted list and another for - // the built-in list (whether chosen or the default). This is because we use different - // variable types for each case. - let (custom_list, built_in_list) = match opt.custom_list_file_path { - Some(custom_list_file_path) => (Some(read_in_custom_list(&custom_list_file_path)?), None), - None => (None, Some(fetch_list(opt.list_choice))), + match &opt.custom_list_file_path { + Some(custom_list_file_path) => generate_passphrases(&opt, &read_in_custom_list(custom_list_file_path)?), + None => generate_passphrases(&opt, fetch_list(opt.list_choice)), }; - // If a "custom_list" was given by the user, we're going to use that list. - // Otherwise we use the built-in list (a default list if the user didn't choose one). + Ok(()) +} - // To get the length of the list we're going to use, we need to check if a - // custom_list was given. - let list_length = match custom_list { - Some(ref custom_list) => custom_list.len(), - None => built_in_list.unwrap().len(), // pretty sure we're safe to unwrap here... - }; +fn generate_passphrases + std::fmt::Display>(opt: &Args, word_list: &[T]) { + let list_length = word_list.len(); // Since user can define a minimum entropy, we might have to do a little math to // figure out how many words we need to include in this passphrase. @@ -135,26 +128,14 @@ fn main() -> Result<(), String> { // Now we can (finally) generate and print some number of passphrases for _ in 0..opt.n_passphrases { - // Again, we have more code than we should because of this pesky list type situation... - let passphrase = match (&custom_list, built_in_list) { - (Some(ref custom_list), _) => generate_passphrase( - number_of_words_to_put_in_passphrase, - &opt.separator, - opt.title_case, - custom_list, - ), - (None, Some(built_in_list)) => generate_passphrase( - number_of_words_to_put_in_passphrase, - &opt.separator, - opt.title_case, - built_in_list, - ), - (None, None) => return Err("List selection error!".to_string()), - }; + let passphrase = generate_passphrase( + number_of_words_to_put_in_passphrase, + &opt.separator, + opt.title_case, + word_list, + ); println!("{}", passphrase); - } - - Ok(()) + }; } /// Print the calculated (estimated) entropy of a passphrase, based on three variables