mirror of
https://github.com/crate-ci/typos.git
synced 2026-11-12 00:53:58 +01:00
feat: Expose codespell's dict to Rust
This commit is contained in:
1 parent
8f428b8fec
commit
1cbdb3a77a
11 files changed
+51234
-2
No files matched your search
@@ -0,0 +1,80 @@
|
||||
use structopt::StructOpt;
|
||||
|
||||
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()
|
||||
.split(",")
|
||||
.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();
|
||||
writeln!(file, "use unicase::UniCase;").unwrap();
|
||||
|
||||
let dict = parse_dict(DICT);
|
||||
|
||||
writeln!(
|
||||
file,
|
||||
"pub(crate) static WORD_DICTIONARY: phf::Map<unicase::UniCase<&'static str>, &[&'static str]> = ",
|
||||
)
|
||||
.unwrap();
|
||||
let mut builder = phf_codegen::Map::new();
|
||||
for (typo, corrections) in dict {
|
||||
let value = itertools::join(corrections.iter().map(|s| format!("{:?}", s)), ", ");
|
||||
let value = format!("&[{}]", value);
|
||||
builder.entry(unicase::UniCase::new(typo), &value);
|
||||
}
|
||||
let codegenned = builder.build();
|
||||
writeln!(file, "{}", codegenned).unwrap();
|
||||
writeln!(file, ";").unwrap();
|
||||
writeln!(file, "").unwrap();
|
||||
}
|
||||
|
||||
#[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();
|
||||
|
||||
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);
|
||||
}
|
||||
Reference in new issue
Block a user