Skip to content

Commit a67e019

Browse files
committed
feat(api): add max_depth to the tree builder
1 parent 879ee7d commit a67e019

11 files changed

Lines changed: 43 additions & 7 deletions

File tree

src/app/sub.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ where
7676
reporter: &reporter,
7777
root,
7878
size_getter,
79+
max_depth: max_depth.get() as u64,
7980
}
8081
.into()
8182
});
@@ -100,6 +101,7 @@ where
100101
OsStringDisplay::os_string_from("(total)"),
101102
Size::default(),
102103
children,
104+
max_depth.get() as u64,
103105
)
104106
};
105107

src/data_tree/constructors.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ use crate::size;
33

44
impl<Name, Size: size::Size> DataTree<Name, Size> {
55
/// Create a tree representation of a directory.
6-
pub fn dir(name: Name, inode_size: Size, children: Vec<Self>) -> Self {
6+
pub fn dir(name: Name, inode_size: Size, children: Vec<Self>, depth: u64) -> Self {
77
let size = inode_size + children.iter().map(DataTree::size).sum();
8+
let children = if depth > 0 { children } else { Vec::new() };
89
DataTree {
910
name,
1011
size,
@@ -26,6 +27,6 @@ impl<Name, Size: size::Size> DataTree<Name, Size> {
2627
where
2728
Size: Copy,
2829
{
29-
move |name, children| DataTree::dir(name, inode_size, children)
30+
move |name, children| DataTree::dir(name, inode_size, children, 1)
3031
}
3132
}

src/data_tree/retain/test.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ type SampleData = Bytes;
77
type SampleTree = DataTree<SampleName, SampleData>;
88

99
fn dir<const INODE_SIZE: u64>(name: &'static str, children: Vec<SampleTree>) -> SampleTree {
10-
SampleTree::dir(name.to_string(), INODE_SIZE.into(), children)
10+
SampleTree::dir(name.to_string(), INODE_SIZE.into(), children, 10)
1111
}
1212

1313
fn file(name: &'static str, size: u64) -> SampleTree {
@@ -23,6 +23,7 @@ fn culled_dir<const INODE_SIZE: u64>(
2323
name.to_string(),
2424
(INODE_SIZE + culled_size).into(),
2525
children,
26+
10,
2627
)
2728
}
2829

src/fs_tree_builder.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use std::{
2929
/// root: std::env::current_dir().unwrap(),
3030
/// size_getter: GetApparentSize,
3131
/// reporter: ErrorOnlyReporter::new(ErrorReport::SILENT),
32+
/// max_depth: 10,
3233
/// };
3334
/// let data_tree: DataTree<OsStringDisplay, Bytes> = builder.into();
3435
/// ```
@@ -45,6 +46,8 @@ where
4546
pub size_getter: SizeGetter,
4647
/// Reports progress to external system.
4748
pub reporter: Report,
49+
/// Deepest level of descendent display in the graph. The size still counts toward total beyond the depth.
50+
pub max_depth: u64,
4851
}
4952

5053
impl<Size, SizeGetter, Report> From<FsTreeBuilder<Size, SizeGetter, Report>>
@@ -60,6 +63,7 @@ where
6063
root,
6164
size_getter,
6265
reporter,
66+
max_depth,
6367
} = builder;
6468

6569
TreeBuilder::<PathBuf, OsStringDisplay, Size, _, _> {
@@ -118,6 +122,8 @@ where
118122
},
119123

120124
join_path: |prefix, name| prefix.join(&name.0),
125+
126+
max_depth,
121127
}
122128
.into()
123129
}

src/tree_builder.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ where
2323
pub get_info: GetInfo,
2424
/// Function to join parent's `path` with a child's name to make the child's `name`.
2525
pub join_path: JoinPath,
26+
/// Deepest level of descendent to store as arrays. The size still counts toward total beyond the depth.
27+
pub max_depth: u64,
2628
}
2729

2830
impl<Path, Name, Size, GetInfo, JoinPath> From<TreeBuilder<Path, Name, Size, GetInfo, JoinPath>>
@@ -41,9 +43,11 @@ where
4143
name,
4244
get_info,
4345
join_path,
46+
max_depth: current_depth,
4447
} = builder;
4548

4649
let Info { size, children } = get_info(&path);
50+
let next_depth = current_depth.saturating_sub(1);
4751

4852
let children: Vec<_> = children
4953
.into_par_iter()
@@ -52,10 +56,11 @@ where
5256
name,
5357
get_info,
5458
join_path,
59+
max_depth: next_depth,
5560
})
5661
.map(Self::from)
5762
.collect();
5863

59-
DataTree::dir(name, size, children)
64+
DataTree::dir(name, size, children, current_depth)
6065
}
6166
}

