use std::path::{Path, PathBuf};
use crate::error::BaseError;
pub trait BuildDestinationStratTrait {
fn build(&self, source: &Path, dest_root: &Path) -> Result<PathBuf, Box<BaseError>>;
}
pub struct NoopDestinationStrat {}
impl BuildDestinationStratTrait for NoopDestinationStrat {
fn build(&self, _source: &Path, dest_root: &Path) -> Result<PathBuf, Box<BaseError>> {
Ok(dest_root.to_path_buf())
}
}
pub struct PrefixStrippedCountParentStrat {
pub count: usize,
}
impl BuildDestinationStratTrait for PrefixStrippedCountParentStrat {
fn build(&self, source: &Path, dest_root: &Path) -> Result<PathBuf, Box<BaseError>> {
let count = if source.has_root() {
self.count + 1
} else {
self.count
};
let final_destination = source
.components()
.skip(count)
.fold(dest_root.to_path_buf(), |acc, component| {
acc.join(Path::new(Path::new(component.as_os_str())))
});
if final_destination.as_path() == dest_root {
throw_fmt!(
"Can't strip {} components from {}",
self.count,
source.display()
)
}
Ok(final_destination)
}
}