use crate::errors;
use crate::errors::OrandaError;
use camino::{Utf8Path, Utf8PathBuf};
pub fn determine_path(
root_path: impl AsRef<Utf8Path>,
member_path: &Option<impl AsRef<Utf8Path>>,
path: impl AsRef<Utf8Path>,
) -> errors::Result<Option<Utf8PathBuf>> {
let root_path = root_path.as_ref();
let member_path = member_path.as_ref().map(|p| p.as_ref());
let path = path.as_ref();
if path.is_absolute() {
return Ok(Some(path.to_owned()));
}
let path_plus_member = if let Some(member_path) = member_path {
if member_path.is_absolute() {
let mut owned = Utf8PathBuf::new();
owned.push(member_path);
owned.push(path);
owned.canonicalize_utf8()
} else {
let mut owned = Utf8PathBuf::new();
owned.push(root_path);
owned.push(member_path);
owned.push(path);
owned.canonicalize_utf8()
}
} else {
let mut owned = Utf8PathBuf::new();
owned.push(root_path);
owned.push(path);
owned.canonicalize_utf8()
};
match path_plus_member {
Ok(path) => {
Ok(Some(pathdiff::diff_utf8_paths(&path, root_path).ok_or(
OrandaError::PathdiffError {
root_path: root_path.to_string(),
path: path.to_string(),
},
)?))
}
Err(_) => {
Ok(None)
}
}
}