-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from allenap/features-for-2.0
Features for 2.0
- Loading branch information
Showing
11 changed files
with
285 additions
and
197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,44 @@ | ||
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![]; | ||
|
||
let list_sizes = ["small", "medium", "large"]; | ||
let list_names = ["adjectives", "adverbs", "names"]; | ||
let list_names = ["adjectives", "adverbs", "nouns"]; | ||
|
||
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.