use std::path::{Path, PathBuf};
pub struct AuditorPathUtils;
impl AuditorPathUtils {
pub fn resolve_audit_file_path(
source_file_path: &str,
output_directory: &str,
base_name: &str,
) -> Result<(PathBuf, bool), String> {
let source_dir = Path::new(source_file_path)
.parent()
.unwrap_or_else(|| Path::new("../../../../.."));
let primary = source_dir.join(format!("{}.mdix.au", base_name));
if primary.exists() {
return Ok((primary, false));
}
let fallback = Path::new(output_directory).join(format!("{}.mdix.au", base_name));
if fallback.exists() && !Self::same_path(source_dir, Path::new(output_directory)) {
return match std::fs::rename(&fallback, &primary) {
Ok(_) => Ok((primary, true)),
Err(_) => Ok((fallback, false)),
}
}
Ok((primary, false))
}
pub fn base_name(source_file_path: &str) -> Result<String, String> {
Path::new(source_file_path)
.file_stem()
.and_then(|s| s.to_str())
.map(|s| s.to_string())
.ok_or_else(|| format!("Invalid source file path: {}", source_file_path))
}
pub fn same_path(a: &Path, b: &Path) -> bool {
match (a.canonicalize(), b.canonicalize()) {
(Ok(ca), Ok(cb)) => ca == cb,
_ => false,
}
}
}