Skip to content

Commit

Permalink
Deduplicate and sort word lists when building
Browse files Browse the repository at this point in the history
Also improves error reporting during the build phase.

Fixes #52.
  • Loading branch information
allenap committed Sep 11, 2023
1 parent 95075e5 commit d087aee
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ default-rng = ["rand/std", "rand/std_rng"]
# Allows the default word lists to be used.
default-words = []

[build-dependencies]
anyhow = "^1.0.75"

[dependencies]
clap = { version = "^3.1.0", features = ["cargo", "derive"], optional = true }
itertools = { version = "^0.10.3", default-features = false }
Expand Down
28 changes: 21 additions & 7 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use anyhow::{Context, Result};
use std::collections::HashSet;
use std::env;
use std::fs;
use std::path::Path;

fn main() {
let out_dir = env::var_os("OUT_DIR").unwrap();
fn main() -> Result<()> {
let words_dir = Path::new("words");
let out_dir = env::var_os("OUT_DIR").context("OUT_DIR not set")?;
let dest_path = Path::new(&out_dir).join("words.rs");

let mut lines: Vec<String> = vec![];
Expand All @@ -14,17 +17,28 @@ fn main() {
for list_size in list_sizes {
lines.push(format!("pub mod {list_size} {{"));
for list_name in list_names {
let list_path = format!("words/{list_size}/{list_name}.txt");
println!("cargo:rerun-if-changed={list_path}");
let list_raw = fs::read_to_string(list_path).unwrap();
let list = list_raw.split_whitespace().collect::<Vec<_>>();
let list_path = words_dir.join(list_size).join(list_name).with_extension("txt");
println!("cargo:rerun-if-changed={}", list_path.to_string_lossy());
let list_raw = fs::read_to_string(&list_path)
.with_context(|| format!("Could not read word list from {list_path:?}"))?;
let list = {
// Ensure we have no duplicates.
let words = list_raw.split_whitespace().collect::<HashSet<_>>();
// Collect into a `Vec` and sort it.
let mut list = words.into_iter().collect::<Vec<_>>();
list.sort();
list
};
lines.push(format!(" pub static {}: [&str; {}] = [", list_name.to_uppercase(), list.len()));
lines.extend(list.iter().map(|word| format!(" \"{word}\",")));
lines.push(" ];".to_string());
}
lines.push("}".to_string());
}

fs::write(dest_path, lines.join("\n")).unwrap();
fs::write(&dest_path, lines.join("\n"))
.with_context(|| format!("Could not write word lists to output file {dest_path:?}"))?;
println!("cargo:rerun-if-changed=build.rs");

Ok(())
}

0 comments on commit d087aee

Please sign in to comment.