commit 1daf36f49415f9919aeba548cebbff7f9acdedd6
parent fa50c39738f056cc17600f4edaeac1a748e0c854
Author: Jan Christian Grünhage <jan.christian@gruenhage.xyz>
Date: Mon, 17 Oct 2022 11:29:50 +0200
feat: expose wordlists as an enum
The enum optionally implements (de)serialization with serde, when that
feature is enabled and has a function for getting a word list out of it.
This is useful for cases like config files, where you'd want to store a
preferred word-list or something like that.
Diffstat:
5 files changed, 123 insertions(+), 18 deletions(-)
diff --git a/Cargo.lock b/Cargo.lock
@@ -10,7 +10,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "diceware"
-version = "0.5.4"
+version = "0.5.5"
dependencies = [
"diceware_wordlists",
"getopts",
@@ -19,7 +19,10 @@ dependencies = [
[[package]]
name = "diceware_wordlists"
-version = "0.5.4"
+version = "0.5.5"
+dependencies = [
+ "serde",
+]
[[package]]
name = "getopts"
@@ -54,6 +57,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
[[package]]
+name = "proc-macro2"
+version = "1.0.47"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725"
+dependencies = [
+ "unicode-ident",
+]
+
+[[package]]
+name = "quote"
+version = "1.0.21"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179"
+dependencies = [
+ "proc-macro2",
+]
+
+[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -84,6 +105,43 @@ dependencies = [
]
[[package]]
+name = "serde"
+version = "1.0.145"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "728eb6351430bccb993660dfffc5a72f91ccc1295abaa8ce19b27ebe4f75568b"
+dependencies = [
+ "serde_derive",
+]
+
+[[package]]
+name = "serde_derive"
+version = "1.0.145"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "81fa1584d3d1bcacd84c277a0dfe21f5b0f6accf4a23d04d4c6d61f1af522b4c"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "syn"
+version = "1.0.102"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3fcd952facd492f9be3ef0d0b7032a6e442ee9b361d4acc2b1d0c4aaa5f613a1"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "unicode-ident",
+]
+
+[[package]]
+name = "unicode-ident"
+version = "1.0.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3"
+
+[[package]]
name = "unicode-width"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
@@ -4,7 +4,7 @@ members = ["diceware_wordlists"]
[package]
name = "diceware"
-version = "0.5.5"
+version = "0.5.6"
authors = ["Yuval Langer <yuvallangerontheroad@gmail.com>"]
license = "AGPL-3.0"
repository = "https://gitlab.com/yuvallanger/rusty-diceware"
@@ -17,4 +17,4 @@ edition = "2021"
[dependencies]
getopts = "^0.2"
rand = "^0.8"
-diceware_wordlists = "^0.5"
+diceware_wordlists = { path = "diceware_wordlists", version = "^0.5" }
diff --git a/diceware_wordlists/Cargo.toml b/diceware_wordlists/Cargo.toml
@@ -1,9 +1,15 @@
[package]
name = "diceware_wordlists"
-version = "0.5.5"
+version = "0.5.6"
authors = ["Yuval Langer <yuvallangerontheroad@gmail.com>"]
license = "MIT OR Apache-2.0"
repository = "https://gitlab.com/yuvallanger/rusty-diceware"
description = "Wordlists for the command line tool Rusty Diceware"
keywords = ["diceware", "password", "passphrase", "generator", "wordlists"]
edition = "2021"
+
+[dependencies]
+serde = { version = "1", optional = true, features = ["derive"], default-features = false }
+
+[features]
+serde = ["dep:serde"]
diff --git a/diceware_wordlists/src/lib.rs b/diceware_wordlists/src/lib.rs
@@ -1 +1,49 @@
+use std::str::FromStr;
+
include!(concat!(env!("OUT_DIR"), "/diceware.rs"));
+
+#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
+#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
+pub enum Wordlist {
+ Beale,
+ Reinhold,
+ Minilock,
+ EffLong,
+ EffShort1,
+ EffShort2,
+}
+
+impl Wordlist {
+ pub fn get_list(&self) -> &'static [&'static str] {
+ match self {
+ Self::Beale => &BEALE_WORDLIST,
+ Self::Reinhold => &REINHOLD_WORDLIST,
+ Self::Minilock => &MINILOCK_WORDLIST,
+ Self::EffLong => &EFF_LONG_WORDLIST,
+ Self::EffShort1 => &EFF_SHORT_WORDLIST_1,
+ Self::EffShort2 => &EFF_SHORT_WORDLIST_2_0,
+ }
+ }
+}
+
+impl FromStr for Wordlist {
+ type Err = &'static str;
+
+ fn from_str(s: &str) -> Result<Self, Self::Err> {
+ match s {
+ "efflong" => Ok(Self::EffLong),
+ "reinhold" => Ok(Self::Reinhold),
+ "beale" => Ok(Self::Beale),
+ "minilock" => Ok(Self::Minilock),
+ "effshort1" => Ok(Self::EffShort1),
+ "effshort2" => Ok(Self::EffShort2),
+ _ => Err("Unknown Wordlist"),
+ }
+ }
+}
+
+impl Default for Wordlist {
+ fn default() -> Self {
+ Self::EffLong
+ }
+}
diff --git a/src/main.rs b/src/main.rs
@@ -23,7 +23,9 @@ use std::fs::File;
use std::io::BufRead;
use std::io::Read;
use std::process::exit;
+use std::str::FromStr;
+use diceware_wordlists::Wordlist;
use getopts::Options;
use rand::prelude::SliceRandom;
use rand::rngs::ThreadRng;
@@ -231,13 +233,12 @@ fn main() {
let is_entropy_printed = matches.opt_present("entropy");
let wordlist_name = if let Some(wordlist_option) = matches.opt_str("l") {
- match wordlist_option.to_lowercase().as_ref() {
- z @ ("beale" | "reinhold" | "minilock" | "efflong" | "effshort1" | "effshort2") => z,
+ match Wordlist::from_str(&wordlist_option.to_lowercase()) {
+ Ok(list) => list,
_ => unknown_wordlist(&wordlist_option),
}
- .to_string()
} else {
- "efflong".to_string()
+ Wordlist::default()
};
let mut rng = thread_rng();
@@ -269,15 +270,7 @@ fn main() {
);
};
} else {
- let wordlist = match wordlist_name.as_ref() {
- "efflong" => diceware_wordlists::EFF_LONG_WORDLIST.as_ref(),
- "reinhold" => diceware_wordlists::REINHOLD_WORDLIST.as_ref(),
- "beale" => diceware_wordlists::BEALE_WORDLIST.as_ref(),
- "minilock" => diceware_wordlists::MINILOCK_WORDLIST.as_ref(),
- "effshort1" => diceware_wordlists::EFF_SHORT_WORDLIST_1.as_ref(),
- "effshort2" => diceware_wordlists::EFF_SHORT_WORDLIST_2_0.as_ref(),
- _ => unknown_wordlist(&wordlist_name),
- };
+ let wordlist = wordlist_name.get_list();
if is_physical_rolls {
let rolls = read_rolls();