rusty-diceware

Commandline diceware, with or without dice, written in Rustlang.
git clone https://kaka.farm/~git/rusty-diceware
Log | Files | Refs | README | LICENSE

commit a14326bb9a7ac5d7f022da2859fb13edd2fea3fe
parent 7bdc3ecfb23e32dcc64c1532eb2564469934af99
Author: Yuval Langer <yuval.langer@gmail.com>
Date:   Thu,  7 Nov 2024 00:52:42 +0200

Apply suggestions made by `cargo clippy`.

Diffstat:
Msrc/main.rs | 75++++++++++++++++++++++++++++++++++++---------------------------------------
1 file changed, 36 insertions(+), 39 deletions(-)

diff --git a/src/main.rs b/src/main.rs @@ -21,18 +21,17 @@ extern crate rand; use std::env; use std::fs::File; +use std::io::stdin; use std::io::Bytes; use std::io::Read; -use std::io::Stdin; -use std::io::stdin; use std::process::exit; use std::str::FromStr; +use crate::rand::Rng; use diceware_wordlists::Wordlist; use getopts::Options; use rand::rngs::ThreadRng; use rand::thread_rng; -use crate::rand::Rng; fn make_options() -> Options { let mut opts = Options::new(); @@ -75,7 +74,7 @@ struct ReadWordDieCasts<'a, T> { } impl<T> ReadWordDieCasts<'_, T> { - fn new<'a, D>( + fn new<'a>( bytes_iter: &'a mut Bytes<T>, wordlist: &'a Vec<&'a str>, ) -> ReadWordDieCasts<'a, T> { @@ -90,14 +89,14 @@ impl<T: Read> Iterator for ReadWordDieCasts<'_, T> { type Item = String; fn next(&mut self) -> Option<<ReadWordDieCasts<T> as Iterator>::Item> { - return read_single_word(self.bytes_iter, self.wordlist); + read_single_word(self.bytes_iter, self.wordlist) } } -fn read_single_word<T>( - bytes_iter: &mut Bytes<T>, - wordlist: &Vec<&str>, -) -> Option<String> where T: Read { +fn read_single_word<T>(bytes_iter: &mut Bytes<T>, wordlist: &[&str]) -> Option<String> +where + T: Read, +{ let die_cast_per_word = die_cast_per_word(wordlist.len()); let mut word_index = 0; @@ -124,17 +123,19 @@ fn read_single_word<T>( match current_byte_result { Ok(b'\n') => { return Some(wordlist[word_index].to_string()); - }, + } Ok(n) => match n { b'1' | b'2' | b'3' | b'4' | b'5' | b'6' => { - eprintln!("Too many rolls. Each word must have exactly {} die rolls.", - die_cast_per_word); + eprintln!( + "Too many rolls. Each word must have exactly {} die rolls.", + die_cast_per_word + ); exit(-1); - }, + } err => { eprintln!("Input must be either 1 to 6 inclusive digits or newline. (Got: ascii code {})", err); exit(-1); - }, + } }, Err(err) => { eprintln!("Unknown error: {}", err); @@ -143,20 +144,20 @@ fn read_single_word<T>( }; } else { let current_byte_result = match bytes_iter.next() { - Some(x) => { - x - }, + Some(x) => x, None => { if die_index == 0 { // We are at a legal EOF state, right before a single word die casting sequence. return None; } else { // We are in the middle of die casting and somehow stdin ended. - eprintln!("Bad number of rolls. Each word must have exactly {} die rolls.", - die_cast_per_word); + eprintln!( + "Bad number of rolls. Each word must have exactly {} die rolls.", + die_cast_per_word + ); exit(-1); } - }, + } }; let die_significance = die_cast_per_word - die_index - 1; @@ -168,23 +169,25 @@ fn read_single_word<T>( Ok(b'5') => 4 * 6_usize.pow(die_significance), Ok(b'6') => 5 * 6_usize.pow(die_significance), Ok(b'\n') => { - eprintln!("Bad number of rolls. Each word must have exactly {} die rolls.", - die_cast_per_word); + eprintln!( + "Bad number of rolls. Each word must have exactly {} die rolls.", + die_cast_per_word + ); exit(-1); - }, + } Ok(x) => { eprintln!("Input must be either 1 to 6 inclusive digits or newline. (Got ascii code {})", x); exit(-1); - }, + } Err(err) => { eprintln!("Error while reading die casts: {}", err); exit(-1); - }, + } }; }; - }; + } - return Some(wordlist[word_index].to_string()); + Some(wordlist[word_index].to_string()) } fn entropy(wordlist: &[&str]) -> f64 { @@ -201,14 +204,8 @@ struct ReadRngWord<'a> { } impl<'a> ReadRngWord<'a> { - fn new( - rng: &'a mut ThreadRng, - wordlist: &'a Vec<&'a str>, - ) -> ReadRngWord<'a> { - ReadRngWord { - rng, - wordlist, - } + fn new(rng: &'a mut ThreadRng, wordlist: &'a Vec<&'a str>) -> ReadRngWord<'a> { + ReadRngWord { rng, wordlist } } } @@ -221,7 +218,7 @@ impl<'a> Iterator for ReadRngWord<'a> { } fn load_wordlist_file(filepath: &str) -> String { - let mut wordlist_file = match File::open(&filepath) { + let mut wordlist_file = match File::open(filepath) { Ok(ok) => ok, Err(err) => panic!("Unable to open file: {}; due to error: {}", filepath, err), }; @@ -294,7 +291,7 @@ fn main() { let stdin = stdin(); let mut bytes_iter = stdin.bytes(); - let mut words = ReadWordDieCasts::new::<Bytes<Stdin>>(&mut bytes_iter, &wordlist); + let mut words = ReadWordDieCasts::new(&mut bytes_iter, &wordlist); if let Some(word) = words.next() { print!("{}", word); @@ -322,7 +319,7 @@ fn main() { for _ in 1..word_num { print!("{}{}", delimiter, words.next().unwrap()); words_count += 1; - }; + } println!(); @@ -337,8 +334,8 @@ fn main() { #[cfg(test)] mod tests { - use std::io::BufReader; use super::*; + use std::io::BufReader; #[test] fn test_read_single_word() {