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 29, 2023
1 parent 1d3e203 commit e59b41e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 100 deletions.
51 changes: 0 additions & 51 deletions build.rs

This file was deleted.

37 changes: 18 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
pub mod file_reader;
pub mod separators;
pub mod unicode_normalization_check;

use crate::separators::make_separator;
use rand::{seq::SliceRandom, thread_rng, Rng};

// Pull in the wordlists as constants for us to use later.
// This is thanks to the build.rs build script. Learn more:
// https://doc.rust-lang.org/cargo/reference/build-scripts.html#case-study-code-generation
include!(concat!(env!("OUT_DIR"), "/wordlists.rs"));

/// The possible word lists that Phraze can use.
#[derive(Clone, Debug, Copy)]
pub enum ListChoice {
Long,
Medium,
Eff,
Mnemonicode,
Effshort,
Qwerty,
Alpha,
Eff,
Effshort,
Mnemonicode,
}

/// Given user's inputs, figure out how many words the generated passphrase will need. If user
Expand Down Expand Up @@ -67,18 +63,21 @@ pub fn convert_minimum_entropy_to_number_of_words(
(minimum_entropy as f64 / entropy_per_word_from_this_list).ceil() as usize
}

/// Take enum of list_choice and find the constant that is the corresponding word list (with the
/// actual words). These are defined in the build script (build.rs)
pub fn fetch_list(list_choice: ListChoice) -> &'static [&'static str] {
/// Take enum of list_choice and find the list that is the corresponding word list (with the
/// actual words).
pub fn fetch_list(list_choice: ListChoice) -> Vec<String> {
match list_choice {
ListChoice::Long => WL_LONG,
ListChoice::Medium => WL_MEDIUM,
ListChoice::Qwerty => WL_QWERTY,
ListChoice::Alpha => WL_ALPHA,
ListChoice::Eff => WL_EFF,
ListChoice::Effshort => WL_EFFSHORT,
ListChoice::Mnemonicode => WL_MNEMONICODE,
}
ListChoice::Long => include_str!("../word-lists/orchard-street-long.txt"),
ListChoice::Medium => include_str!("../word-lists/orchard-street-medium.txt"),
ListChoice::Qwerty => include_str!("../word-lists/orchard-street-qwerty.txt"),
ListChoice::Alpha => include_str!("../word-lists/orchard-street-alpha.txt"),
ListChoice::Eff => include_str!("../word-lists/eff-long.txt"),
ListChoice::Effshort => include_str!("../word-lists/eff-short-1.txt"),
ListChoice::Mnemonicode => include_str!("../word-lists/mnemonicode.txt"),
}.lines()
.filter(|s| !s.is_empty())
.map(|s| s.to_string())
.collect()
}

/// Actually generate the passphrase, given a couple neccessary parameters.
Expand Down
40 changes: 10 additions & 30 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,12 @@ 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))),
let word_list: Vec<String> = match opt.custom_list_file_path {
Some(custom_list_file_path) => read_in_custom_list(&custom_list_file_path)?,
None => 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...
};
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 @@ -136,21 +125,12 @@ 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);
}

Expand Down

0 comments on commit e59b41e

Please sign in to comment.