use super::remedy::Case;
use crate::charset::CharSet;
use crate::fix::Map;
use crate::judge::{Checker, Config, File, Looked, files, judged};
use crate::lint::{Finding, Group, Levels, Lint, exit_code};
use crate::render::{self, Batch, Format, Remedy, Shape, Skipped};
use crate::scan::Unreadable;
use std::path::Path;
pub(super) struct Row {
pub path: String,
pub set: String,
pub findings: Vec<Finding>,
pub remedies: Vec<Remedy>,
}
pub(super) struct Skip {
pub path: String,
pub reason: Unreadable,
}
pub(super) struct Report {
pub text: String,
pub code: u8,
pub note: String,
}
struct Walk {
checker: Checker,
map: Option<Map>,
}
impl Walk {
fn gather(&self, root: &Path, paths: &[String]) -> Result<Found, String> {
let mut found = Found::default();
for file in files(root, paths)? {
let File { shown, text, .. } = file?;
let (set, levels) = self.checker.shared_law(&shown)?;
let looked = match text {
Ok(text) => self.judged(&text, &set, &levels),
Err(reason) => (Looked::Unread(reason), Vec::new()),
};
found.absorb(shown, &set, looked);
}
Ok(found)
}
fn judged(&self, text: &str, set: &CharSet, levels: &Levels) -> Judged {
let judge = self.checker.judge(set);
let findings = judged(text, &judge, levels);
let remedies = match &self.map {
Some(map) if !findings.is_empty() => {
Case::new(&judge, levels, text).remedies(map, &findings)
}
_ => Vec::new(),
};
(Looked::Findings(findings), remedies)
}
}
type Judged = (Looked, Vec<Remedy>);
#[derive(Default)]
struct Found {
rows: Vec<Row>,
skips: Vec<Skip>,
}
impl Found {
fn absorb(&mut self, path: String, set: &CharSet, looked: Judged) {
match looked {
(Looked::Unread(reason), _) => {
self.skips.push(Skip { path, reason });
}
(Looked::Findings(found), _) if found.is_empty() => {}
(Looked::Findings(findings), remedies) => {
self.rows
.push(Row::new(path, &set.name, findings, remedies));
}
}
}
}
impl Row {
pub(super) fn new(
path: String,
set: &str,
mut findings: Vec<Finding>,
remedies: Vec<Remedy>,
) -> Self {
findings.shrink_to_fit();
let set = set.to_owned();
Self {
path,
set,
findings,
remedies,
}
}
fn batch<'r>(&'r self, at: usize, findings: &'r [Finding]) -> Batch<'r> {
let end = at.saturating_add(findings.len());
let set = findings.first().map_or(self.set.as_str(), |first| {
judged_against(first.lint, &self.set)
});
Batch {
path: &self.path,
set,
findings,
remedies: self.remedies.get(at..end).unwrap_or(&[]),
}
}
}
pub(super) fn judged_against(lint: Lint, set: &str) -> &str {
if lint.group == Group::Hazard {
Group::Hazard.name()
} else {
set
}
}
pub(super) fn batches(source: &Row) -> impl Iterator<Item = Batch<'_>> {
let named = |f: &Finding| judged_against(f.lint, &source.set);
let mut start = 0_usize;
source
.findings
.chunk_by(move |a, b| named(a) == named(b))
.map(move |findings| {
let at = start;
start = start.saturating_add(findings.len());
source.batch(at, findings)
})
}
#[cfg(test)]
pub(super) fn run(
config: &Config,
paths: &[String],
format: Format,
) -> Result<Report, String> {
shaped(config, paths, format, Shape::default())
}
pub(super) fn shaped(
config: &Config,
paths: &[String],
format: Format,
shape: Shape,
) -> Result<Report, String> {
let remedied = format == Format::Json || shape.summary;
let walk = Walk {
checker: Checker::configured(config)?,
map: remedied.then(|| config.map()).transpose()?,
};
let found = walk.gather(&config.root, paths)?;
Ok(reported(&found, format, shape))
}
fn reported(found: &Found, format: Format, shape: Shape) -> Report {
let batches: Vec<Batch<'_>> = found.rows.iter().flat_map(batches).collect();
let skipped: Vec<Skipped<'_>> = found.skips.iter().map(skip).collect();
Report {
text: render::check_batches(format, &batches, &skipped, shape),
code: found_code(&found.rows).max(unreadable_code(&found.skips)),
note: render::check_note(format, &batches),
}
}
pub(super) fn found_code(rows: &[Row]) -> u8 {
rows.iter()
.map(|row| exit_code(&row.findings))
.max()
.unwrap_or(0)
}
pub(super) fn unreadable_code(skips: &[Skip]) -> u8 {
let broken =
|skip: &Skip| matches!(skip.reason, Unreadable::NotUtf8 { .. });
u8::from(skips.iter().any(broken))
}
pub(super) fn skip(source: &Skip) -> Skipped<'_> {
Skipped {
path: &source.path,
reason: source.reason,
}
}
#[cfg(test)]
#[path = "check_test.rs"]
mod tests;