tests/_utils.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ where
153153
panic!("Unexpected call to report_error: {error:?}")
154154
}),
155155
root: root.join(suffix),
156+
max_depth: 10,
156157
}
157158
.pipe(DataTree::<OsStringDisplay, Size>::from)
158159
.into_par_sorted(|left, right| left.name().cmp(right.name()))

tests/cli_errors.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ fn fs_errors() {
133133
root: workspace.to_path_buf(),
134134
size_getter: GetApparentSize,
135135
reporter: ErrorOnlyReporter::new(ErrorReport::SILENT),
136+
max_depth: 10,
136137
};
137138
let mut data_tree: DataTree<OsStringDisplay, _> = builder.into();
138139
data_tree.par_sort_by(|left, right| left.size().cmp(&right.size()).reverse());

tests/json.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type SampleTree = DataTree<SampleName, SampleData>;
3030

3131
fn sample_tree() -> SampleTree {
3232
let dir = |name: &'static str, children: Vec<SampleTree>| {
33-
SampleTree::dir(name.to_string(), 1024.into(), children)
33+
SampleTree::dir(name.to_string(), 1024.into(), children, 10)
3434
};
3535
let file =
3636
|name: &'static str, size: u64| SampleTree::file(name.to_string(), Bytes::from(size));
@@ -82,6 +82,7 @@ fn json_output() {
8282
root: workspace.to_path_buf(),
8383
size_getter: GetApparentSize,
8484
reporter: ErrorOnlyReporter::new(ErrorReport::SILENT),
85+
max_depth: 10,
8586
};
8687
let expected = builder
8788
.pipe(DataTree::<_, Bytes>::from)

tests/tree_builder.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ impl SampleTree {
5858
}
5959
},
6060
join_path: |prefix, name| format!("{prefix}{SAMPLE_SEPARATOR}{name}"),
61+
max_depth: 10,
6162
}
6263
.pipe(DataTree::from)
6364
.into_par_sorted(|left, right| left.name().as_str().cmp(right.name().as_str()))

