use crate::{make_unmergeable_dir_content_mergeable, FileSystemTree, MergeableFileSystemTree}; use pipe_trait::Pipe; use std::collections::BTreeMap; /// Node of a filesystem tree. pub trait Node { /// Content of the node if it is a file. type FileContent; /// Content of the node if it is a directory. type DirectoryContent; /// Read the content of the node. fn read(self) -> NodeContent; } /// Content of a node in the filesystem tree /// /// **Generic parameters:** /// * `FileContent`: Content of the node if it is a file. /// * `DirectoryContent`: Content of the node if it is a directory. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum NodeContent { /// The node is a file. File(FileContent), /// The node is a directory. Directory(DirectoryContent), } impl Node for FileSystemTree where Path: Ord, { type FileContent = FileContent; type DirectoryContent = BTreeMap; fn read(self) -> NodeContent { match self { FileSystemTree::File(content) => NodeContent::File(content), FileSystemTree::Directory(content) => NodeContent::Directory(content), } } } impl Node for MergeableFileSystemTree where Path: Ord, { type FileContent = FileContent; type DirectoryContent = BTreeMap; fn read(self) -> NodeContent { match self.into() { FileSystemTree::File(content) => NodeContent::File(content), FileSystemTree::Directory(content) => content .pipe(make_unmergeable_dir_content_mergeable) .pipe(NodeContent::Directory), } } }