Skip to content

Commit

Permalink
Unify the types of built-in and custom word lists.
Browse files Browse the repository at this point in the history
Fixes #15
  • Loading branch information
westonal committed Oct 30, 2023
1 parent 63710a9 commit f8ce518
Showing 1 changed file with 14 additions and 33 deletions.
47 changes: 14 additions & 33 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: AsRef<str> + 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.
Expand All @@ -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
Expand Down

0 comments on commit f8ce518

Please sign in to comment.