Skip to content

Commit eda9160

Browse files
committed
added static string and -f flag (0.3)
1 parent a29fb81 commit eda9160

File tree

5 files changed

+30
-12
lines changed

5 files changed

+30
-12
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "dicers"
3-
version = "0.2.0"
3+
version = "0.3.0"
44
authors = ["Tidle <tidle@users.noreply.github.com>"]
55
edition = "2018"
66

src/args.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ pub struct Args {
55
pub long: bool,
66
pub manual: bool,
77
pub words: usize,
8+
pub wordlist: Option<String>,
89
}
910

1011
impl Args {
1112
pub fn new() -> Args {
1213
let app = App::new("dicers")
13-
.version("0.2")
14+
.version("0.3")
1415
.about("generates a diceware password")
1516
.author("tidle")
1617
.arg(
@@ -25,6 +26,13 @@ impl Args {
2526
.long("manual")
2627
.help("manually input dice rolls rather than using the computer"),
2728
)
29+
.arg(
30+
Arg::with_name("FILE")
31+
.short("f")
32+
.long("file")
33+
.takes_value(true)
34+
.help("manually specify a wordlist, rather than using the built in wordlists"),
35+
)
2836
.arg(
2937
Arg::with_name("WORD COUNT")
3038
.short("w")
@@ -44,11 +52,13 @@ impl Args {
4452
eprintln!("Invalid number: {}", e);
4553
std::process::exit(60);
4654
});
55+
let wordlist = app.value_of("FILE").map(|x| x.to_string());
4756

4857
Args {
4958
long,
5059
manual,
5160
words,
61+
wordlist,
5262
}
5363
}
5464
}

src/dat.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub static LIST4: &'static str = include_str!("../list4.txt");
2+
pub static LIST5: &'static str = include_str!("../list5.txt");

src/main.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod args;
22
mod dice;
3+
mod dat;
34

45
use args::Args;
56
use dice::roll;
@@ -14,14 +15,14 @@ fn main() {
1415
password.push_str(&match result {
1516
Roll4(v) => format!(
1617
"{} ",
17-
short_word(v).unwrap_or_else(|e| {
18+
short_word(v, &args.wordlist).unwrap_or_else(|e| {
1819
eprintln!("Error getting word: {}", e);
1920
std::process::exit(128)
2021
})
2122
),
2223
Roll5(v) => format!(
2324
"{} ",
24-
long_word(v).unwrap_or_else(|e| {
25+
long_word(v, &args.wordlist).unwrap_or_else(|e| {
2526
eprintln!("Error getting word: {}", e);
2627
std::process::exit(128)
2728
})
@@ -42,22 +43,27 @@ fn main() {
4243
)
4344
}
4445

45-
fn short_word(dice: [DiceFace; 4]) -> std::io::Result<String> {
46+
fn short_word(dice: [DiceFace; 4], wordlist: &Option<String>) -> std::io::Result<String> {
4647
let match_str = format!("{}{}{}{}", dice[0], dice[1], dice[2], dice[3]);
47-
word(&match_str, "list4.txt")
48+
word(&match_str, wordlist, dat::LIST4)
4849
}
4950

50-
fn long_word(dice: [DiceFace; 5]) -> std::io::Result<String> {
51+
fn long_word(dice: [DiceFace; 5], wordlist: &Option<String>) -> std::io::Result<String> {
5152
let match_str = format!("{}{}{}{}{}", dice[0], dice[1], dice[2], dice[3], dice[4]);
52-
word(&match_str, "list5.txt")
53+
word(&match_str, wordlist, dat::LIST5)
5354
}
5455

55-
fn word(match_str: &str, filename: &str) -> std::io::Result<String> {
56+
fn word(match_str: &str, filename: &Option<String>, fblist: &str) -> std::io::Result<String> {
5657
use std::io::BufRead;
5758
use std::{fs, io};
5859

59-
let wordlist = fs::File::open(filename)?;
60-
let wordlist = io::BufReader::new(wordlist);
60+
let wordlist : Box<dyn BufRead>;
61+
if let Some(f) = filename {
62+
let w = fs::File::open(f)?;
63+
wordlist = Box::new(io::BufReader::new(w));
64+
} else {
65+
wordlist = Box::new(io::BufReader::new(fblist.as_bytes()))
66+
}
6167
let mut result = String::new();
6268

6369
for line in wordlist.lines() {

0 commit comments

Comments
 (0)