use std::path::Path;
use serde_json::Value;
use xuanji::{Outcome, Violation};
use crate::collect::collect_item_async_exposures;
use crate::driver::run_boundaries;
use crate::dsl::AsyncExposureBoundary;
use crate::emit::{SingleModuleViolationContext, push_single_module_violations};
use crate::file_scope::resolve_crate;
use crate::module_resolve::resolve_module_items;
use crate::resolve::collect_uses;
use crate::rules::ASYNC_EXPOSURE_RULE;
pub fn check_async_exposure(boundaries: &[AsyncExposureBoundary], manifest_path: &Path) -> Outcome {
run_boundaries(boundaries, manifest_path, check_async_exposure_boundary)
}
pub(crate) fn check_async_exposure_boundary(
metadata: &Value,
boundary: &AsyncExposureBoundary,
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 = async_exposure_module_findings(
src_dir,
&root_file,
&boundary.module,
&boundary.crate_package,
)?;
push_single_module_violations(
violations,
SingleModuleViolationContext {
src_dir,
root_file: &root_file,
module: &boundary.module,
crate_package: &boundary.crate_package,
rule: ASYNC_EXPOSURE_RULE,
reason: &boundary.reason,
severity: boundary.severity,
anchor: boundary.anchor(),
},
findings,
)
}
pub(crate) fn async_exposure_module_findings(
src_dir: &Path,
root_file: &Path,
module: &str,
crate_package: &str,
) -> Result<Vec<String>, String> {
let items = resolve_module_items(src_dir, root_file, module, crate_package)?;
let uses = collect_uses(&items);
let mut found = Vec::new();
for (ordinal, item) in items.iter().enumerate() {
collect_item_async_exposures(item, module, &uses, ordinal, &mut found);
}
found.sort();
found.dedup();
Ok(found)
}