use std::path::Path;
use serde_json::Value;
use xuanji::{Outcome, Violation};
use crate::collect::collect_item_dyn_exposures;
use crate::crate_scope::dependency_names;
use crate::driver::run_boundaries;
use crate::dsl::DynTraitBoundary;
use crate::emit::{SingleModuleViolationContext, push_single_module_violations};
use crate::file_scope::resolve_crate;
use crate::finding::{ExposureKind, SemanticFact, shape_finding};
use crate::rules::DYN_TRAIT_RULE;
use crate::shape_scan::{operand_module_findings, shape_module_findings};
pub fn check_dyn_trait(boundaries: &[DynTraitBoundary], manifest_path: &Path) -> Outcome {
run_boundaries(boundaries, manifest_path, check_dyn_trait_boundary)
}
pub(crate) fn check_dyn_trait_boundary(
metadata: &Value,
boundary: &DynTraitBoundary,
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 = if boundary.forbidden_operands.is_empty() {
dyn_module_findings(
src_dir,
&root_file,
&boundary.module,
&boundary.crate_package,
)?
} else {
dyn_operand_module_findings(
src_dir,
&root_file,
&boundary.module,
&boundary.forbidden_operands,
&boundary.crate_package,
&dependency_names(package),
)?
};
push_single_module_violations(
violations,
SingleModuleViolationContext {
src_dir,
root_file: &root_file,
module: &boundary.module,
crate_package: &boundary.crate_package,
rule: DYN_TRAIT_RULE,
reason: &boundary.reason,
severity: boundary.severity,
anchor: boundary.anchor(),
},
findings,
)
}
pub(crate) fn dyn_module_findings(
src_dir: &Path,
root_file: &Path,
module: &str,
crate_package: &str,
) -> Result<Vec<SemanticFact>, String> {
shape_module_findings(
src_dir,
root_file,
module,
crate_package,
collect_item_dyn_exposures,
|exposure| shape_finding(exposure, ExposureKind::DynTrait),
)
}
pub(crate) fn dyn_operand_module_findings(
src_dir: &Path,
root_file: &Path,
module: &str,
forbidden: &[String],
crate_package: &str,
dep_names: &[String],
) -> Result<Vec<SemanticFact>, String> {
operand_module_findings(
src_dir,
root_file,
module,
forbidden,
crate_package,
dep_names,
(ExposureKind::DynTrait, collect_item_dyn_exposures),
)
}