use crate::finding::qualified_call::QualifiedCall;
use crate::finding::qualified_call_finder::QualifiedCallFinder;
use crate::reporting::offence::Offence;
use crate::rule::Rule;
use crate::source_file::SourceFile;
pub struct ImportedPathsRule;
impl ImportedPathsRule {
pub fn new() -> Self {
Self
}
fn offence(&self, file: &SourceFile, call: &QualifiedCall) -> Offence {
Offence::new(
file.relative_path(),
call.line,
self.name(),
format!(
"`{}` is reached through a path; no import of this file names it",
call.path
),
format!("add `use {};` and call `{}`", call.import(), call.call()),
)
.with_subject(&call.path)
}
}
impl Default for ImportedPathsRule {
fn default() -> Self {
Self::new()
}
}
impl Rule for ImportedPathsRule {
fn name(&self) -> &'static str {
"imported-paths"
}
fn check(&self, file: &SourceFile) -> Vec<Offence> {
QualifiedCallFinder::find(file)
.unwrap_or_default()
.iter()
.map(|call| self.offence(file, call))
.collect()
}
fn check_workspace(&self, _files: &[SourceFile]) -> Vec<Offence> {
Vec::new()
}
fn is_configured(&self) -> bool {
true
}
}