rusty-diceware

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit ceeb4239896435b0757e58f1d5db8cbce44b8af4
parent aa03cb6dfc0299e75ee223137786234784e70ec3
Author: Yuval Langer <yuvallangerontheroad@gmail.com>
Date:   Wed,  7 Sep 2022 03:40:47 +0300

Deduplicate build stuff and note in CHANGELOG.

Diffstat:
MCHANGELOG.md | 16+++++++++++-----
Mbuild.rs | 65++++++++++++++++++++++++++++++++++-------------------------------
2 files changed, 45 insertions(+), 36 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md @@ -1,3 +1,10 @@ +## v0.3.7 + +* Deduplicate code implementing the `Word` trait using macros. (Thanks + @vbrandl!) +* Deduplicate tests code using macros. (Thanks @vbrandl, again!) +* Deduplicate `build.rs` code using a function. How not exciting. + ## v0.3.6 * No, really, fix tests. (Added the `Word` trait) @@ -8,11 +15,10 @@ ## v0.3.4 -* Deduplicate word printing code by using the trait `Word` and moving - that code into a a generic function which uses that trait. -* Move to 2021 edition of Rustlang. -* Add a commented out wordlist command line option for a future - version. +* Deduplicate word printing code by using the trait `Word` and moving that code + into a a generic function which uses that trait. +* Move to 2021 edition of Rustlang. Add a commented out wordlist command line + option for a future version. * Remove some commented out code. * Use the `Self` keyword in `impl`s instead of the type's concrete name. diff --git a/build.rs b/build.rs @@ -14,11 +14,9 @@ fn make_wordlist(contents: &str) -> Vec<&str> { .collect() } -fn make_minilock_wordlist(contents: &str) -> Vec<&str> { - (&contents[1023..543718]).split(',').collect() -} +fn make_beale_struct(contents: &str) -> std::string::String { + let wordlist = make_wordlist(contents); -fn make_beale_struct(wordlist: Vec<&str>) -> std::string::String { let mut output = std::string::String::new(); // 7776 words = 6*6*6*6*6; five 6 faced dice throws. @@ -38,7 +36,9 @@ fn make_beale_struct(wordlist: Vec<&str>) -> std::string::String { output } -fn make_reinhold_struct(wordlist: Vec<&str>) -> std::string::String { +fn make_reinhold_struct(contents: &str) -> std::string::String { + let wordlist = make_wordlist(contents); + let mut output = std::string::String::new(); // 7776 words = 6*6*6*6*6; five 6 faced dice throws. @@ -58,7 +58,9 @@ fn make_reinhold_struct(wordlist: Vec<&str>) -> std::string::String { output } -fn make_minilock_struct(wordlist: Vec<&str>) -> std::string::String { +fn make_minilock_struct(contents: &str) -> std::string::String { + let wordlist: Vec<&str> = (&contents[1023..543718]).split(',').collect(); + let mut output = std::string::String::new(); // 58110 words in the MiniLock wordlist. @@ -78,39 +80,40 @@ fn make_minilock_struct(wordlist: Vec<&str>) -> std::string::String { output } -fn main() { +fn build_wordlist( + wordlist_filename: &str, + destination_file: &mut File, + make_wordlist_struct: &dyn Fn(&str) -> String, +) { let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); println!("{}", manifest_dir); let wordlists_dir = Path::new(&manifest_dir).join("bin").join("wordlists"); - let mut beale_wordlist_file = File::open(&wordlists_dir.join("beale.wordlist.asc")).unwrap(); - let mut reinhold_wordlist_file = - File::open(&wordlists_dir.join("diceware.wordlist.asc")).unwrap(); - let mut minilock_wordlist_file = File::open(&wordlists_dir.join("phrase.js")).unwrap(); - let mut beale_wordlist_string = String::new(); - let mut reinhold_wordlist_string = String::new(); - let mut minilock_wordlist_string = String::new(); + let mut wordlist_file = File::open(&wordlists_dir.join(wordlist_filename)).unwrap(); - beale_wordlist_file - .read_to_string(&mut beale_wordlist_string) - .unwrap(); - reinhold_wordlist_file - .read_to_string(&mut reinhold_wordlist_string) - .unwrap(); - minilock_wordlist_file - .read_to_string(&mut minilock_wordlist_string) - .unwrap(); + let mut wordlist_string = String::new(); - let out_dir = env::var("OUT_DIR").unwrap(); - let dest_path = Path::new(&out_dir).join("diceware.rs"); - let mut f = File::create(&dest_path).unwrap(); + wordlist_file.read_to_string(&mut wordlist_string).unwrap(); - f.write_all(make_beale_struct(make_wordlist(&beale_wordlist_string)).as_bytes()) + destination_file + .write_all(make_wordlist_struct(&wordlist_string).as_bytes()) .unwrap(); +} - f.write_all(make_reinhold_struct(make_wordlist(&reinhold_wordlist_string)).as_bytes()) - .unwrap(); +fn main() { + let out_dir = env::var("OUT_DIR").unwrap(); + let dest_path = Path::new(&out_dir).join("diceware.rs"); + let mut destination_file = File::create(&dest_path).unwrap(); - f.write_all(make_minilock_struct(make_minilock_wordlist(&minilock_wordlist_string)).as_bytes()) - .unwrap(); + build_wordlist( + "beale.wordlist.asc", + &mut destination_file, + &make_beale_struct, + ); + build_wordlist( + "diceware.wordlist.asc", + &mut destination_file, + &make_reinhold_struct, + ); + build_wordlist("phrase.js", &mut destination_file, &make_minilock_struct); }