Skip to content

Commit

Permalink
Chore: get rid of copying Context (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekropotin authored Jun 27, 2024
1 parent 941fb33 commit 79bf9d6
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 22 deletions.
12 changes: 6 additions & 6 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
};

use crate::rules::ALL_RULES;
#[derive(Deserialize, Debug, PartialEq, Clone)]
#[derive(Deserialize, Debug, PartialEq)]
pub enum RuleSeverity {
#[serde(rename = "err")]
Error,
Expand All @@ -16,7 +16,7 @@ pub enum RuleSeverity {
Off,
}

#[derive(Deserialize, Debug, PartialEq, Clone)]
#[derive(Deserialize, Debug, PartialEq)]
pub enum HeadingStyle {
#[serde(rename = "consistent")]
Consistent,
Expand All @@ -26,7 +26,7 @@ pub enum HeadingStyle {
Setext,
}

#[derive(Deserialize, Debug, PartialEq, Clone)]
#[derive(Deserialize, Debug, PartialEq)]
pub struct MD003HeadingStyleTable {
pub style: HeadingStyle,
}
Expand All @@ -39,19 +39,19 @@ impl Default for MD003HeadingStyleTable {
}
}

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

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

#[derive(Deserialize, Debug, Default, PartialEq, Clone)]
#[derive(Deserialize, Debug, Default, PartialEq)]
pub struct QuickmarkConfig {
pub linters: LintersTable,
}
Expand Down
6 changes: 3 additions & 3 deletions src/linter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fmt::Display, path::PathBuf};
use std::{fmt::Display, path::PathBuf, rc::Rc};

use comrak::{
nodes::{Ast, Sourcepos},
Expand Down Expand Up @@ -71,7 +71,7 @@ impl Display for RuleViolation {
}
}

#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct Context {
pub file_path: PathBuf,
pub config: QuickmarkConfig,
Expand All @@ -85,7 +85,7 @@ pub struct MultiRuleLinter {
}

impl MultiRuleLinter {
pub fn new(context: Context) -> Self {
pub fn new(context: Rc<Context>) -> Self {
Self {
linters: ALL_RULES
.iter()
Expand Down
10 changes: 4 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use quickmark::config::config_in_path_or_default;
use quickmark::linter::{print_linting_errors, MultiRuleLinter};
use std::cmp::min;
use std::env;
use std::rc::Rc;
use std::{fs, path::PathBuf, process::exit};
#[derive(Parser, Debug)]
#[command(version, about = "Quickmark: An extremely fast CommonMark linter")]
Expand All @@ -22,15 +23,12 @@ fn main() -> anyhow::Result<()> {
let pwd = env::current_dir()?;
let config = config_in_path_or_default(&pwd)?;

let context = quickmark::linter::Context {
file_path: file_path.clone(),
config: config.clone(),
};
let context = Rc::new(quickmark::linter::Context { file_path, config });

let mut linter = MultiRuleLinter::new(context);
let mut linter = MultiRuleLinter::new(context.clone());

let lint_res = linter.lint(&file_content);
let (errs, _) = print_linting_errors(&lint_res, &config);
let (errs, _) = print_linting_errors(&lint_res, &context.config);
let exit_code = min(errs, 1);
exit(exit_code);
}
10 changes: 7 additions & 3 deletions src/rules/md001.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
use std::rc::Rc;

use crate::{
linter::RuleViolation,
rules::{Context, Rule, RuleLinter},
};
use comrak::nodes::{Ast, NodeHeading, NodeValue};

pub(crate) struct MD001Linter {
context: Context,
context: Rc<Context>,
current_heading_level: u8,
}

impl MD001Linter {
pub fn new(context: Context) -> Self {
pub fn new(context: Rc<Context>) -> Self {
Self {
context,
current_heading_level: 0,
Expand Down Expand Up @@ -53,6 +55,7 @@ pub const MD001: Rule = Rule {
mod test {
use std::collections::HashMap;
use std::path::PathBuf;
use std::rc::Rc;

use crate::config::{
HeadingStyle, LintersSettingsTable, LintersTable, MD003HeadingStyleTable, QuickmarkConfig,
Expand All @@ -63,7 +66,7 @@ mod test {

use super::MD001;

fn test_context() -> Context {
fn test_context() -> Rc<Context> {
let severity: HashMap<_, _> = vec![("heading-style".to_string(), RuleSeverity::Error)]
.into_iter()
.collect();
Expand All @@ -80,6 +83,7 @@ mod test {
},
},
}
.into()
}

#[test]
Expand Down
9 changes: 6 additions & 3 deletions src/rules/md003.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use core::fmt;
use std::rc::Rc;

use comrak::nodes::{Ast, NodeHeading, NodeValue};

Expand All @@ -25,12 +26,12 @@ impl fmt::Display for Style {
}

pub(crate) struct MD003Linter {
context: Context,
context: Rc<Context>,
enforced_style: Option<Style>,
}

impl MD003Linter {
pub fn new(context: Context) -> Self {
pub fn new(context: Rc<Context>) -> Self {
let enforced_style = match context.config.linters.settings.heading_style.style {
HeadingStyle::ATX => Some(Style::Atx),
HeadingStyle::Setext => Some(Style::Setext),
Expand Down Expand Up @@ -86,6 +87,7 @@ pub const MD003: Rule = Rule {
mod test {
use std::collections::HashMap;
use std::path::PathBuf;
use std::rc::Rc;

use crate::config::{
HeadingStyle, LintersSettingsTable, LintersTable, MD003HeadingStyleTable, QuickmarkConfig,
Expand All @@ -96,7 +98,7 @@ mod test {

use super::MD003;

fn test_context(style: HeadingStyle) -> Context {
fn test_context(style: HeadingStyle) -> Rc<Context> {
let severity: HashMap<_, _> = vec![("heading-style".to_string(), RuleSeverity::Error)]
.into_iter()
.collect();
Expand All @@ -111,6 +113,7 @@ mod test {
},
},
}
.into()
}

#[test]
Expand Down
4 changes: 3 additions & 1 deletion src/rules/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::rc::Rc;

use crate::linter::{Context, RuleLinter};

pub mod md001;
Expand All @@ -9,7 +11,7 @@ pub struct Rule {
pub alias: &'static str,
pub tags: &'static [&'static str],
pub description: &'static str,
pub new_linter: fn(Context) -> Box<dyn RuleLinter>,
pub new_linter: fn(Rc<Context>) -> Box<dyn RuleLinter>,
}

pub const ALL_RULES: &[Rule] = &[md001::MD001, md003::MD003];

0 comments on commit 79bf9d6

Please sign in to comment.