use crate::lint::{Finding, Hazards, Lint};
use crate::scan::{Hit, scan_bytes, scan_str};
const CONTROL: &str = "control-character";
pub(crate) enum Unruled {
Text(Vec<Finding>),
NotText(Vec<Finding>),
}
pub(crate) fn unruled(bytes: &[u8], hazards: &Hazards) -> Unruled {
match scan_bytes(bytes, |c| !hazards.contains(c)) {
Ok((text, hits)) => Unruled::Text(found(text, hits, hazards)),
Err(_) => Unruled::NotText(lossy(bytes, hazards)),
}
}
fn found(text: &str, hits: Vec<Hit>, hazards: &Hazards) -> Vec<Finding> {
let exempt = hazards.exempt(text);
hazards_in(hits, |hit| hazards.lint_at(hit, &exempt))
}
fn lossy(bytes: &[u8], hazards: &Hazards) -> Vec<Finding> {
let text = String::from_utf8_lossy(bytes);
let hits = scan_str(&text, |c| !hazards.contains(c));
let all = found(&text, hits, hazards);
all.into_iter().filter(|f| f.lint.name != CONTROL).collect()
}
pub(crate) fn hazards_in(
hits: Vec<Hit>,
lint: impl Fn(Hit) -> Option<Lint>,
) -> Vec<Finding> {
hits.into_iter()
.filter_map(|hit| {
let lint = lint(hit)?;
let level = lint.default_level();
Some(Finding { hit, lint, level })
})
.collect()
}