Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up main function #15

Closed
sts10 opened this issue Oct 28, 2023 · 0 comments · Fixed by #16
Closed

Clean up main function #15

sts10 opened this issue Oct 28, 2023 · 0 comments · Fixed by #16
Labels
help wanted Extra attention is needed

Comments

@sts10
Copy link
Owner

sts10 commented Oct 28, 2023

As noted in #14, I don't like how the main function looks right now. Specifically, how I need to have one variable for the built-in list and another for the user-submitted "custom" list.

phraze/src/main.rs

Lines 104 to 161 in d530eba

// 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))),
};
// 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).
// 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...
};
// 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.
let number_of_words_to_put_in_passphrase = calculate_number_words_needed(
opt.number_of_words,
opt.minimum_entropy,
opt.strength_count,
list_length,
);
// If user enabled verbose option
if opt.verbose {
// print entropy information, but use eprint to only print it
// to the terminal
print_entropy(
number_of_words_to_put_in_passphrase,
list_length,
opt.n_passphrases,
);
}
// 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()),
};
println!("{}", passphrase);
}

Any ideas on how to refactor this would be appreciated!

For example, is there a way to make a single Type or Struct that can take both a &' static[& 'static str] and a Vec<String>?

@sts10 sts10 added the help wanted Extra attention is needed label Oct 28, 2023
westonal added a commit to westonal/phraze that referenced this issue Oct 29, 2023
westonal added a commit to westonal/phraze that referenced this issue Oct 30, 2023
@sts10 sts10 closed this as completed in 80763cc Oct 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant