Skip to content

Commit

Permalink
Improve format_or_lint_with_disable_rules method.
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Apr 30, 2024
1 parent 61bc877 commit 1fda6a8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
6 changes: 6 additions & 0 deletions autocorrect/src/result/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ pub enum Severity {
Warning = 2,
}

impl Default for Severity {
fn default() -> Self {
Severity::Pass
}
}

impl Severity {
pub fn is_error(&self) -> bool {
self == &Severity::Error
Expand Down
11 changes: 6 additions & 5 deletions autocorrect/src/rule/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,17 @@ pub(crate) fn format_or_lint_with_disable_rules(
lint: bool,
disable_rules: &HashMap<String, bool>,
) -> RuleResult {
let mut result = RuleResult::new(text);
let mut result = RuleResult::default();

// skip if not has CJK
if CJK_RE.is_match(text) {
result.out = String::from("");
let mut part = String::new();
for ch in text.chars() {
part.push(ch);

// Is next char is newline or space, break part to format
if ch == ' ' || ch == '\n' || ch == '\r' {
let mut sub_result = RuleResult::new(&part.clone());
if matches!(ch, ' ' | '\n' | '\r') {
let mut sub_result = RuleResult::new(&part);
sub_result.severity = result.severity;

part.clear();
Expand All @@ -110,14 +109,16 @@ pub(crate) fn format_or_lint_with_disable_rules(
}

if !part.is_empty() {
let mut sub_result = RuleResult::new(&part.clone());
let mut sub_result = RuleResult::new(&part);
sub_result.severity = result.severity;

format_part(&mut sub_result, lint, disable_rules);

result.out.push_str(&sub_result.out);
result.severity = sub_result.severity;
}
} else {
result.out = text.to_string();
}

format_after_rules(&mut result, lint, disable_rules);
Expand Down
3 changes: 2 additions & 1 deletion autocorrect/src/rule/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub(crate) struct Rule {
pub format_fn: fn(input: &str) -> String,
}

#[derive(Default)]
pub(crate) struct RuleResult {
pub out: String,
pub severity: Severity,
Expand All @@ -16,7 +17,7 @@ impl RuleResult {
pub fn new(input: &str) -> Self {
Self {
out: input.to_string(),
severity: Severity::Pass,
..Default::default()
}
}
}
Expand Down

0 comments on commit 1fda6a8

Please sign in to comment.