use std::path::{Path, PathBuf};
#[must_use]
pub(crate) fn canonical_key(path: &str) -> String {
let p = Path::new(path);
if let Ok(c) = p.canonicalize() {
return c.display().to_string();
}
if let (Some(parent), Some(file)) = (p.parent(), p.file_name())
&& let Ok(parent_c) = parent.canonicalize()
{
return parent_c.join(file).display().to_string();
}
let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
if p.is_absolute() {
p.display().to_string()
} else {
cwd.join(p).display().to_string()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn canonical_key_falls_back_to_absolutized_for_nonexistent_path() {
let key = canonical_key("/tmp/does-not-exist-xyz-12345");
assert!(key.ends_with("/does-not-exist-xyz-12345"));
}
#[test]
fn canonical_key_collapses_dot_components() {
let dir = tempfile::tempdir().unwrap();
let target = dir.path().join("file.txt");
std::fs::write(&target, "x").unwrap();
let direct = canonical_key(target.to_str().unwrap());
let with_dot = canonical_key(&format!("{}/./file.txt", dir.path().display()));
assert_eq!(direct, with_dot);
}
}