use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use crate::Workspace;
use vvv_core::{Layout, Project};
use crate::EngineError;
#[derive(Debug, Clone, Default)]
pub struct MoveSet {
files: BTreeMap<PathBuf, PathBuf>,
}
impl MoveSet {
pub fn compute(
workspace: &Workspace,
layout: &dyn Layout,
project: &Project,
from: &Path,
to: &Path,
) -> Result<Self, EngineError> {
let mut set = Self::default();
set.add(workspace, from, to)?;
for (f, t) in layout.companions(project, from, to) {
set.add(workspace, &f, &t)?;
}
Ok(set)
}
fn add(&mut self, workspace: &Workspace, from: &Path, to: &Path) -> Result<(), EngineError> {
for path in workspace.vfs().walk(&workspace.absolute(from))? {
let rel = workspace.relative(&path);
let destination = match rel.strip_prefix(from) {
Ok(inside) if inside.as_os_str().is_empty() => to.to_path_buf(),
Ok(inside) => to.join(inside),
Err(_) => continue,
};
self.files.insert(rel, destination);
}
Ok(())
}
pub fn is_empty(&self) -> bool {
self.files.is_empty()
}
pub fn destination(&self, path: &Path) -> Option<&Path> {
self.files.get(path).map(PathBuf::as_path)
}
pub fn iter(&self) -> impl Iterator<Item = (&Path, &Path)> {
self.files.iter().map(|(f, t)| (f.as_path(), t.as_path()))
}
}