Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

feat(harper-cli): add config command

Matthew Espino57bddf06523f
diff --git a/Cargo.lock b/Cargo.lock
index 0113c4184..d7b1a5936 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -606,6 +606,7 @@ dependencies = [
"harper-core",
"harper-literate-haskell",
"harper-typst",
+ "serde",
"serde_json",
]
diff --git a/harper-cli/Cargo.toml b/harper-cli/Cargo.toml
index 05528322f..6d4f4e84a 100644
--- a/harper-cli/Cargo.toml
+++ b/harper-cli/Cargo.toml
@@ -14,6 +14,7 @@ harper-literate-haskell = { path = "../harper-literate-haskell", version = "0.16
harper-core = { path = "../harper-core", version = "0.16.0" }
harper-comments = { path = "../harper-comments", version = "0.16.0" }
harper-typst = { path = "../harper-typst", version = "0.16.0" }
+serde = { version = "1.0.214", features = ["derive"] }
serde_json = "1.0.135"
[features]
diff --git a/harper-cli/src/main.rs b/harper-cli/src/main.rs
index d306b0218..caea919c0 100644
--- a/harper-cli/src/main.rs
+++ b/harper-cli/src/main.rs
@@ -1,5 +1,6 @@
#![doc = include_str!("../README.md")]
+use std::collections::{BTreeMap, HashMap};
use std::path::{Path, PathBuf};
use anyhow::format_err;
@@ -10,6 +11,7 @@ use harper_core::linting::{LintGroup, LintGroupConfig, Linter};
use harper_core::parsers::{Markdown, MarkdownOptions};
use harper_core::{remove_overlaps, Dictionary, Document, FstDictionary, TokenKind};
use harper_literate_haskell::LiterateHaskellParser;
+use serde::Serialize;
/// A debugging tool for the Harper grammar checker.
#[derive(Debug, Parser)]
@@ -41,18 +43,21 @@ enum Args {
Metadata { word: String },
/// Emit a decompressed, line-separated list of the words in Harper's dictionary.
Words,
+ /// Print the default config with descriptions.
+ Config,
}
fn main() -> anyhow::Result<()> {
let args = Args::parse();
let markdown_options = MarkdownOptions::default();
let linting_options = LintGroupConfig::default();
+ let dictionary = FstDictionary::curated();
match args {
Args::Lint { file, count } => {
let (doc, source) = load_file(&file, markdown_options)?;
- let mut linter = LintGroup::new(linting_options, FstDictionary::curated());
+ let mut linter = LintGroup::new(linting_options, dictionary);
let mut lints = linter.lint(&doc);
if count {
@@ -145,11 +150,9 @@ fn main() -> anyhow::Result<()> {
Ok(())
}
Args::Words => {
- let dict = FstDictionary::curated();
-
let mut word_str = String::new();
- for word in dict.words_iter() {
+ for word in dictionary.words_iter() {
word_str.clear();
word_str.extend(word);
@@ -159,12 +162,40 @@ fn main() -> anyhow::Result<()> {
Ok(())
}
Args::Metadata { word } => {
- let dict = FstDictionary::curated();
- let metadata = dict.get_word_metadata_str(&word);
+ let metadata = dictionary.get_word_metadata_str(&word);
let json = serde_json::to_string_pretty(&metadata).unwrap();
println!("{json}");
+ Ok(())
+ }
+ Args::Config => {
+ #[derive(Serialize)]
+ struct Config {
+ default_value: bool,
+ description: String,
+ }
+
+ let mut linter = LintGroup::new(linting_options, dictionary);
+ linter.config.fill_default_values();
+
+ let default_config: HashMap<String, bool> =
+ serde_json::from_str(&serde_json::to_string(&linter.config).unwrap()).unwrap();
+
+ // Use `BTreeMap` so output is sorted by keys.
+ let mut configs = BTreeMap::new();
+ for (key, desc) in linter.all_descriptions().to_vec_pairs() {
+ configs.insert(
+ key.to_owned(),
+ Config {
+ default_value: default_config[key],
+ description: desc.to_owned(),
+ },
+ );
+ }
+
+ println!("{}", serde_json::to_string_pretty(&configs).unwrap());
+
Ok(())
}
}