Skip to content

Commit

Permalink
fix(rules): use default config and optional a config file in current …
Browse files Browse the repository at this point in the history
…working dir
  • Loading branch information
Matthias Zaunseder committed Feb 18, 2024
1 parent 82452d6 commit 91bb190
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
3 changes: 3 additions & 0 deletions commitguard.config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[rules]

scope-empty = ["error", "always"]
8 changes: 0 additions & 8 deletions src/commitguard.config.json

This file was deleted.

29 changes: 24 additions & 5 deletions src/rules/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path;

use config::Config;
use serde::Deserialize;

Expand All @@ -7,13 +9,13 @@ pub mod scope_empty;
pub mod scope_enum;
pub mod scope_max_length;

pub(crate) trait Rule {
pub trait Rule {
fn run(&self, commit: &Commit) -> Option<miette::Report>;
}

/// Severity of the rule
#[derive(Debug, Deserialize, PartialEq)]
pub(crate) enum Severity {
pub enum Severity {
/// Turn off the rule
#[serde(rename = "off")]
Off,
Expand All @@ -27,7 +29,7 @@ pub(crate) enum Severity {

/// When the rule should be applied
#[derive(Debug, Deserialize)]
pub(crate) enum Condition {
pub enum Condition {
/// The options should "never" be found (e.g. in a list of disallowed values)
#[serde(rename = "never")]
Never,
Expand Down Expand Up @@ -68,12 +70,15 @@ enum TargetCase {
/// Options for all rules without options
#[derive(Debug, Deserialize)]
pub struct NoOpts(Severity, Condition);

/// Options for all enum rules
#[derive(Debug, Deserialize)]
pub struct EnumOpts(Severity, Condition, Vec<String>);

/// Options for all length rules
#[derive(Debug, Deserialize)]
pub struct LengthOpts(Severity, usize);

/// Options for all case rules
#[derive(Debug, Deserialize)]
pub struct CaseOpts(Severity, Condition, TargetCase);
Expand Down Expand Up @@ -135,9 +140,23 @@ impl LintResult {
}

pub fn run(commit: &Commit) -> LintResult {
let pwd = std::env::current_dir().unwrap_or_else(|_e| path::PathBuf::from("/"));
println!("Current directory: {}", pwd.display());

let settings = Config::builder()
.add_source(config::File::from_str(
r#"
[rules]
scope-empty = ["error", "never"]
scope-enum = ["error", "always", ["foo", "bar", "baz"]]
scope-max-length = ["error", 20]
scope-case = ["error", "always", "lower-case"]
"#,
config::FileFormat::Toml,
))
// Source can be `commitlint.config.toml` or `commitlint.config.json``
.add_source(config::File::with_name("src/commitguard.config"))
.add_source(config::File::from(pwd.join("commitguard.config")).required(false))
.build()
.unwrap();

Expand Down Expand Up @@ -183,5 +202,5 @@ pub fn run(commit: &Commit) -> LintResult {
}
}

return lint_result;
lint_result
}
4 changes: 2 additions & 2 deletions src/rules/scope_empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::parser::Commit;
use super::{Condition, NoOpts, Rule, Severity};
use miette::{miette, LabeledSpan, Report};

pub(crate) struct ScopeEmptyRule {
pub(crate) opts: NoOpts,
pub struct ScopeEmptyRule {
pub opts: NoOpts,
}

impl Rule for ScopeEmptyRule {
Expand Down

0 comments on commit 91bb190

Please sign in to comment.