use std::collections::HashMap;
use std::path::{Path, PathBuf};
use serde_json::Value;
use crate::errors::{crate_not_found_error, missing_src_error};
use crate::module_resolve::resolve_module_file;
use xingbiao::{crate_root_file, find_package};
pub(crate) fn seam_file(
findings: &[String],
src_dir: &Path,
root_file: &Path,
module: &str,
crate_package: &str,
) -> Result<Option<String>, String> {
if findings.is_empty() {
return Ok(None);
}
Ok(Some(
resolve_module_file(src_dir, root_file, module, crate_package)?
.display()
.to_string(),
))
}
pub(crate) fn per_finding_file(
module: &str,
src_dir: &Path,
root_file: &Path,
crate_package: &str,
cache: &mut HashMap<String, Option<String>>,
) -> Option<String> {
if let Some(cached) = cache.get(module) {
return cached.clone();
}
let file = resolve_module_file(src_dir, root_file, module, crate_package)
.ok()
.map(|path| path.display().to_string());
cache.insert(module.to_string(), file.clone());
file
}
pub(crate) fn resolve_crate<'m>(
metadata: &'m Value,
crate_package: &str,
) -> Result<(&'m Value, PathBuf, PathBuf), String> {
let package = find_package(metadata, crate_package)
.ok_or_else(|| crate_not_found_error(crate_package))?;
let root_file = crate_root_file(package).ok_or_else(|| missing_src_error(crate_package))?;
let src_dir = root_file
.parent()
.ok_or_else(|| missing_src_error(crate_package))?
.to_path_buf();
Ok((package, root_file, src_dir))
}