use crate::{make_unmergeable_dir_content_mergeable, FileSystemTree, MergeableFileSystemTree};
use pipe_trait::Pipe;
use std::collections::BTreeMap;
pub trait Node {
type FileContent;
type DirectoryContent;
fn read(self) -> NodeContent<Self::FileContent, Self::DirectoryContent>;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NodeContent<FileContent, DirectoryContent> {
File(FileContent),
Directory(DirectoryContent),
}
impl<Path, FileContent> Node for FileSystemTree<Path, FileContent>
where
Path: Ord,
{
type FileContent = FileContent;
type DirectoryContent = BTreeMap<Path, Self>;
fn read(self) -> NodeContent<FileContent, Self::DirectoryContent> {
match self {
FileSystemTree::File(content) => NodeContent::File(content),
FileSystemTree::Directory(content) => NodeContent::Directory(content),
}
}
}
impl<Path, FileContent> Node for MergeableFileSystemTree<Path, FileContent>
where
Path: Ord,
{
type FileContent = FileContent;
type DirectoryContent = BTreeMap<Path, Self>;
fn read(self) -> NodeContent<FileContent, Self::DirectoryContent> {
match self.into() {
FileSystemTree::File(content) => NodeContent::File(content),
FileSystemTree::Directory(content) => content
.pipe(make_unmergeable_dir_content_mergeable)
.pipe(NodeContent::Directory),
}
}
}