refactor(codegen): Hard code data

This commit is contained in:
Ed Page 2019-10-28 10:09:29 -06:00
parent 1cbdb3a77a
commit 5de368ac9d
3 changed files with 8 additions and 11 deletions

1
Cargo.lock generated
View file

@ -804,6 +804,7 @@ dependencies = [
"phf 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "phf 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"phf_codegen 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "phf_codegen 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"structopt 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "structopt 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
"typos-dict 0.1.1",
"unicase 2.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "unicase 2.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
] ]

View file

@ -25,7 +25,7 @@ stages:
steps: steps:
- template: azure/install-rust.yml@templates - template: azure/install-rust.yml@templates
- script: | - script: |
cargo run --package typos-codegen -- --input dict/typos/assets/words.csv --output dict/typos/src/dict_codegen.rs --check cargo run --package typos-codegen -- --output dict/typos/src/dict_codegen.rs --check
displayName: Verify typos-dict displayName: Verify typos-dict
- script: | - script: |
cargo run --package codespell-codegen -- --output dict/codespell/src/dict_codegen.rs --check cargo run --package codespell-codegen -- --output dict/codespell/src/dict_codegen.rs --check

View file

@ -1,6 +1,8 @@
use structopt::StructOpt; use structopt::StructOpt;
fn generate<W: std::io::Write>(input: &[u8], file: &mut W) { pub const DICT: &[u8] = include_bytes!("../../assets/words.csv");
fn generate<W: std::io::Write>(file: &mut W) {
writeln!( writeln!(
file, file,
"// This file is code-genned by {}", "// This file is code-genned by {}",
@ -16,7 +18,7 @@ fn generate<W: std::io::Write>(input: &[u8], file: &mut W) {
) )
.unwrap(); .unwrap();
let mut builder = phf_codegen::Map::new(); let mut builder = phf_codegen::Map::new();
let records: Vec<_> = csv::Reader::from_reader(input) let records: Vec<_> = csv::Reader::from_reader(DICT)
.records() .records()
.map(|r| r.unwrap()) .map(|r| r.unwrap())
.collect(); .collect();
@ -32,8 +34,6 @@ fn generate<W: std::io::Write>(input: &[u8], file: &mut W) {
#[derive(Debug, StructOpt)] #[derive(Debug, StructOpt)]
#[structopt(rename_all = "kebab-case")] #[structopt(rename_all = "kebab-case")]
struct Options { struct Options {
#[structopt(long, parse(from_os_str))]
input: std::path::PathBuf,
#[structopt(flatten)] #[structopt(flatten)]
codegen: codegenrs::CodeGenArgs, codegen: codegenrs::CodeGenArgs,
#[structopt(flatten)] #[structopt(flatten)]
@ -43,12 +43,8 @@ struct Options {
fn run() -> Result<i32, Box<dyn std::error::Error>> { fn run() -> Result<i32, Box<dyn std::error::Error>> {
let options = Options::from_args(); let options = Options::from_args();
let content = { let mut content = vec![];
let mut content = vec![]; generate(&mut content);
let input = std::fs::read(&options.input)?;
generate(&input, &mut content);
content
};
let content = String::from_utf8(content)?; let content = String::from_utf8(content)?;
let content = options.rustmft.reformat(&content)?; let content = options.rustmft.reformat(&content)?;