use marque_rules::{AppliedFix, Diagnostic};
#[non_exhaustive]
#[derive(Debug, Default)]
pub struct LintResult {
pub diagnostics: Vec<Diagnostic>,
pub truncated: bool,
pub candidates_processed: usize,
pub candidates_total: usize,
}
impl LintResult {
pub fn is_clean(&self) -> bool {
self.diagnostics.is_empty()
}
pub fn error_count(&self) -> usize {
use marque_rules::Severity;
self.diagnostics
.iter()
.filter(|d| d.severity == Severity::Error)
.count()
}
pub fn warn_count(&self) -> usize {
use marque_rules::Severity;
self.diagnostics
.iter()
.filter(|d| d.severity == Severity::Warn)
.count()
}
pub fn info_count(&self) -> usize {
use marque_rules::Severity;
self.diagnostics
.iter()
.filter(|d| d.severity == Severity::Info)
.count()
}
pub fn suggest_count(&self) -> usize {
use marque_rules::Severity;
self.diagnostics
.iter()
.filter(|d| d.severity == Severity::Suggest)
.count()
}
pub fn fix_count(&self) -> usize {
use marque_rules::Severity;
self.diagnostics
.iter()
.filter(|d| d.severity == Severity::Fix && d.fix.is_some())
.count()
}
}
#[derive(Debug)]
pub struct FixResult {
pub source: Vec<u8>,
pub applied: Vec<AppliedFix>,
pub remaining_diagnostics: Vec<Diagnostic>,
}
#[cfg(test)]
#[cfg_attr(coverage_nightly, coverage(off))]
mod tests {
use super::*;
use marque_core::Span;
use marque_rules::{Diagnostic, RuleId, Severity};
#[test]
fn is_clean_returns_true_when_no_diagnostics() {
let clean_result = LintResult {
diagnostics: vec![],
..Default::default()
};
assert!(clean_result.is_clean());
}
#[test]
fn is_clean_returns_false_when_has_diagnostics() {
let dirty_result = LintResult {
diagnostics: vec![Diagnostic::new(
RuleId::new("E001"),
Severity::Error,
Span::new(0, 0),
"test",
"test",
None,
)],
..Default::default()
};
assert!(!dirty_result.is_clean());
}
#[test]
fn info_count_isolates_info_from_error_and_warn() {
let result = LintResult {
diagnostics: vec![
Diagnostic::new(
RuleId::new("W034"),
Severity::Info,
Span::new(0, 0),
"info one",
"test",
None,
),
Diagnostic::new(
RuleId::new("W034"),
Severity::Info,
Span::new(0, 0),
"info two",
"test",
None,
),
Diagnostic::new(
RuleId::new("W003"),
Severity::Warn,
Span::new(0, 0),
"warn",
"test",
None,
),
Diagnostic::new(
RuleId::new("E001"),
Severity::Error,
Span::new(0, 0),
"err",
"test",
None,
),
],
..Default::default()
};
assert_eq!(result.info_count(), 2);
assert_eq!(result.warn_count(), 1);
assert_eq!(result.error_count(), 1);
assert_eq!(result.fix_count(), 0);
}
}