build_fs_tree/
node.rs

1use crate::{make_unmergeable_dir_content_mergeable, FileSystemTree, MergeableFileSystemTree};
2use pipe_trait::Pipe;
3use std::collections::BTreeMap;
4
5/// Node of a filesystem tree.
6pub trait Node {
7    /// Content of the node if it is a file.
8    type FileContent;
9    /// Content of the node if it is a directory.
10    type DirectoryContent;
11    /// Read the content of the node.
12    fn read(self) -> NodeContent<Self::FileContent, Self::DirectoryContent>;
13}
14
15/// Content of a node in the filesystem tree
16///
17/// **Generic parameters:**
18/// * `FileContent`: Content of the node if it is a file.
19/// * `DirectoryContent`: Content of the node if it is a directory.
20#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21pub enum NodeContent<FileContent, DirectoryContent> {
22    /// The node is a file.
23    File(FileContent),
24    /// The node is a directory.
25    Directory(DirectoryContent),
26}
27
28impl<Path, FileContent> Node for FileSystemTree<Path, FileContent>
29where
30    Path: Ord,
31{
32    type FileContent = FileContent;
33    type DirectoryContent = BTreeMap<Path, Self>;
34
35    fn read(self) -> NodeContent<FileContent, Self::DirectoryContent> {
36        match self {
37            FileSystemTree::File(content) => NodeContent::File(content),
38            FileSystemTree::Directory(content) => NodeContent::Directory(content),
39        }
40    }
41}
42
43impl<Path, FileContent> Node for MergeableFileSystemTree<Path, FileContent>
44where
45    Path: Ord,
46{
47    type FileContent = FileContent;
48    type DirectoryContent = BTreeMap<Path, Self>;
49
50    fn read(self) -> NodeContent<FileContent, Self::DirectoryContent> {
51        match self.into() {
52            FileSystemTree::File(content) => NodeContent::File(content),
53            FileSystemTree::Directory(content) => content
54                .pipe(make_unmergeable_dir_content_mergeable)
55                .pipe(NodeContent::Directory),
56        }
57    }
58}