mdbook_lint_core/
config.rs

1//! Core configuration types for mdbook-lint-core
2//!
3//! This module contains the minimal configuration types needed by the core
4//! linting engine. The full configuration is handled by the CLI crate.
5
6use serde::{Deserialize, Serialize};
7use std::collections::HashMap;
8
9/// Core configuration for the linting engine
10#[derive(Debug, Clone, Serialize, Deserialize, Default)]
11pub struct Config {
12    /// List of enabled rule categories
13    #[serde(rename = "enabled-categories", default)]
14    pub enabled_categories: Vec<String>,
15
16    /// List of disabled rule categories
17    #[serde(rename = "disabled-categories", default)]
18    pub disabled_categories: Vec<String>,
19
20    /// List of explicitly enabled rules
21    #[serde(rename = "enabled-rules", default)]
22    pub enabled_rules: Vec<String>,
23
24    /// List of explicitly disabled rules
25    #[serde(rename = "disabled-rules", default)]
26    pub disabled_rules: Vec<String>,
27
28    /// How to handle deprecated rule warnings
29    #[serde(rename = "deprecated-warning", default)]
30    pub deprecated_warning: DeprecatedWarningLevel,
31
32    /// Enable markdownlint compatibility mode (disables rules that are disabled by default in markdownlint)
33    #[serde(rename = "markdownlint-compatible", default)]
34    pub markdownlint_compatible: bool,
35
36    /// Rule-specific configuration
37    #[serde(flatten)]
38    pub rule_configs: HashMap<String, toml::Value>,
39}
40
41/// How to handle deprecated rule warnings
42#[derive(Debug, Clone, Serialize, Deserialize, Default)]
43#[serde(rename_all = "lowercase")]
44pub enum DeprecatedWarningLevel {
45    /// Show warning messages for deprecated rules (default)
46    #[default]
47    Warn,
48    /// Show info messages for deprecated rules
49    Info,
50    /// Don't show any messages for deprecated rules
51    Silent,
52}
53
54impl Config {
55    /// Check if a rule should be run based on configuration
56    pub fn should_run_rule(
57        &self,
58        rule_id: &str,
59        rule_category: &str,
60        rule_enabled_by_default: bool,
61    ) -> bool {
62        // Explicit rule configuration takes precedence
63        if self.enabled_rules.contains(&rule_id.to_string()) {
64            return true;
65        }
66        if self.disabled_rules.contains(&rule_id.to_string()) {
67            return false;
68        }
69
70        // Category configuration takes precedence over default
71        if self.enabled_categories.contains(&rule_category.to_string()) {
72            return true;
73        }
74        if self
75            .disabled_categories
76            .contains(&rule_category.to_string())
77        {
78            return false;
79        }
80
81        // Use default enabled state
82        rule_enabled_by_default
83    }
84}