markdownlint-rs 0.1.0

A fast, flexible, configuration-based command-line interface for linting Markdown/CommonMark files
Documentation
use crate::markdown::MarkdownParser;
use crate::types::Violation;
use serde_json::Value;
use std::collections::HashMap;

pub trait Rule: Send + Sync {
    fn name(&self) -> &str;
    fn description(&self) -> &str;
    fn tags(&self) -> &[&str];

    /// Check the markdown content for violations
    fn check(&self, parser: &MarkdownParser, config: Option<&Value>) -> Vec<Violation>;

    /// Whether this rule can automatically fix violations
    fn fixable(&self) -> bool {
        false
    }
}

#[derive(Default)]
pub struct RuleRegistry {
    rules: HashMap<String, Box<dyn Rule>>,
}

impl RuleRegistry {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn register(&mut self, rule: Box<dyn Rule>) {
        self.rules.insert(rule.name().to_string(), rule);
    }

    pub fn get(&self, name: &str) -> Option<&dyn Rule> {
        self.rules.get(name).map(|r| r.as_ref())
    }

    pub fn all_rules(&self) -> impl Iterator<Item = &dyn Rule> {
        self.rules.values().map(|r| r.as_ref())
    }
}