Skip to content

Commit

Permalink
refactor(rule001): use new and improved word iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
charislam committed Nov 30, 2024
1 parent 352c62a commit 6435a4c
Show file tree
Hide file tree
Showing 8 changed files with 244 additions and 240 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ glob = "0.3.1"
itertools = "0.13.0"
log = "0.4.22"
markdown = "1.0.0-alpha.21"
once_cell = "1.20.2"
regex = "1.11.0"
serde = { version = "1.0.210", features = ["derive"] }
serde_json = "1.0.128"
Expand Down
4 changes: 2 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,10 @@ impl Config {
}
});
}
toml::Value::Boolean(false) if RuleRegistry::is_valid_rule(&key) => {
toml::Value::Boolean(false) if registry.is_valid_rule(&key) => {
filtered_rules.insert(key.clone());
}
toml::Value::Table(table) if RuleRegistry::is_valid_rule(&key) => {
toml::Value::Table(table) if registry.is_valid_rule(&key) => {
let level = table.get("level");
if let Some(toml::Value::String(level)) = level.as_ref() {
match TryInto::<LintLevel>::try_into(level.as_str()) {
Expand Down
15 changes: 15 additions & 0 deletions src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,21 @@ impl DerefMut for AdjustedRange {
}
}

impl From<AdjustedRange> for Range<usize> {
fn from(range: AdjustedRange) -> Self {
Self::from(&range)
}
}

impl From<&AdjustedRange> for Range<usize> {
fn from(range: &AdjustedRange) -> Self {
Self {
start: range.start.into(),
end: range.end.into(),
}
}
}

impl AdjustedRange {
pub fn new(start: AdjustedOffset, end: AdjustedOffset) -> Self {
Self(Range { start, end })
Expand Down
17 changes: 6 additions & 11 deletions src/rules.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use anyhow::Result;
use log::{debug, error, warn};
use markdown::mdast::Node;
use once_cell::sync::Lazy;
use regex::Regex;
use std::{collections::HashMap, fmt::Debug};

Expand All @@ -18,15 +17,15 @@ mod rule002_admonition_types;
use rule001_heading_case::Rule001HeadingCase;
use rule002_admonition_types::Rule002AdmonitionTypes;

static ALL_RULES: Lazy<Vec<Box<dyn Rule>>> = Lazy::new(|| {
fn get_all_rules() -> Vec<Box<dyn Rule>> {
vec![
Box::new(Rule001HeadingCase::default()),
Box::new(Rule002AdmonitionTypes::default()),
]
});
}

#[allow(private_bounds)] // RuleClone is used within this module tree only
pub trait Rule: Send + Sync + Debug + RuleName + RuleClone {
pub trait Rule: Debug + RuleName + RuleClone {
fn default_level(&self) -> LintLevel;
fn setup(&mut self, _settings: Option<&RuleSettings>) {}
fn check(&self, ast: &Node, context: &RuleContext, level: LintLevel) -> Option<Vec<LintError>>;
Expand Down Expand Up @@ -236,19 +235,15 @@ enum RuleRegistryState {

impl RuleRegistry {
pub fn new() -> Self {
let mut rules = Vec::<Box<dyn Rule>>::with_capacity(ALL_RULES.len());
ALL_RULES.iter().for_each(|rule| {
rules.push(rule.clone());
});
Self {
state: RuleRegistryState::PreSetup,
rules,
rules: get_all_rules(),
configured_levels: Default::default(),
}
}

pub fn is_valid_rule(rule_name: &str) -> bool {
ALL_RULES.iter().any(|rule| rule.name() == rule_name)
pub fn is_valid_rule(&self, rule_name: &str) -> bool {
self.rules.iter().any(|rule| rule.name() == rule_name)
}

pub fn deactivate_rule(&mut self, rule_name: &str) {
Expand Down
Loading

0 comments on commit 6435a4c

Please sign in to comment.