Skip to content

Commit

Permalink
feat: add severity "off" to turn off rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Zaunseder committed Feb 2, 2024
1 parent 3e3cec5 commit 361bc1a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
34 changes: 23 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,34 @@ use serde::Deserialize;

fn rule_scope_enum(commit: &Commit, opts: &ScopeEnumOpts) -> Option<Report> {
let severity = &opts.0;
let when = &opts.1;
let condition = &opts.1;
let scopes = &opts.2;

if severity == &Severity::Off {
return None;
}

if let Some(scope) = &commit.scope {
let is_in_scopes = scopes.contains(&scope.as_str().to_string());
let is_valid = match when {
When::Never => !is_in_scopes,
When::Always => is_in_scopes,
let is_in_scopes = scopes.contains(&scope.to_string());
let is_valid = match condition {
Condition::Never => !is_in_scopes,
Condition::Always => is_in_scopes,
};
if !is_valid {
return Some(
miette!(
severity = match severity {
Severity::Warning => miette::Severity::Warning,
Severity::Error => miette::Severity::Error,
Severity::Off => miette::Severity::Advice,
},
labels = vec![LabeledSpan::at(
scope.start()..scope.end(),
"not allowed scope"
),],
help = String::from("scope must") + match when {
When::Never => " not",
When::Always => "",
help = String::from("scope must") + match condition {
Condition::Never => " not",
Condition::Always => "",
} + " be one of " + &scopes.join(", "),
code = "rule/scope-enum",
url = "https://example.com",
Expand All @@ -44,25 +49,32 @@ fn rule_scope_enum(commit: &Commit, opts: &ScopeEnumOpts) -> Option<Report> {
}

/// Severity of the rule
#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, PartialEq)]
enum Severity {
/// Turn off the rule
#[serde(rename = "off")]
Off,
/// Warn about the violation of a rule
#[serde(rename = "warning")]
Warning,
/// Error about the violation of a rule
#[serde(rename = "error")]
Error,
}

/// When the rule should be applied
#[derive(Debug, Deserialize)]
enum When {
enum Condition {
/// The options should "never" be found (e.g. in a list of disallowed values)
#[serde(rename = "never")]
Never,
/// The options should "always" be found (e.g. in a list of allowed values)
#[serde(rename = "always")]
Always,
}

/// Options for the scope-enum rule
type ScopeEnumOpts = (Severity, When, Vec<String>);
type ScopeEnumOpts = (Severity, Condition, Vec<String>);

/// Config all the rules
#[derive(Debug, Deserialize)]
Expand Down
6 changes: 3 additions & 3 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ impl<'a> CommitSpan<'a> {
}
}

pub fn as_str(&self) -> &'a str {
self.input
pub fn to_string(&self) -> String {
self.input.to_string()
}

pub fn start(&self) -> usize {
Expand Down Expand Up @@ -76,7 +76,7 @@ pub fn parse_commit(commit_msg: &str) -> Commit {
for pair in pairs {
match pair.as_rule() {
Rule::commit => {
commit.raw = String::from(pair.as_str());
commit.raw = pair.as_str().to_string();

for inner_pair in pair.into_inner() {
match inner_pair.as_rule() {
Expand Down

0 comments on commit 361bc1a

Please sign in to comment.