typos/crates/wikipedia-dict/codegen/src/main.rs

71 lines
1.6 KiB
Rust
Raw Normal View History

2022-02-01 11:34:12 -05:00
use clap::Parser;
2019-10-28 12:01:22 -04:00
pub const DICT: &str = include_str!("../../assets/dictionary.txt");
fn parse_dict(raw: &str) -> impl Iterator<Item = (&str, Vec<&str>)> {
raw.lines().map(|s| {
let mut parts = s.splitn(2, "->");
let typo = parts.next().unwrap().trim();
let corrections = parts
.next()
.unwrap()
2019-10-28 18:25:41 -04:00
.split(',')
2019-10-28 12:01:22 -04:00
.filter_map(|c| {
let c = c.trim();
if c.is_empty() {
None
} else {
Some(c)
}
})
.collect();
(typo, corrections)
})
}
fn generate<W: std::io::Write>(file: &mut W) {
writeln!(
file,
"// This file is code-genned by {}",
env!("CARGO_PKG_NAME")
)
.unwrap();
writeln!(file).unwrap();
let dict = parse_dict(DICT);
dictgen::generate_table(
file,
"WORD_DICTIONARY",
"&[&str]",
dict.map(|kv| (kv.0, format!("&{:?}", kv.1))),
)
.unwrap();
2019-10-28 12:01:22 -04:00
}
2022-02-01 11:34:12 -05:00
#[derive(Debug, Parser)]
2019-10-28 12:01:22 -04:00
struct Options {
2022-02-01 11:34:12 -05:00
#[clap(flatten)]
2019-10-28 12:01:22 -04:00
codegen: codegenrs::CodeGenArgs,
2022-02-01 11:34:12 -05:00
#[clap(flatten)]
2019-10-28 12:01:22 -04:00
rustmft: codegenrs::RustfmtArgs,
}
fn run() -> Result<i32, Box<dyn std::error::Error>> {
2022-02-01 11:34:12 -05:00
let options = Options::parse();
2019-10-28 12:01:22 -04:00
let mut content = vec![];
generate(&mut content);
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);
}