use std::collections::HashSet;
use std::path::Path;
use serde_json::Value;
use xuanji::{Outcome, Polarity, Violation};
use crate::containment::{leaf_of, path_leaf, resolve_self_type, under_subtree};
use crate::driver::run_boundaries;
use crate::dsl::ForbiddenMarkerBoundary;
use crate::emit::{MultiModuleViolationContext, push_multi_module_violations};
use crate::file_scope::resolve_crate;
use crate::finding::SemanticFinding;
use crate::resolve::{
BareFallback, canonical_path_str, canonical_self_owner, path_to_string, resolve_path,
};
use crate::rules::FORBIDDEN_MARKER_RULE;
use crate::scan::scan_crate;
pub fn check_forbidden_marker(
boundaries: &[ForbiddenMarkerBoundary],
manifest_path: &Path,
) -> Outcome {
run_boundaries(boundaries, manifest_path, check_forbidden_marker_boundary)
}
pub(crate) fn check_forbidden_marker_boundary(
metadata: &Value,
boundary: &ForbiddenMarkerBoundary,
violations: &mut Vec<Violation>,
) -> Result<(), String> {
let (_package, root_file, src_dir) = resolve_crate(metadata, &boundary.crate_package)?;
let src_dir = src_dir.as_path();
let findings = forbidden_marker_findings(
src_dir,
&root_file,
&boundary.module,
&boundary.forbidden,
&boundary.crate_package,
)?;
push_multi_module_violations(
violations,
MultiModuleViolationContext {
src_dir,
root_file: &root_file,
target: &boundary.module,
crate_package: &boundary.crate_package,
rule: FORBIDDEN_MARKER_RULE,
reason: &boundary.reason,
severity: boundary.severity,
anchor: boundary.anchor(),
polarity: Polarity::DenyBreach,
},
findings,
);
Ok(())
}
pub(crate) fn forbidden_marker_findings(
src_dir: &Path,
root_file: &Path,
subtree: &str,
forbidden: &[String],
crate_package: &str,
) -> Result<Vec<(String, String)>, String> {
let scan = scan_crate(src_dir, root_file, crate_package, &HashSet::new())?;
let subtree = canonical_path_str(subtree);
let defined: HashSet<&str> = scan
.type_defs
.iter()
.map(|td| td.canonical.as_str())
.collect();
let mut findings = Vec::new();
for entry in forbidden {
let entry_leaf = leaf_of(entry);
for td in &scan.type_defs {
if !under_subtree(&td.canonical, &subtree) {
continue;
}
for (ordinal, derived) in td.derives.iter().enumerate() {
let derived_leaf =
resolve_path(derived, &td.uses, &td.module, BareFallback::Ignore)
.map(|p| leaf_of(&p).to_string())
.unwrap_or_else(|| path_leaf(derived));
if derived_leaf == entry_leaf {
let marker =
path_to_string(derived).unwrap_or_else(|| format!("{entry}<_#{ordinal}>"));
findings.push((
SemanticFinding::ForbiddenDerive {
marker,
canonical: td.canonical.clone(),
}
.to_string(),
td.module.clone(),
));
}
}
}
for (ordinal, site) in scan.impls.iter().enumerate() {
let trait_leaf = resolve_path(
&site.trait_path,
&site.uses,
&site.module,
BareFallback::Ignore,
)
.map(|p| leaf_of(&p).to_string())
.unwrap_or_else(|| path_leaf(&site.trait_path));
if trait_leaf != entry_leaf {
continue;
}
let Some(landing) = resolve_self_type(
&site.self_ty,
&site.uses,
&site.module,
&scan.alias_targets,
&scan.reexports,
) else {
continue; };
if !(under_subtree(&landing, &subtree) && defined.contains(landing.as_str())) {
continue;
}
let marker =
path_to_string(&site.trait_path).unwrap_or_else(|| format!("{entry}<_#{ordinal}>"));
let owner = canonical_self_owner(&site.self_ty, &site.uses, &site.module, ordinal);
findings.push((
SemanticFinding::ForbiddenImpl {
marker,
owner,
module: site.module.clone(),
}
.to_string(),
site.module.clone(),
));
}
}
findings.sort();
findings.dedup_by(|a, b| a.0 == b.0);
Ok(findings)
}