tests/usual_cli.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ fn total_width() {
4343
root: workspace.to_path_buf(),
4444
size_getter: DEFAULT_GET_SIZE,
4545
reporter: ErrorOnlyReporter::new(ErrorReport::SILENT),
46+
max_depth: 10,
4647
};
4748
let mut data_tree: DataTree<OsStringDisplay, _> = builder.into();
4849
data_tree.par_cull_insignificant_data(0.01);
@@ -81,6 +82,7 @@ fn column_width() {
8182
root: workspace.to_path_buf(),
8283
size_getter: DEFAULT_GET_SIZE,
8384
reporter: ErrorOnlyReporter::new(ErrorReport::SILENT),
85+
max_depth: 10,
8486
};
8587
let mut data_tree: DataTree<OsStringDisplay, _> = builder.into();
8688
data_tree.par_cull_insignificant_data(0.01);
@@ -119,6 +121,7 @@ fn min_ratio_0() {
119121
root: workspace.to_path_buf(),
120122
size_getter: GetApparentSize,
121123
reporter: ErrorOnlyReporter::new(ErrorReport::SILENT),
124+
max_depth: 10,
122125
};
123126
let mut data_tree: DataTree<OsStringDisplay, _> = builder.into();
124127
data_tree.par_sort_by(|left, right| left.size().cmp(&right.size()).reverse());
@@ -156,6 +159,7 @@ fn min_ratio() {
156159
root: workspace.to_path_buf(),
157160
size_getter: GetApparentSize,
158161
reporter: ErrorOnlyReporter::new(ErrorReport::SILENT),
162+
max_depth: 10,
159163
};
160164
let mut data_tree: DataTree<OsStringDisplay, _> = builder.into();
161165
data_tree.par_cull_insignificant_data(0.1);
@@ -194,6 +198,7 @@ fn max_depth_2() {
194198
root: workspace.to_path_buf(),
195199
size_getter: GetApparentSize,
196200
reporter: ErrorOnlyReporter::new(ErrorReport::SILENT),
201+
max_depth: 10,
197202
};
198203
let mut data_tree: DataTree<OsStringDisplay, _> = builder.into();
199204
data_tree.par_cull_insignificant_data(0.01);
@@ -232,6 +237,7 @@ fn max_depth_1() {
232237
root: workspace.to_path_buf(),
233238
size_getter: GetApparentSize,
234239
reporter: ErrorOnlyReporter::new(ErrorReport::SILENT),
240+
max_depth: 10,
235241
};
236242
let mut data_tree: DataTree<OsStringDisplay, _> = builder.into();
237243
data_tree.par_cull_insignificant_data(0.01);
@@ -269,6 +275,7 @@ fn top_down() {
269275
root: workspace.to_path_buf(),
270276
size_getter: DEFAULT_GET_SIZE,
271277
reporter: ErrorOnlyReporter::new(ErrorReport::SILENT),
278+
max_depth: 10,
272279
};
273280
let mut data_tree: DataTree<OsStringDisplay, _> = builder.into();
274281
data_tree.par_cull_insignificant_data(0.01);
@@ -306,6 +313,7 @@ fn align_right() {
306313
root: workspace.to_path_buf(),
307314
size_getter: DEFAULT_GET_SIZE,
308315
reporter: ErrorOnlyReporter::new(ErrorReport::SILENT),
316+
max_depth: 10,
309317
};
310318
let mut data_tree: DataTree<OsStringDisplay, _> = builder.into();
311319
data_tree.par_cull_insignificant_data(0.01);
@@ -343,6 +351,7 @@ fn quantity_apparent_size() {
343351
root: workspace.to_path_buf(),
344352
size_getter: GetApparentSize,
345353
reporter: ErrorOnlyReporter::new(ErrorReport::SILENT),
354+
max_depth: 10,
346355
};
347356
let mut data_tree: DataTree<OsStringDisplay, _> = builder.into();
348357
data_tree.par_cull_insignificant_data(0.01);
@@ -381,6 +390,7 @@ fn quantity_block_size() {
381390
root: workspace.to_path_buf(),
382391
size_getter: GetBlockSize,
383392
reporter: ErrorOnlyReporter::new(ErrorReport::SILENT),
393+
max_depth: 10,
384394
};
385395
let mut data_tree: DataTree<OsStringDisplay, _> = builder.into();
386396
data_tree.par_cull_insignificant_data(0.01);
@@ -419,6 +429,7 @@ fn quantity_block_count() {
419429
root: workspace.to_path_buf(),
420430
size_getter: GetBlockCount,
421431
reporter: ErrorOnlyReporter::new(ErrorReport::SILENT),
432+
max_depth: 10,
422433
};
423434
let mut data_tree: DataTree<OsStringDisplay, _> = builder.into();
424435
data_tree.par_cull_insignificant_data(0.01);
@@ -458,6 +469,7 @@ fn bytes_format_plain() {
458469
root: workspace.to_path_buf(),
459470
size_getter: GetBlockSize,
460471
reporter: ErrorOnlyReporter::new(ErrorReport::SILENT),
472+
max_depth: 10,
461473
};
462474
let mut data_tree: DataTree<OsStringDisplay, _> = builder.into();
463475
data_tree.par_cull_insignificant_data(0.01);
@@ -497,6 +509,7 @@ fn bytes_format_metric() {
497509
root: workspace.to_path_buf(),
498510
size_getter: GetBlockSize,
499511
reporter: ErrorOnlyReporter::new(ErrorReport::SILENT),
512+
max_depth: 10,
500513
};
501514
let mut data_tree: DataTree<OsStringDisplay, _> = builder.into();
502515
data_tree.par_cull_insignificant_data(0.01);
@@ -536,6 +549,7 @@ fn bytes_format_binary() {
536549
root: workspace.to_path_buf(),
537550
size_getter: GetBlockSize,
538551
reporter: ErrorOnlyReporter::new(ErrorReport::SILENT),
552+
max_depth: 10,
539553
};
540554
let mut data_tree: DataTree<OsStringDisplay, _> = builder.into();
541555
data_tree.par_cull_insignificant_data(0.01);
@@ -573,6 +587,7 @@ fn path_to_workspace() {
573587
root: workspace.to_path_buf(),
574588
size_getter: DEFAULT_GET_SIZE,
575589
reporter: ErrorOnlyReporter::new(ErrorReport::SILENT),
590+
max_depth: 10,
576591
};
577592
let mut data_tree: DataTree<OsStringDisplay, _> = builder.into();
578593
data_tree.par_cull_insignificant_data(0.01);
@@ -615,6 +630,7 @@ fn multiple_names() {
615630
root: workspace.to_path_buf().join(name),
616631
size_getter: GetApparentSize,
617632
reporter: ErrorOnlyReporter::new(ErrorReport::SILENT),
633+
max_depth: 10,
618634
};
619635
let mut data_tree: DataTree<OsStringDisplay, _> = builder.into();
620636
*data_tree.name_mut() = OsStringDisplay::os_string_from(name);
@@ -625,6 +641,7 @@ fn multiple_names() {
625641
OsStringDisplay::os_string_from("(total)"),
626642
0.into(),
627643
children.collect(),
644+
10,
628645
)
629646
})
630647
.into_par_sorted(|left, right| left.size().cmp(&right.size()).reverse());

0 commit comments

Comments
 (0)