use std::path::{Component, Path, PathBuf};
pub fn resolve_target_path(source_dir: &Path, target_path: &Path) -> PathBuf {
if matches!(
target_path.components().next(),
Some(Component::CurDir | Component::ParentDir),
) {
source_dir.join(target_path)
} else {
target_path.to_owned()
}
}
#[cfg(test)]
mod tests {
use crate::path_util::resolve_target_path;
use std::path::{Path, PathBuf};
#[test]
fn resolve_target_path_parent_dir() {
assert_eq!(
resolve_target_path(Path::new("docs/guidelines.md"), Path::new("../src/main.rs")),
PathBuf::from("docs/guidelines.md/../src/main.rs"),
);
}
#[test]
fn resolve_target_path_current_dir() {
assert_eq!(
resolve_target_path(Path::new("docs/guidelines.md"), Path::new("./src/main.rs")),
PathBuf::from("docs/guidelines.md/./src/main.rs"),
);
}
#[test]
fn resolve_target_path_non_relative() {
assert_eq!(
resolve_target_path(Path::new("docs/guidelines.md"), Path::new("src/main.rs")),
PathBuf::from("src/main.rs"),
);
}
}