use crate::lint::{Finding, Level, Lint, Target};
use crate::scan::Hit;
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub(crate) struct Levels {
directives: Vec<(Target, Level)>,
strict: bool,
granted: Vec<Lint>,
}
impl Levels {
pub(crate) fn new() -> Self {
Self::default()
}
pub(crate) fn set(&mut self, target: Target, level: Level) {
self.directives.push((target, level));
}
pub(crate) fn set_charset(&mut self, level: Level) {
self.set(Target::BARE, level);
}
pub(crate) fn set_strict(&mut self, strict: bool) {
self.strict = strict;
}
pub(crate) fn grant(&mut self, lint: Lint) {
if !self.granted.contains(&lint) {
self.granted.push(lint);
}
}
pub(crate) fn is_granted(&self, lint: Lint) -> bool {
self.granted.contains(&lint)
}
pub(crate) fn level_of(&self, lint: Lint) -> Level {
if self.is_granted(lint) {
return Level::Warn;
}
let stated = self.stated(lint);
if self.strict {
stated.under_strict()
} else {
stated
}
}
pub(crate) fn finding(&self, hit: Hit, lint: Lint) -> Finding {
Finding {
hit,
lint,
level: self.level_of(lint),
}
}
fn stated(&self, lint: Lint) -> Level {
let mut level = lint.default_level();
for (target, stated) in &self.directives {
if level == Level::Forbid {
break;
}
if target.covers(lint) {
level = *stated;
}
}
level
}
}
#[cfg(test)]
mod tests {
use super::Levels;
use crate::lint::{Group, Level, Lint, Target};
use crate::scan::{Hit, Position};
fn hazard() -> Lint {
Lint::new("invisible", Group::Hazard)
}
fn charset() -> Lint {
crate::lint::OUTSIDE_SET
}
fn pedantic() -> Lint {
Lint::new("crlf", Group::Pedantic)
}
fn hit() -> Hit {
let position = Position {
line: 1,
column: 1,
byte: 0,
};
Hit {
position,
character: 'x',
}
}
#[test]
fn the_test_lints_are_the_registered_ones() {
assert_eq!(Lint::named("outside-set"), Some(charset()));
assert_eq!(Lint::named("crlf"), Some(pedantic()));
assert_eq!(Lint::named("invisible"), Some(hazard()));
}
#[test]
fn with_no_directive_a_lint_sits_at_its_groups_default() {
let levels = Levels::new();
assert_eq!(levels.level_of(hazard()), Level::Forbid);
assert_eq!(levels.level_of(charset()), Level::Deny);
assert_eq!(levels.level_of(pedantic()), Level::Allow);
}
#[test]
fn the_later_directive_wins() {
let mut levels = Levels::new();
levels.set_charset(Level::Warn);
levels.set_charset(Level::Allow);
assert_eq!(levels.level_of(charset()), Level::Allow);
levels.set_charset(Level::Deny);
assert_eq!(levels.level_of(charset()), Level::Deny);
}
#[test]
fn a_lint_directive_and_a_group_directive_race_by_order() {
let mut levels = Levels::new();
levels.set(Target::Lint(pedantic()), Level::Deny);
levels.set(Target::Group(Group::Pedantic), Level::Warn);
assert_eq!(levels.level_of(pedantic()), Level::Warn);
levels.set(Target::Lint(pedantic()), Level::Allow);
assert_eq!(levels.level_of(pedantic()), Level::Allow);
}
#[test]
fn a_directive_moves_only_what_it_covers() {
let mut levels = Levels::new();
levels.set_charset(Level::Allow);
assert_eq!(levels.level_of(charset()), Level::Allow);
assert_eq!(levels.level_of(hazard()), Level::Forbid);
assert_eq!(levels.level_of(pedantic()), Level::Allow);
}
#[test]
fn a_group_directive_moves_every_lint_in_that_group() {
let mut levels = Levels::new();
levels.set(Target::Group(Group::Pedantic), Level::Warn);
assert_eq!(levels.level_of(pedantic()), Level::Warn);
let other = Lint::new("confusable", Group::Pedantic);
assert_eq!(levels.level_of(other), Level::Warn);
}
#[test]
fn no_directive_of_any_level_lowers_a_forbidding_group() {
for level in Level::ALL {
let mut levels = Levels::new();
levels.set(Target::Group(Group::Hazard), *level);
levels.set(Target::Lint(hazard()), *level);
assert_eq!(levels.level_of(hazard()), Level::Forbid, "{level:?}");
}
}
#[test]
fn a_forbid_a_rule_raised_is_just_as_final() {
let mut levels = Levels::new();
levels.set_charset(Level::Forbid);
levels.set_charset(Level::Allow);
levels.set(Target::Lint(charset()), Level::Warn);
assert_eq!(levels.level_of(charset()), Level::Forbid);
}
#[test]
fn strict_does_not_lower_a_forbid_either() {
let mut levels = Levels::new();
levels.set(Target::Group(Group::Hazard), Level::Allow);
levels.set_strict(true);
assert_eq!(levels.level_of(hazard()), Level::Forbid);
}
#[test]
fn strict_raises_a_warned_lint_to_deny() {
let mut levels = Levels::new();
levels.set_charset(Level::Warn);
assert_eq!(levels.level_of(charset()), Level::Warn);
levels.set_strict(true);
assert_eq!(levels.level_of(charset()), Level::Deny);
}
#[test]
fn strict_leaves_an_allowed_lint_silent() {
let mut levels = Levels::new();
levels.set_charset(Level::Allow);
levels.set_strict(true);
assert_eq!(levels.level_of(charset()), Level::Allow);
assert_eq!(levels.level_of(pedantic()), Level::Allow);
}
#[test]
fn strict_can_be_turned_back_off() {
let mut levels = Levels::new();
levels.set_charset(Level::Warn);
levels.set_strict(true);
levels.set_strict(false);
assert_eq!(levels.level_of(charset()), Level::Warn);
}
#[test]
fn a_finding_carries_the_level_this_table_resolved() {
let mut levels = Levels::new();
levels.set_charset(Level::Warn);
let finding = levels.finding(hit(), charset());
assert_eq!(finding.level, Level::Warn);
assert_eq!(finding.lint, charset());
assert_eq!(finding.hit, hit());
}
}