-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathtree_builder.rs
More file actions
71 lines (64 loc) · 2.34 KB
/
tree_builder.rs
File metadata and controls
71 lines (64 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
pub mod info;
pub use info::Info;
use super::{data_tree::DataTree, size};
use rayon::prelude::*;
/// Collection of functions and starting points in order to build a [`DataTree`] with [`From`] or [`Into`].
#[derive(Debug)]
pub struct TreeBuilder<Path, Name, Size, GetInfo, JoinPath>
where
Path: Send + Sync,
Name: Send + Sync,
GetInfo: Fn(&Path) -> Info<Name, Size> + Copy + Send + Sync,
JoinPath: Fn(&Path, &Name) -> Path + Copy + Send + Sync,
Size: size::Size + Send,
{
/// Path to the root.
pub path: Path,
/// Name of the root.
pub name: Name,
/// Function to extract necessary information from `path` (`size` and `children`).
pub get_info: GetInfo,
/// Function to join parent's `path` with a child's name to make the child's `name`.
pub join_path: JoinPath,
/// Deepest level of descendent to store as arrays. The sizes beyond the max depth still count toward total.
pub max_depth: u64,
}
impl<Path, Name, Size, GetInfo, JoinPath> From<TreeBuilder<Path, Name, Size, GetInfo, JoinPath>>
for DataTree<Name, Size>
where
Path: Send + Sync,
Name: Send + Sync,
GetInfo: Fn(&Path) -> Info<Name, Size> + Copy + Send + Sync,
JoinPath: Fn(&Path, &Name) -> Path + Copy + Send + Sync,
Size: size::Size + Send,
{
/// Create a [`DataTree`] from a [`TreeBuilder`].
fn from(builder: TreeBuilder<Path, Name, Size, GetInfo, JoinPath>) -> Self {
let TreeBuilder {
path,
name,
get_info,
join_path,
max_depth,
} = builder;
let Info { size, children } = get_info(&path);
let max_depth = max_depth.saturating_sub(1);
let children: Vec<_> = children
.into_par_iter()
.map(|name| TreeBuilder {
path: join_path(&path, &name),
name,
get_info,
join_path,
max_depth,
})
.map(Self::from)
.collect(); // TODO: this collect can be called into different types depending on whether `max_depth` is 0
let mut tree = DataTree::dir(name, size, children);
if max_depth == 0 {
// TODO: replace this with a more memory efficient method that doesn't require constructing `children` in the first place
tree.drop_children();
}
tree
}
}