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 BorrowedPath: ToOwned<Owned = Self::OwnedPath> + ?Sized;
type OwnedPath: AsRef<Self::BorrowedPath>;
fn join(prefix: &Self::BorrowedPath, name: &Name) -> Self::OwnedPath;
fn write_file(path: &Self::BorrowedPath, content: &Self::FileContent) -> Result<(), Error>;
fn create_dir(path: &Self::BorrowedPath) -> Result<(), Error>;
fn build<Path>(self, path: Path) -> Result<(), BuildError<Self::OwnedPath, Error>>
where
Path: AsRef<Self::BorrowedPath>,
{
let path = path.as_ref();
let children = match self.read() {
NodeContent::File(content) => {
return Self::write_file(path, &content).map_err(|error| BuildError {
operation: FailedOperation::WriteFile,
path: path.to_owned(),
error,
})
}
NodeContent::Directory(children) => children,
};
Self::create_dir(path).map_err(|error| BuildError {
operation: FailedOperation::CreateDir,
path: path.to_owned(),
error,
})?;
for (name, child) in children {
child.build(Self::join(path, &name))?;
}
Ok(())
}
}