use syn::parse_file;
use crate::reporting::offence::Offence;
use crate::rule::Rule;
use crate::source_file::SourceFile;
pub struct ReadableSourceRule;
impl ReadableSourceRule {
pub const NAME: &'static str = "readable-source";
pub fn new() -> Self {
Self
}
fn line_of(error: &syn::Error) -> usize {
error.span().start().line.max(1)
}
}
impl Default for ReadableSourceRule {
fn default() -> Self {
Self::new()
}
}
impl Rule for ReadableSourceRule {
fn name(&self) -> &'static str {
Self::NAME
}
fn check(&self, file: &SourceFile) -> Vec<Offence> {
match parse_file(&file.contents()) {
Ok(_) => Vec::new(),
Err(error) => vec![
Offence::new(
file.relative_path(),
Self::line_of(&error),
self.name(),
format!("file does not parse as Rust: {error}"),
"correct the syntax error rustc reports for this file, or restore \
the file if it is corrupted"
.to_string(),
)
.with_subject(file.relative_path()),
],
}
}
}