Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Basic support of config files #11

Merged
merged 5 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 58 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ edition = "2021"
anyhow = "1.0.86"
clap = { version = "4.5.4", features = ["derive"] }
comrak = "0.24.1"
serde = { version = "1.0.203", features = ["derive"] }
toml = "0.8.14"
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,20 @@ quickmark /path/to/file.md

### Configuration

TBD
Quickmark looks up for `quickmark.toml` configuration file in the current working directory. If the file was not found, the default is used.

Below is a full configuration with default values:

```toml
[linters.severity]
# possible values are: 'warn', 'err' and 'off'
heading-increment = 'err'
heading-style = 'err'

# see a specific rule's doc for details of configuration
[linters.settings.heading-style]
style = 'consistent'
```

## Rules

Expand Down
179 changes: 179 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
use serde::Deserialize;
use std::{
collections::{HashMap, HashSet},
fs,
path::Path,
};

use crate::rules::ALL_RULES;
#[derive(Deserialize, Debug, PartialEq, Clone)]
pub enum RuleSeverity {
#[serde(rename = "err")]
Error,
#[serde(rename = "warn")]
Warning,
#[serde(rename = "off")]
Off,
}

#[derive(Deserialize, Debug, PartialEq, Clone)]
pub enum HeadingStyle {
#[serde(rename = "consistent")]
Consistent,
#[serde(rename = "atx")]
ATX,
#[serde(rename = "setext")]
Setext,
}

#[derive(Deserialize, Debug, PartialEq, Clone)]
pub struct MD003HeadingStyleTable {
pub style: HeadingStyle,
}

impl Default for MD003HeadingStyleTable {
fn default() -> Self {
Self {
style: HeadingStyle::Consistent,
}
}
}

#[derive(Deserialize, Debug, Default, PartialEq, Clone)]
pub struct LintersSettingsTable {
#[serde(rename = "heading-style")]
pub heading_style: MD003HeadingStyleTable,
}

#[derive(Deserialize, Debug, Default, PartialEq, Clone)]
pub struct LintersTable {
pub severity: HashMap<String, RuleSeverity>,
pub settings: LintersSettingsTable,
}

#[derive(Deserialize, Debug, Default, PartialEq, Clone)]
pub struct QuickmarkConfig {
pub linters: LintersTable,
}

fn normalize_severities(severities: &mut HashMap<String, RuleSeverity>) {
let rule_aliases: HashSet<&str> = ALL_RULES.iter().map(|r| r.alias).collect();
severities.retain(|key, _| rule_aliases.contains(key.as_str()));
for &rule in &rule_aliases {
severities
.entry(rule.to_string())
.or_insert(RuleSeverity::Error);
}
}

pub fn parse_config(config_str: &str) -> anyhow::Result<QuickmarkConfig> {
let mut res: QuickmarkConfig = toml::from_str(config_str)?;
normalize_severities(&mut res.linters.severity);
Ok(res)
}

pub fn config_in_path_or_default(path: &Path) -> anyhow::Result<QuickmarkConfig> {
let config_file = path.join("quickmark.toml");
if config_file.is_file() {
let config = fs::read_to_string(config_file)?;
return parse_config(&config);
};
println!(
"Config file was not found at {}. Default config will be used.",
config_file.to_string_lossy()
);
let mut config = QuickmarkConfig::default();
normalize_severities(&mut config.linters.severity);
Ok(config)
}

#[cfg(test)]
mod test {
use std::path::Path;

use crate::config::{HeadingStyle, RuleSeverity};

use super::{config_in_path_or_default, parse_config};

const FIXTURES_PATH: &str = "tests/fixtures";

#[test]
pub fn test_deser() {
let config = r#"
[linters.severity]
heading-increment = 'warn'
heading-style = 'err'

[linters.settings.heading-style]
style = 'atx'
"#;

let parsed = parse_config(config).unwrap();
assert_eq!(
RuleSeverity::Warning,
*parsed.linters.severity.get("heading-increment").unwrap()
);
assert_eq!(
RuleSeverity::Error,
*parsed.linters.severity.get("heading-style").unwrap()
);
assert_eq!(
HeadingStyle::ATX,
parsed.linters.settings.heading_style.style
)
}

#[test]
pub fn test_normalize_severities() {
let config = r#"
[linters.severity]
heading-style = 'err'
some-bullshit = 'warn'

[linters.settings.heading-style]
style = 'atx'
"#;

let parsed = parse_config(config).unwrap();
assert_eq!(
RuleSeverity::Error,
*parsed.linters.severity.get("heading-increment").unwrap()
);
assert_eq!(None, parsed.linters.severity.get("some-bullshit"));
}
#[test]
pub fn test_get_config_from_file() {
let config = config_in_path_or_default(Path::new(FIXTURES_PATH))
.expect("get_config_in_pwd_or_default() failed");
assert_eq!(
RuleSeverity::Warning,
*config.linters.severity.get("heading-increment").unwrap()
);
assert_eq!(
RuleSeverity::Off,
*config.linters.severity.get("heading-style").unwrap()
);
assert_eq!(
HeadingStyle::ATX,
config.linters.settings.heading_style.style
);
}

#[test]
pub fn test_get_config_default() {
let config = config_in_path_or_default(Path::new("tests"))
.expect("get_config_in_pwd_or_default() failed");
assert_eq!(
RuleSeverity::Error,
*config.linters.severity.get("heading-increment").unwrap()
);
assert_eq!(
RuleSeverity::Error,
*config.linters.severity.get("heading-style").unwrap()
);
assert_eq!(
HeadingStyle::Consistent,
config.linters.settings.heading_style.style
);
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod config;
pub mod linter;
pub mod rules;
Loading