2019-10-05 09:30:30 -04:00
|
|
|
use structopt::StructOpt;
|
|
|
|
|
2020-04-07 20:50:06 -04:00
|
|
|
const DICT: &[u8] = include_bytes!("../../assets/words.csv");
|
2019-10-28 12:09:29 -04:00
|
|
|
|
|
|
|
fn generate<W: std::io::Write>(file: &mut W) {
|
2019-10-05 09:30:30 -04:00
|
|
|
writeln!(
|
|
|
|
file,
|
|
|
|
"// This file is code-genned by {}",
|
|
|
|
env!("CARGO_PKG_NAME")
|
|
|
|
)
|
|
|
|
.unwrap();
|
2019-10-29 09:53:32 -04:00
|
|
|
writeln!(file, "#![allow(clippy::unreadable_literal)]",).unwrap();
|
2019-10-05 09:30:30 -04:00
|
|
|
writeln!(file).unwrap();
|
|
|
|
writeln!(file, "use unicase::UniCase;").unwrap();
|
|
|
|
|
2020-11-10 21:45:57 -05:00
|
|
|
let mut smallest = usize::MAX;
|
|
|
|
let mut largest = usize::MIN;
|
|
|
|
|
2019-10-05 09:30:30 -04:00
|
|
|
writeln!(
|
|
|
|
file,
|
2019-10-28 12:31:16 -04:00
|
|
|
"pub static WORD_DICTIONARY: phf::Map<unicase::UniCase<&'static str>, &'static str> = "
|
2019-10-05 09:30:30 -04:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let mut builder = phf_codegen::Map::new();
|
2020-08-15 22:03:00 -04:00
|
|
|
let records: Vec<_> = csv::ReaderBuilder::new()
|
|
|
|
.has_headers(false)
|
|
|
|
.from_reader(DICT)
|
2019-10-05 09:30:30 -04:00
|
|
|
.records()
|
|
|
|
.map(|r| r.unwrap())
|
|
|
|
.collect();
|
|
|
|
for record in &records {
|
2020-11-10 21:45:57 -05:00
|
|
|
smallest = std::cmp::min(smallest, record[0].len());
|
|
|
|
largest = std::cmp::max(largest, record[0].len());
|
2019-10-05 09:30:30 -04:00
|
|
|
let value = format!(r#""{}""#, &record[1]);
|
2019-10-25 12:24:25 -04:00
|
|
|
builder.entry(unicase::UniCase::new(&record[0]), &value);
|
2019-10-05 09:30:30 -04:00
|
|
|
}
|
2019-10-25 12:24:25 -04:00
|
|
|
let codegenned = builder.build();
|
|
|
|
writeln!(file, "{}", codegenned).unwrap();
|
2019-10-05 09:30:30 -04:00
|
|
|
writeln!(file, ";").unwrap();
|
2020-11-10 21:45:57 -05:00
|
|
|
writeln!(file).unwrap();
|
|
|
|
writeln!(file, "pub const WORD_MIN: usize = {};", smallest).unwrap();
|
|
|
|
writeln!(file, "pub const WORD_MAX: usize = {};", largest).unwrap();
|
2019-10-05 09:30:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, StructOpt)]
|
|
|
|
#[structopt(rename_all = "kebab-case")]
|
|
|
|
struct Options {
|
|
|
|
#[structopt(flatten)]
|
|
|
|
codegen: codegenrs::CodeGenArgs,
|
|
|
|
#[structopt(flatten)]
|
|
|
|
rustmft: codegenrs::RustfmtArgs,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run() -> Result<i32, Box<dyn std::error::Error>> {
|
|
|
|
let options = Options::from_args();
|
|
|
|
|
2019-10-28 12:09:29 -04:00
|
|
|
let mut content = vec![];
|
|
|
|
generate(&mut content);
|
2019-10-05 09:30:30 -04:00
|
|
|
|
|
|
|
let content = String::from_utf8(content)?;
|
|
|
|
let content = options.rustmft.reformat(&content)?;
|
|
|
|
options.codegen.write_str(&content)?;
|
|
|
|
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let code = run().unwrap();
|
|
|
|
std::process::exit(code);
|
|
|
|
}
|