use crate::finding::registry_parser::RegistryParser;
use crate::finding::registry_policy::RegistryPolicy;
use crate::reporting::offence::Offence;
use crate::rule::Rule;
use crate::source_file::SourceFile;
pub struct ModuleRegistryRule;
impl ModuleRegistryRule {
pub const TESTS_ROOT: &'static str = "tests/";
pub fn new() -> Self {
Self
}
fn applies_to(file: &SourceFile) -> bool {
!file.relative_path().starts_with(Self::TESTS_ROOT) && Self::is_registry(file)
}
fn is_registry(file: &SourceFile) -> bool {
matches!(
file.relative_path().rsplit('/').next(),
Some("lib.rs") | Some("mod.rs")
)
}
}
impl Default for ModuleRegistryRule {
fn default() -> Self {
Self::new()
}
}
impl Rule for ModuleRegistryRule {
fn name(&self) -> &'static str {
"module-registry"
}
fn check(&self, file: &SourceFile) -> Vec<Offence> {
if !Self::applies_to(file) {
return Vec::new();
}
RegistryParser::strays(file, RegistryPolicy::source())
.unwrap_or_default()
.into_iter()
.map(|stray| {
Offence::new(
file.relative_path(),
stray.line,
self.name(),
format!(
"{} does not belong in a module registry, which holds the \
header, inner attributes, `extern crate alloc;` and pub mod \
declarations only",
stray.label
),
format!("move {} into a module of its own", stray.label),
)
.with_subject(&stray.label)
})
.collect()
}
fn check_workspace(&self, _files: &[SourceFile]) -> Vec<Offence> {
Vec::new()
}
fn is_configured(&self) -> bool {
true
}
}