use std::path::{Path, PathBuf};
use anyhow::Context;
pub fn map_path(weaveconfig_root: &Path, path: &Path) -> Result<PathBuf, anyhow::Error> {
let canonical_root = weaveconfig_root
.canonicalize()
.with_context(|| format!("Failed to canonicalize root path: {}", weaveconfig_root.display()))?;
let canonical_path = path
.canonicalize()
.with_context(|| format!("Failed to canonicalize path: {}", path.display()))?;
let trimmed_root = canonical_root.parent().context("Root has no parent")?;
let relative_path = canonical_path
.strip_prefix(&canonical_root)
.with_context(|| {
format!(
"Path {} is not within root {}",
canonical_path.display(),
canonical_root.display()
)
})?;
Ok(trimmed_root.join(relative_path))
}