mod error;
mod impl_mergeable_tree;
mod impl_tree;
pub use error::*;
use crate::{Node, NodeContent};
pub trait Build<Name, Error>: Node + Sized
where
Self::DirectoryContent: IntoIterator<Item = (Name, Self)>,
{
type Path: Clone;
fn join(prefix: &Self::Path, name: &Name) -> Self::Path;
fn write_file(path: &Self::Path, content: &Self::FileContent) -> Result<(), Error>;
fn create_dir(path: &Self::Path) -> Result<(), Error>;
fn build(self, path: &Self::Path) -> Result<(), BuildError<Self::Path, Error>> {
let children = match self.read() {
NodeContent::File(content) => {
return Self::write_file(&path, &content).map_err(|error| BuildError {
operation: FailedOperation::WriteFile,
path: path.clone(),
error,
})
}
NodeContent::Directory(children) => children,
};
Self::create_dir(&path).map_err(|error| BuildError {
operation: FailedOperation::CreateDir,
path: path.clone(),
error,
})?;
for (name, child) in children {
child.build(&Self::join(&path, &name))?;
}
Ok(())
}
}