diff --git a/Cargo.lock b/Cargo.lock index 33c9c1e8..64adbbb4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -70,6 +70,31 @@ version = "2.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "843867be96c8daad0d758b57df9392b6d8d271134fce549de6ce169ff98a92af" +[[package]] +name = "bon" +version = "3.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d13a61f2963b88eef9c1be03df65d42f6996dfeac1054870d950fcf66686f83" +dependencies = [ + "bon-macros", + "rustversion", +] + +[[package]] +name = "bon-macros" +version = "3.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d314cc62af2b6b0c65780555abb4d02a03dd3b799cd42419044f0c38d99738c0" +dependencies = [ + "darling 0.23.0", + "ident_case", + "prettyplease", + "proc-macro2", + "quote", + "rustversion", + "syn 2.0.117", +] + [[package]] name = "build-fs-tree" version = "0.8.1" @@ -242,6 +267,16 @@ dependencies = [ "darling_macro 0.21.3", ] +[[package]] +name = "darling" +version = "0.23.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25ae13da2f202d56bd7f91c25fba009e7717a1e4a1cc98a76d844b65ae912e9d" +dependencies = [ + "darling_core 0.23.0", + "darling_macro 0.23.0", +] + [[package]] name = "darling_core" version = "0.12.4" @@ -270,6 +305,19 @@ dependencies = [ "syn 2.0.117", ] +[[package]] +name = "darling_core" +version = "0.23.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9865a50f7c335f53564bb694ef660825eb8610e0a53d3e11bf1b0d3df31e03b0" +dependencies = [ + "ident_case", + "proc-macro2", + "quote", + "strsim 0.11.1", + "syn 2.0.117", +] + [[package]] name = "darling_macro" version = "0.12.4" @@ -292,6 +340,17 @@ dependencies = [ "syn 2.0.117", ] +[[package]] +name = "darling_macro" +version = "0.23.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3984ec7bd6cfa798e62b4a642426a5be0e68f9401cfc2a01e3fa9ea2fcdb8d" +dependencies = [ + "darling_core 0.23.0", + "quote", + "syn 2.0.117", +] + [[package]] name = "dashmap" version = "6.1.0" @@ -632,6 +691,7 @@ name = "parallel-disk-usage" version = "0.21.1" dependencies = [ "assert-cmp", + "bon", "build-fs-tree", "clap", "clap-utilities", @@ -796,6 +856,12 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "rustversion" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" + [[package]] name = "ryu" version = "1.0.23" diff --git a/Cargo.toml b/Cargo.toml index 92b0ed2b..0b437d1f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -56,6 +56,7 @@ cli-completions = ["cli"] [dependencies] assert-cmp = "0.3.0" +bon = "3.9.0" clap = { version = "4.5.60", optional = true } clap_complete = { version = "4.5.66", optional = true } clap-utilities = { version = "0.3.0", optional = true } diff --git a/src/app/sub.rs b/src/app/sub.rs index 3500a5f3..f6fb2c88 100644 --- a/src/app/sub.rs +++ b/src/app/sub.rs @@ -12,11 +12,13 @@ use crate::{ status_board::GLOBAL_STATUS_BOARD, visualizer::{BarAlignment, ColumnWidthDistribution, Direction, Visualizer}, }; +use bon::Builder; use pipe_trait::Pipe; use serde::Serialize; use std::{io::stdout, iter::once, path::PathBuf}; /// The sub program of the main application. +#[derive(Builder)] pub struct Sub where Report: ParallelReporter + Sync, diff --git a/src/fs_tree_builder.rs b/src/fs_tree_builder.rs index 37167b2c..f31d769d 100644 --- a/src/fs_tree_builder.rs +++ b/src/fs_tree_builder.rs @@ -7,6 +7,7 @@ use super::{ size, tree_builder::{Info, TreeBuilder}, }; +use bon::Builder; use pipe_trait::Pipe; use std::{ fs::{read_dir, symlink_metadata}, @@ -27,16 +28,17 @@ use std::{ /// size::Bytes, /// hardlink::HardlinkIgnorant, /// }; -/// let builder = FsTreeBuilder { -/// root: std::env::current_dir().unwrap(), -/// hardlinks_recorder: &HardlinkIgnorant, -/// size_getter: GetApparentSize, -/// reporter: &ErrorOnlyReporter::new(ErrorReport::SILENT), -/// max_depth: 10, -/// }; +/// let reporter = ErrorOnlyReporter::new(ErrorReport::SILENT); +/// let builder = FsTreeBuilder::builder() +/// .root(std::env::current_dir().unwrap()) +/// .hardlinks_recorder(&HardlinkIgnorant) +/// .size_getter(GetApparentSize) +/// .reporter(&reporter) +/// .max_depth(10) +/// .build(); /// let data_tree: DataTree = builder.into(); /// ``` -#[derive(Debug)] +#[derive(Debug, Builder)] pub struct FsTreeBuilder<'a, Size, SizeGetter, HardlinksRecorder, Report> where Report: Reporter + Sync + ?Sized, diff --git a/src/tree_builder.rs b/src/tree_builder.rs index 5f82572b..46240f32 100644 --- a/src/tree_builder.rs +++ b/src/tree_builder.rs @@ -3,10 +3,11 @@ pub mod info; pub use info::Info; use super::{data_tree::DataTree, size}; +use bon::Builder; use rayon::prelude::*; /// Collection of functions and starting points in order to build a [`DataTree`] with [`From`] or [`Into`]. -#[derive(Debug)] +#[derive(Debug, Builder)] pub struct TreeBuilder where Path: Send + Sync, diff --git a/src/visualizer.rs b/src/visualizer.rs index 71effaaf..9f3a172c 100644 --- a/src/visualizer.rs +++ b/src/visualizer.rs @@ -15,6 +15,7 @@ pub use proportion_bar::{ProportionBar, ProportionBarBlock}; pub use tree::{TreeHorizontalSlice, TreeSkeletalComponent}; use super::{data_tree::DataTree, size}; +use bon::Builder; use std::fmt::Display; /// Visualize a [`DataTree`]. @@ -32,17 +33,17 @@ use std::fmt::Display; /// # use parallel_disk_usage::visualizer::{Visualizer, Direction, BarAlignment, ColumnWidthDistribution}; /// # fn _wrapper(create_data_tree: fn() -> DataTree) { /// let data_tree: DataTree = create_data_tree(); -/// let visualizer = Visualizer { -/// data_tree: &data_tree, -/// bytes_format: BytesFormat::MetricUnits, -/// direction: Direction::BottomUp, -/// bar_alignment: BarAlignment::Right, -/// column_width_distribution: ColumnWidthDistribution::total(100), -/// }; +/// let visualizer = Visualizer::builder() +/// .data_tree(&data_tree) +/// .bytes_format(BytesFormat::MetricUnits) +/// .direction(Direction::BottomUp) +/// .bar_alignment(BarAlignment::Right) +/// .column_width_distribution(ColumnWidthDistribution::total(100)) +/// .build(); /// println!("{visualizer}"); /// # } /// ``` -#[derive(Debug)] +#[derive(Debug, Builder)] pub struct Visualizer<'a, Name, Size> where Name: Display, diff --git a/tests/_utils.rs b/tests/_utils.rs index 75b4a326..77887a66 100644 --- a/tests/_utils.rs +++ b/tests/_utils.rs @@ -366,18 +366,18 @@ where } let measure = |suffix: &str| { - FsTreeBuilder { - size_getter, - hardlinks_recorder: &HardlinkIgnorant, - reporter: &ErrorOnlyReporter::new(|error| { + FsTreeBuilder::builder() + .size_getter(size_getter) + .hardlinks_recorder(&HardlinkIgnorant) + .reporter(&ErrorOnlyReporter::new(|error| { panic!("Unexpected call to report_error: {error:?}") - }), - root: root.join(suffix), - max_depth: 10, - } - .pipe(DataTree::::from) - .into_par_sorted(|left, right| left.name().cmp(right.name())) - .into_reflection() + })) + .root(root.join(suffix)) + .max_depth(10) + .build() + .pipe(DataTree::::from) + .into_par_sorted(|left, right| left.name().cmp(right.name())) + .into_reflection() }; let sub = |suffix: &str| root.join(suffix).pipe(OsStringDisplay::os_string_from); diff --git a/tests/cli_errors.rs b/tests/cli_errors.rs index c33f0d9a..24893222 100644 --- a/tests/cli_errors.rs +++ b/tests/cli_errors.rs @@ -130,23 +130,24 @@ fn fs_errors() { dbg!(&status); eprintln!("STDERR+STDOUT:\n{stderr}{stdout}\n"); - let builder = FsTreeBuilder { - root: workspace.to_path_buf(), - size_getter: GetApparentSize, - hardlinks_recorder: &HardlinkIgnorant, - reporter: &ErrorOnlyReporter::new(ErrorReport::SILENT), - max_depth: 10, - }; + let reporter = ErrorOnlyReporter::new(ErrorReport::SILENT); + let builder = FsTreeBuilder::builder() + .root(workspace.to_path_buf()) + .size_getter(GetApparentSize) + .hardlinks_recorder(&HardlinkIgnorant) + .reporter(&reporter) + .max_depth(10) + .build(); let mut data_tree: DataTree = builder.into(); data_tree.par_sort_by(|left, right| left.size().cmp(&right.size()).reverse()); *data_tree.name_mut() = OsStringDisplay::os_string_from("."); - let visualizer = Visualizer { - data_tree: &data_tree, - bytes_format: BytesFormat::MetricUnits, - direction: Direction::BottomUp, - bar_alignment: BarAlignment::Left, - column_width_distribution: ColumnWidthDistribution::total(100), - }; + let visualizer = Visualizer::builder() + .data_tree(&data_tree) + .bytes_format(BytesFormat::MetricUnits) + .direction(Direction::BottomUp) + .bar_alignment(BarAlignment::Left) + .column_width_distribution(ColumnWidthDistribution::total(100)) + .build(); let expected_stdout = format!("{visualizer}"); eprintln!("EXPECTED STDOUT:\n{}\n", &expected_stdout); diff --git a/tests/json.rs b/tests/json.rs index 95aee0b9..61520cd8 100644 --- a/tests/json.rs +++ b/tests/json.rs @@ -80,13 +80,14 @@ fn json_output() { .tree .pipe(sanitize_tree_reflection); dbg!(&actual); - let builder = FsTreeBuilder { - root: workspace.to_path_buf(), - size_getter: GetApparentSize, - hardlinks_recorder: &HardlinkIgnorant, - reporter: &ErrorOnlyReporter::new(ErrorReport::SILENT), - max_depth: 10, - }; + let reporter = ErrorOnlyReporter::new(ErrorReport::SILENT); + let builder = FsTreeBuilder::builder() + .root(workspace.to_path_buf()) + .size_getter(GetApparentSize) + .hardlinks_recorder(&HardlinkIgnorant) + .reporter(&reporter) + .max_depth(10) + .build(); let expected = builder .pipe(DataTree::<_, Bytes>::from) .into_reflection() @@ -135,13 +136,14 @@ fn json_input() { let actual = actual.trim_end(); eprintln!("ACTUAL:\n{actual}\n"); - let visualizer = Visualizer { - data_tree: &sample_tree(), - bytes_format: BytesFormat::MetricUnits, - direction: Direction::BottomUp, - bar_alignment: BarAlignment::Left, - column_width_distribution: ColumnWidthDistribution::total(100), - }; + let tree = sample_tree(); + let visualizer = Visualizer::builder() + .data_tree(&tree) + .bytes_format(BytesFormat::MetricUnits) + .direction(Direction::BottomUp) + .bar_alignment(BarAlignment::Left) + .column_width_distribution(ColumnWidthDistribution::total(100)) + .build(); let expected = format!("{visualizer}"); let expected = expected.trim_end(); eprintln!("EXPECTED:\n{expected}\n"); diff --git a/tests/tree_builder.rs b/tests/tree_builder.rs index cd1951c2..d3023dc5 100644 --- a/tests/tree_builder.rs +++ b/tests/tree_builder.rs @@ -39,10 +39,10 @@ impl SampleTree { } fn tree(&self, root: &'static str) -> DataTree { - TreeBuilder { - path: root.to_string(), - name: root.to_string(), - get_info: |path| { + TreeBuilder::builder() + .path(root.to_string()) + .name(root.to_string()) + .get_info(|path| { let path: Vec<_> = path .split(SAMPLE_SEPARATOR) .map(ToString::to_string) @@ -56,12 +56,12 @@ impl SampleTree { )), None => panic!("Path does not exist"), } - }, - join_path: |prefix, name| format!("{prefix}{SAMPLE_SEPARATOR}{name}"), - max_depth: 10, - } - .pipe(DataTree::from) - .into_par_sorted(|left, right| left.name().as_str().cmp(right.name().as_str())) + }) + .join_path(|prefix, name| format!("{prefix}{SAMPLE_SEPARATOR}{name}")) + .max_depth(10) + .build() + .pipe(DataTree::from) + .into_par_sorted(|left, right| left.name().as_str().cmp(right.name().as_str())) } } diff --git a/tests/usual_cli.rs b/tests/usual_cli.rs index cbacb9f2..9789666d 100644 --- a/tests/usual_cli.rs +++ b/tests/usual_cli.rs @@ -40,24 +40,25 @@ fn total_width() { .pipe(stdout_text); eprintln!("ACTUAL:\n{actual}\n"); - let builder = FsTreeBuilder { - root: workspace.to_path_buf(), - size_getter: DEFAULT_GET_SIZE, - hardlinks_recorder: &HardlinkIgnorant, - reporter: &ErrorOnlyReporter::new(ErrorReport::SILENT), - max_depth: 10, - }; + let reporter = ErrorOnlyReporter::new(ErrorReport::SILENT); + let builder = FsTreeBuilder::builder() + .root(workspace.to_path_buf()) + .size_getter(DEFAULT_GET_SIZE) + .hardlinks_recorder(&HardlinkIgnorant) + .reporter(&reporter) + .max_depth(10) + .build(); let mut data_tree: DataTree = builder.into(); data_tree.par_cull_insignificant_data(0.01); data_tree.par_sort_by(|left, right| left.size().cmp(&right.size()).reverse()); *data_tree.name_mut() = OsStringDisplay::os_string_from("."); - let visualizer = Visualizer:: { - data_tree: &data_tree, - bytes_format: BytesFormat::MetricUnits, - direction: Direction::BottomUp, - bar_alignment: BarAlignment::Left, - column_width_distribution: ColumnWidthDistribution::total(100), - }; + let visualizer = Visualizer::::builder() + .data_tree(&data_tree) + .bytes_format(BytesFormat::MetricUnits) + .direction(Direction::BottomUp) + .bar_alignment(BarAlignment::Left) + .column_width_distribution(ColumnWidthDistribution::total(100)) + .build(); let expected = format!("{visualizer}"); let expected = expected.trim_end(); eprintln!("EXPECTED:\n{expected}\n"); @@ -79,24 +80,25 @@ fn column_width() { .pipe(stdout_text); eprintln!("ACTUAL:\n{actual}\n"); - let builder = FsTreeBuilder { - root: workspace.to_path_buf(), - size_getter: DEFAULT_GET_SIZE, - hardlinks_recorder: &HardlinkIgnorant, - reporter: &ErrorOnlyReporter::new(ErrorReport::SILENT), - max_depth: 10, - }; + let reporter = ErrorOnlyReporter::new(ErrorReport::SILENT); + let builder = FsTreeBuilder::builder() + .root(workspace.to_path_buf()) + .size_getter(DEFAULT_GET_SIZE) + .hardlinks_recorder(&HardlinkIgnorant) + .reporter(&reporter) + .max_depth(10) + .build(); let mut data_tree: DataTree = builder.into(); data_tree.par_cull_insignificant_data(0.01); data_tree.par_sort_by(|left, right| left.size().cmp(&right.size()).reverse()); *data_tree.name_mut() = OsStringDisplay::os_string_from("."); - let visualizer = Visualizer:: { - data_tree: &data_tree, - bytes_format: BytesFormat::MetricUnits, - direction: Direction::BottomUp, - bar_alignment: BarAlignment::Left, - column_width_distribution: ColumnWidthDistribution::components(10, 90), - }; + let visualizer = Visualizer::::builder() + .data_tree(&data_tree) + .bytes_format(BytesFormat::MetricUnits) + .direction(Direction::BottomUp) + .bar_alignment(BarAlignment::Left) + .column_width_distribution(ColumnWidthDistribution::components(10, 90)) + .build(); let expected = format!("{visualizer}"); let expected = expected.trim_end(); eprintln!("EXPECTED:\n{expected}\n"); @@ -118,23 +120,24 @@ fn min_ratio_0() { .pipe(stdout_text); eprintln!("ACTUAL:\n{actual}\n"); - let builder = FsTreeBuilder { - root: workspace.to_path_buf(), - size_getter: GetApparentSize, - hardlinks_recorder: &HardlinkIgnorant, - reporter: &ErrorOnlyReporter::new(ErrorReport::SILENT), - max_depth: 10, - }; + let reporter = ErrorOnlyReporter::new(ErrorReport::SILENT); + let builder = FsTreeBuilder::builder() + .root(workspace.to_path_buf()) + .size_getter(GetApparentSize) + .hardlinks_recorder(&HardlinkIgnorant) + .reporter(&reporter) + .max_depth(10) + .build(); let mut data_tree: DataTree = builder.into(); data_tree.par_sort_by(|left, right| left.size().cmp(&right.size()).reverse()); *data_tree.name_mut() = OsStringDisplay::os_string_from("."); - let visualizer = Visualizer:: { - data_tree: &data_tree, - bytes_format: BytesFormat::MetricUnits, - direction: Direction::BottomUp, - bar_alignment: BarAlignment::Left, - column_width_distribution: ColumnWidthDistribution::total(100), - }; + let visualizer = Visualizer::::builder() + .data_tree(&data_tree) + .bytes_format(BytesFormat::MetricUnits) + .direction(Direction::BottomUp) + .bar_alignment(BarAlignment::Left) + .column_width_distribution(ColumnWidthDistribution::total(100)) + .build(); let expected = format!("{visualizer}"); let expected = expected.trim_end(); eprintln!("EXPECTED:\n{expected}\n"); @@ -156,24 +159,25 @@ fn min_ratio() { .pipe(stdout_text); eprintln!("ACTUAL:\n{actual}\n"); - let builder = FsTreeBuilder { - root: workspace.to_path_buf(), - size_getter: GetApparentSize, - hardlinks_recorder: &HardlinkIgnorant, - reporter: &ErrorOnlyReporter::new(ErrorReport::SILENT), - max_depth: 10, - }; + let reporter = ErrorOnlyReporter::new(ErrorReport::SILENT); + let builder = FsTreeBuilder::builder() + .root(workspace.to_path_buf()) + .size_getter(GetApparentSize) + .hardlinks_recorder(&HardlinkIgnorant) + .reporter(&reporter) + .max_depth(10) + .build(); let mut data_tree: DataTree = builder.into(); data_tree.par_cull_insignificant_data(0.1); data_tree.par_sort_by(|left, right| left.size().cmp(&right.size()).reverse()); *data_tree.name_mut() = OsStringDisplay::os_string_from("."); - let visualizer = Visualizer:: { - data_tree: &data_tree, - bytes_format: BytesFormat::MetricUnits, - direction: Direction::BottomUp, - bar_alignment: BarAlignment::Left, - column_width_distribution: ColumnWidthDistribution::total(100), - }; + let visualizer = Visualizer::::builder() + .data_tree(&data_tree) + .bytes_format(BytesFormat::MetricUnits) + .direction(Direction::BottomUp) + .bar_alignment(BarAlignment::Left) + .column_width_distribution(ColumnWidthDistribution::total(100)) + .build(); let expected = format!("{visualizer}"); let expected = expected.trim_end(); eprintln!("EXPECTED:\n{expected}\n"); @@ -195,24 +199,25 @@ fn max_depth_2() { .pipe(stdout_text); eprintln!("ACTUAL:\n{actual}\n"); - let builder = FsTreeBuilder { - root: workspace.to_path_buf(), - size_getter: GetApparentSize, - hardlinks_recorder: &HardlinkIgnorant, - reporter: &ErrorOnlyReporter::new(ErrorReport::SILENT), - max_depth: 2, - }; + let reporter = ErrorOnlyReporter::new(ErrorReport::SILENT); + let builder = FsTreeBuilder::builder() + .root(workspace.to_path_buf()) + .size_getter(GetApparentSize) + .hardlinks_recorder(&HardlinkIgnorant) + .reporter(&reporter) + .max_depth(2) + .build(); let mut data_tree: DataTree = builder.into(); data_tree.par_cull_insignificant_data(0.01); data_tree.par_sort_by(|left, right| left.size().cmp(&right.size()).reverse()); *data_tree.name_mut() = OsStringDisplay::os_string_from("."); - let visualizer = Visualizer:: { - data_tree: &data_tree, - bytes_format: BytesFormat::MetricUnits, - direction: Direction::BottomUp, - bar_alignment: BarAlignment::Left, - column_width_distribution: ColumnWidthDistribution::total(100), - }; + let visualizer = Visualizer::::builder() + .data_tree(&data_tree) + .bytes_format(BytesFormat::MetricUnits) + .direction(Direction::BottomUp) + .bar_alignment(BarAlignment::Left) + .column_width_distribution(ColumnWidthDistribution::total(100)) + .build(); let expected = format!("{visualizer}"); let expected = expected.trim_end(); eprintln!("EXPECTED:\n{expected}\n"); @@ -234,24 +239,25 @@ fn max_depth_1() { .pipe(stdout_text); eprintln!("ACTUAL:\n{actual}\n"); - let builder = FsTreeBuilder { - root: workspace.to_path_buf(), - size_getter: GetApparentSize, - hardlinks_recorder: &HardlinkIgnorant, - reporter: &ErrorOnlyReporter::new(ErrorReport::SILENT), - max_depth: 1, - }; + let reporter = ErrorOnlyReporter::new(ErrorReport::SILENT); + let builder = FsTreeBuilder::builder() + .root(workspace.to_path_buf()) + .size_getter(GetApparentSize) + .hardlinks_recorder(&HardlinkIgnorant) + .reporter(&reporter) + .max_depth(1) + .build(); let mut data_tree: DataTree = builder.into(); data_tree.par_cull_insignificant_data(0.01); data_tree.par_sort_by(|left, right| left.size().cmp(&right.size()).reverse()); *data_tree.name_mut() = OsStringDisplay::os_string_from("."); - let visualizer = Visualizer:: { - data_tree: &data_tree, - bytes_format: BytesFormat::MetricUnits, - direction: Direction::BottomUp, - bar_alignment: BarAlignment::Left, - column_width_distribution: ColumnWidthDistribution::total(100), - }; + let visualizer = Visualizer::::builder() + .data_tree(&data_tree) + .bytes_format(BytesFormat::MetricUnits) + .direction(Direction::BottomUp) + .bar_alignment(BarAlignment::Left) + .column_width_distribution(ColumnWidthDistribution::total(100)) + .build(); let expected = format!("{visualizer}"); let expected = expected.trim_end(); eprintln!("EXPECTED:\n{expected}\n"); @@ -272,24 +278,25 @@ fn top_down() { .pipe(stdout_text); eprintln!("ACTUAL:\n{actual}\n"); - let builder = FsTreeBuilder { - root: workspace.to_path_buf(), - size_getter: DEFAULT_GET_SIZE, - hardlinks_recorder: &HardlinkIgnorant, - reporter: &ErrorOnlyReporter::new(ErrorReport::SILENT), - max_depth: 10, - }; + let reporter = ErrorOnlyReporter::new(ErrorReport::SILENT); + let builder = FsTreeBuilder::builder() + .root(workspace.to_path_buf()) + .size_getter(DEFAULT_GET_SIZE) + .hardlinks_recorder(&HardlinkIgnorant) + .reporter(&reporter) + .max_depth(10) + .build(); let mut data_tree: DataTree = builder.into(); data_tree.par_cull_insignificant_data(0.01); data_tree.par_sort_by(|left, right| left.size().cmp(&right.size()).reverse()); *data_tree.name_mut() = OsStringDisplay::os_string_from("."); - let visualizer = Visualizer:: { - data_tree: &data_tree, - bytes_format: BytesFormat::MetricUnits, - direction: Direction::TopDown, - bar_alignment: BarAlignment::Left, - column_width_distribution: ColumnWidthDistribution::total(100), - }; + let visualizer = Visualizer::::builder() + .data_tree(&data_tree) + .bytes_format(BytesFormat::MetricUnits) + .direction(Direction::TopDown) + .bar_alignment(BarAlignment::Left) + .column_width_distribution(ColumnWidthDistribution::total(100)) + .build(); let expected = format!("{visualizer}"); let expected = expected.trim_end(); eprintln!("EXPECTED:\n{expected}\n"); @@ -310,24 +317,25 @@ fn align_right() { .pipe(stdout_text); eprintln!("ACTUAL:\n{actual}\n"); - let builder = FsTreeBuilder { - root: workspace.to_path_buf(), - size_getter: DEFAULT_GET_SIZE, - hardlinks_recorder: &HardlinkIgnorant, - reporter: &ErrorOnlyReporter::new(ErrorReport::SILENT), - max_depth: 10, - }; + let reporter = ErrorOnlyReporter::new(ErrorReport::SILENT); + let builder = FsTreeBuilder::builder() + .root(workspace.to_path_buf()) + .size_getter(DEFAULT_GET_SIZE) + .hardlinks_recorder(&HardlinkIgnorant) + .reporter(&reporter) + .max_depth(10) + .build(); let mut data_tree: DataTree = builder.into(); data_tree.par_cull_insignificant_data(0.01); data_tree.par_sort_by(|left, right| left.size().cmp(&right.size()).reverse()); *data_tree.name_mut() = OsStringDisplay::os_string_from("."); - let visualizer = Visualizer:: { - data_tree: &data_tree, - bytes_format: BytesFormat::MetricUnits, - direction: Direction::BottomUp, - bar_alignment: BarAlignment::Right, - column_width_distribution: ColumnWidthDistribution::total(100), - }; + let visualizer = Visualizer::::builder() + .data_tree(&data_tree) + .bytes_format(BytesFormat::MetricUnits) + .direction(Direction::BottomUp) + .bar_alignment(BarAlignment::Right) + .column_width_distribution(ColumnWidthDistribution::total(100)) + .build(); let expected = format!("{visualizer}"); let expected = expected.trim_end(); eprintln!("EXPECTED:\n{expected}\n"); @@ -348,24 +356,25 @@ fn quantity_apparent_size() { .pipe(stdout_text); eprintln!("ACTUAL:\n{actual}\n"); - let builder = FsTreeBuilder { - root: workspace.to_path_buf(), - size_getter: GetApparentSize, - hardlinks_recorder: &HardlinkIgnorant, - reporter: &ErrorOnlyReporter::new(ErrorReport::SILENT), - max_depth: 10, - }; + let reporter = ErrorOnlyReporter::new(ErrorReport::SILENT); + let builder = FsTreeBuilder::builder() + .root(workspace.to_path_buf()) + .size_getter(GetApparentSize) + .hardlinks_recorder(&HardlinkIgnorant) + .reporter(&reporter) + .max_depth(10) + .build(); let mut data_tree: DataTree = builder.into(); data_tree.par_cull_insignificant_data(0.01); data_tree.par_sort_by(|left, right| left.size().cmp(&right.size()).reverse()); *data_tree.name_mut() = OsStringDisplay::os_string_from("."); - let visualizer = Visualizer:: { - data_tree: &data_tree, - bytes_format: BytesFormat::MetricUnits, - direction: Direction::BottomUp, - bar_alignment: BarAlignment::Left, - column_width_distribution: ColumnWidthDistribution::total(100), - }; + let visualizer = Visualizer::::builder() + .data_tree(&data_tree) + .bytes_format(BytesFormat::MetricUnits) + .direction(Direction::BottomUp) + .bar_alignment(BarAlignment::Left) + .column_width_distribution(ColumnWidthDistribution::total(100)) + .build(); let expected = format!("{visualizer}"); let expected = expected.trim_end(); eprintln!("EXPECTED:\n{expected}\n"); @@ -387,24 +396,25 @@ fn quantity_block_size() { .pipe(stdout_text); eprintln!("ACTUAL:\n{actual}\n"); - let builder = FsTreeBuilder { - root: workspace.to_path_buf(), - size_getter: GetBlockSize, - hardlinks_recorder: &HardlinkIgnorant, - reporter: &ErrorOnlyReporter::new(ErrorReport::SILENT), - max_depth: 10, - }; + let reporter = ErrorOnlyReporter::new(ErrorReport::SILENT); + let builder = FsTreeBuilder::builder() + .root(workspace.to_path_buf()) + .size_getter(GetBlockSize) + .hardlinks_recorder(&HardlinkIgnorant) + .reporter(&reporter) + .max_depth(10) + .build(); let mut data_tree: DataTree = builder.into(); data_tree.par_cull_insignificant_data(0.01); data_tree.par_sort_by(|left, right| left.size().cmp(&right.size()).reverse()); *data_tree.name_mut() = OsStringDisplay::os_string_from("."); - let visualizer = Visualizer:: { - data_tree: &data_tree, - bytes_format: BytesFormat::MetricUnits, - direction: Direction::BottomUp, - bar_alignment: BarAlignment::Left, - column_width_distribution: ColumnWidthDistribution::total(100), - }; + let visualizer = Visualizer::::builder() + .data_tree(&data_tree) + .bytes_format(BytesFormat::MetricUnits) + .direction(Direction::BottomUp) + .bar_alignment(BarAlignment::Left) + .column_width_distribution(ColumnWidthDistribution::total(100)) + .build(); let expected = format!("{visualizer}"); let expected = expected.trim_end(); eprintln!("EXPECTED:\n{expected}\n"); @@ -426,24 +436,25 @@ fn quantity_block_count() { .pipe(stdout_text); eprintln!("ACTUAL:\n{actual}\n"); - let builder = FsTreeBuilder { - root: workspace.to_path_buf(), - size_getter: GetBlockCount, - hardlinks_recorder: &HardlinkIgnorant, - reporter: &ErrorOnlyReporter::new(ErrorReport::SILENT), - max_depth: 10, - }; + let reporter = ErrorOnlyReporter::new(ErrorReport::SILENT); + let builder = FsTreeBuilder::builder() + .root(workspace.to_path_buf()) + .size_getter(GetBlockCount) + .hardlinks_recorder(&HardlinkIgnorant) + .reporter(&reporter) + .max_depth(10) + .build(); let mut data_tree: DataTree = builder.into(); data_tree.par_cull_insignificant_data(0.01); data_tree.par_sort_by(|left, right| left.size().cmp(&right.size()).reverse()); *data_tree.name_mut() = OsStringDisplay::os_string_from("."); - let visualizer = Visualizer:: { - data_tree: &data_tree, - bytes_format: (), - direction: Direction::BottomUp, - bar_alignment: BarAlignment::Left, - column_width_distribution: ColumnWidthDistribution::total(100), - }; + let visualizer = Visualizer::::builder() + .data_tree(&data_tree) + .bytes_format(()) + .direction(Direction::BottomUp) + .bar_alignment(BarAlignment::Left) + .column_width_distribution(ColumnWidthDistribution::total(100)) + .build(); let expected = format!("{visualizer}"); let expected = expected.trim_end(); eprintln!("EXPECTED:\n{expected}\n"); @@ -465,24 +476,25 @@ fn bytes_format_plain() { .pipe(stdout_text); eprintln!("ACTUAL:\n{actual}\n"); - let builder = FsTreeBuilder { - root: workspace.to_path_buf(), - size_getter: GetApparentSize, - hardlinks_recorder: &HardlinkIgnorant, - reporter: &ErrorOnlyReporter::new(ErrorReport::SILENT), - max_depth: 10, - }; + let reporter = ErrorOnlyReporter::new(ErrorReport::SILENT); + let builder = FsTreeBuilder::builder() + .root(workspace.to_path_buf()) + .size_getter(GetApparentSize) + .hardlinks_recorder(&HardlinkIgnorant) + .reporter(&reporter) + .max_depth(10) + .build(); let mut data_tree: DataTree = builder.into(); data_tree.par_cull_insignificant_data(0.01); data_tree.par_sort_by(|left, right| left.size().cmp(&right.size()).reverse()); *data_tree.name_mut() = OsStringDisplay::os_string_from("."); - let visualizer = Visualizer:: { - data_tree: &data_tree, - bytes_format: BytesFormat::PlainNumber, - direction: Direction::BottomUp, - bar_alignment: BarAlignment::Left, - column_width_distribution: ColumnWidthDistribution::total(100), - }; + let visualizer = Visualizer::::builder() + .data_tree(&data_tree) + .bytes_format(BytesFormat::PlainNumber) + .direction(Direction::BottomUp) + .bar_alignment(BarAlignment::Left) + .column_width_distribution(ColumnWidthDistribution::total(100)) + .build(); let expected = format!("{visualizer}"); let expected = expected.trim_end(); eprintln!("EXPECTED:\n{expected}\n"); @@ -504,24 +516,25 @@ fn bytes_format_metric() { .pipe(stdout_text); eprintln!("ACTUAL:\n{actual}\n"); - let builder = FsTreeBuilder { - root: workspace.to_path_buf(), - size_getter: GetApparentSize, - hardlinks_recorder: &HardlinkIgnorant, - reporter: &ErrorOnlyReporter::new(ErrorReport::SILENT), - max_depth: 10, - }; + let reporter = ErrorOnlyReporter::new(ErrorReport::SILENT); + let builder = FsTreeBuilder::builder() + .root(workspace.to_path_buf()) + .size_getter(GetApparentSize) + .hardlinks_recorder(&HardlinkIgnorant) + .reporter(&reporter) + .max_depth(10) + .build(); let mut data_tree: DataTree = builder.into(); data_tree.par_cull_insignificant_data(0.01); data_tree.par_sort_by(|left, right| left.size().cmp(&right.size()).reverse()); *data_tree.name_mut() = OsStringDisplay::os_string_from("."); - let visualizer = Visualizer:: { - data_tree: &data_tree, - bytes_format: BytesFormat::MetricUnits, - direction: Direction::BottomUp, - bar_alignment: BarAlignment::Left, - column_width_distribution: ColumnWidthDistribution::total(100), - }; + let visualizer = Visualizer::::builder() + .data_tree(&data_tree) + .bytes_format(BytesFormat::MetricUnits) + .direction(Direction::BottomUp) + .bar_alignment(BarAlignment::Left) + .column_width_distribution(ColumnWidthDistribution::total(100)) + .build(); let expected = format!("{visualizer}"); let expected = expected.trim_end(); eprintln!("EXPECTED:\n{expected}\n"); @@ -543,24 +556,25 @@ fn bytes_format_binary() { .pipe(stdout_text); eprintln!("ACTUAL:\n{actual}\n"); - let builder = FsTreeBuilder { - root: workspace.to_path_buf(), - size_getter: GetApparentSize, - hardlinks_recorder: &HardlinkIgnorant, - reporter: &ErrorOnlyReporter::new(ErrorReport::SILENT), - max_depth: 10, - }; + let reporter = ErrorOnlyReporter::new(ErrorReport::SILENT); + let builder = FsTreeBuilder::builder() + .root(workspace.to_path_buf()) + .size_getter(GetApparentSize) + .hardlinks_recorder(&HardlinkIgnorant) + .reporter(&reporter) + .max_depth(10) + .build(); let mut data_tree: DataTree = builder.into(); data_tree.par_cull_insignificant_data(0.01); data_tree.par_sort_by(|left, right| left.size().cmp(&right.size()).reverse()); *data_tree.name_mut() = OsStringDisplay::os_string_from("."); - let visualizer = Visualizer:: { - data_tree: &data_tree, - bytes_format: BytesFormat::BinaryUnits, - direction: Direction::BottomUp, - bar_alignment: BarAlignment::Left, - column_width_distribution: ColumnWidthDistribution::total(100), - }; + let visualizer = Visualizer::::builder() + .data_tree(&data_tree) + .bytes_format(BytesFormat::BinaryUnits) + .direction(Direction::BottomUp) + .bar_alignment(BarAlignment::Left) + .column_width_distribution(ColumnWidthDistribution::total(100)) + .build(); let expected = format!("{visualizer}"); let expected = expected.trim_end(); eprintln!("EXPECTED:\n{expected}\n"); @@ -581,23 +595,24 @@ fn path_to_workspace() { .pipe(stdout_text); eprintln!("ACTUAL:\n{actual}\n"); - let builder = FsTreeBuilder { - root: workspace.to_path_buf(), - size_getter: DEFAULT_GET_SIZE, - hardlinks_recorder: &HardlinkIgnorant, - reporter: &ErrorOnlyReporter::new(ErrorReport::SILENT), - max_depth: 10, - }; + let reporter = ErrorOnlyReporter::new(ErrorReport::SILENT); + let builder = FsTreeBuilder::builder() + .root(workspace.to_path_buf()) + .size_getter(DEFAULT_GET_SIZE) + .hardlinks_recorder(&HardlinkIgnorant) + .reporter(&reporter) + .max_depth(10) + .build(); let mut data_tree: DataTree = builder.into(); data_tree.par_cull_insignificant_data(0.01); data_tree.par_sort_by(|left, right| left.size().cmp(&right.size()).reverse()); - let visualizer = Visualizer:: { - data_tree: &data_tree, - bytes_format: BytesFormat::MetricUnits, - direction: Direction::BottomUp, - bar_alignment: BarAlignment::Left, - column_width_distribution: ColumnWidthDistribution::total(100), - }; + let visualizer = Visualizer::::builder() + .data_tree(&data_tree) + .bytes_format(BytesFormat::MetricUnits) + .direction(Direction::BottomUp) + .bar_alignment(BarAlignment::Left) + .column_width_distribution(ColumnWidthDistribution::total(100)) + .build(); let expected = format!("{visualizer}"); let expected = expected.trim_end(); eprintln!("EXPECTED:\n{expected}\n"); @@ -624,13 +639,14 @@ fn multiple_names() { let mut data_tree = ["nested", "flat", "empty-dir"] .iter() .map(|name| { - let builder = FsTreeBuilder { - root: workspace.to_path_buf().join(name), - size_getter: GetApparentSize, - hardlinks_recorder: &HardlinkIgnorant, - reporter: &ErrorOnlyReporter::new(ErrorReport::SILENT), - max_depth: 10, - }; + let reporter = ErrorOnlyReporter::new(ErrorReport::SILENT); + let builder = FsTreeBuilder::builder() + .root(workspace.to_path_buf().join(name)) + .size_getter(GetApparentSize) + .hardlinks_recorder(&HardlinkIgnorant) + .reporter(&reporter) + .max_depth(10) + .build(); let mut data_tree: DataTree = builder.into(); *data_tree.name_mut() = OsStringDisplay::os_string_from(name); data_tree @@ -644,13 +660,13 @@ fn multiple_names() { }) .into_par_sorted(|left, right| left.size().cmp(&right.size()).reverse()); data_tree.par_cull_insignificant_data(0.01); - let visualizer = Visualizer:: { - data_tree: &data_tree, - bytes_format: BytesFormat::MetricUnits, - direction: Direction::BottomUp, - bar_alignment: BarAlignment::Left, - column_width_distribution: ColumnWidthDistribution::total(100), - }; + let visualizer = Visualizer::::builder() + .data_tree(&data_tree) + .bytes_format(BytesFormat::MetricUnits) + .direction(Direction::BottomUp) + .bar_alignment(BarAlignment::Left) + .column_width_distribution(ColumnWidthDistribution::total(100)) + .build(); let expected = format!("{visualizer}"); let expected = expected.trim_end(); eprintln!("EXPECTED:\n{expected}\n"); @@ -689,13 +705,14 @@ fn multiple_names_max_depth_2() { let mut data_tree = ["nested", "flat", "empty-dir"] .iter() .map(|name| { - let builder = FsTreeBuilder { - root: workspace.to_path_buf().join(name), - size_getter: GetApparentSize, - hardlinks_recorder: &HardlinkIgnorant, - reporter: &ErrorOnlyReporter::new(ErrorReport::SILENT), - max_depth: 1, - }; + let reporter = ErrorOnlyReporter::new(ErrorReport::SILENT); + let builder = FsTreeBuilder::builder() + .root(workspace.to_path_buf().join(name)) + .size_getter(GetApparentSize) + .hardlinks_recorder(&HardlinkIgnorant) + .reporter(&reporter) + .max_depth(1) + .build(); let mut data_tree: DataTree = builder.into(); *data_tree.name_mut() = OsStringDisplay::os_string_from(name); data_tree @@ -709,13 +726,13 @@ fn multiple_names_max_depth_2() { }) .into_par_sorted(|left, right| left.size().cmp(&right.size()).reverse()); data_tree.par_cull_insignificant_data(0.01); - let visualizer = Visualizer:: { - data_tree: &data_tree, - bytes_format: BytesFormat::MetricUnits, - direction: Direction::BottomUp, - bar_alignment: BarAlignment::Left, - column_width_distribution: ColumnWidthDistribution::total(100), - }; + let visualizer = Visualizer::::builder() + .data_tree(&data_tree) + .bytes_format(BytesFormat::MetricUnits) + .direction(Direction::BottomUp) + .bar_alignment(BarAlignment::Left) + .column_width_distribution(ColumnWidthDistribution::total(100)) + .build(); let expected = format!("{visualizer}"); let expected = expected.trim_end(); eprintln!("EXPECTED:\n{expected}\n"); @@ -749,13 +766,14 @@ fn multiple_names_max_depth_1() { let mut data_tree = ["nested", "flat", "empty-dir"] .iter() .map(|name| { - let builder = FsTreeBuilder { - root: workspace.to_path_buf().join(name), - size_getter: GetApparentSize, - hardlinks_recorder: &HardlinkIgnorant, - reporter: &ErrorOnlyReporter::new(ErrorReport::SILENT), - max_depth: 10, - }; + let reporter = ErrorOnlyReporter::new(ErrorReport::SILENT); + let builder = FsTreeBuilder::builder() + .root(workspace.to_path_buf().join(name)) + .size_getter(GetApparentSize) + .hardlinks_recorder(&HardlinkIgnorant) + .reporter(&reporter) + .max_depth(10) + .build(); let mut data_tree: DataTree = builder.into(); *data_tree.name_mut() = OsStringDisplay::os_string_from(name); data_tree @@ -770,13 +788,13 @@ fn multiple_names_max_depth_1() { .into_par_retained(|_, _| false) .into_par_sorted(|left, right| left.size().cmp(&right.size()).reverse()); data_tree.par_cull_insignificant_data(0.01); - let visualizer = Visualizer:: { - data_tree: &data_tree, - bytes_format: BytesFormat::MetricUnits, - direction: Direction::BottomUp, - bar_alignment: BarAlignment::Left, - column_width_distribution: ColumnWidthDistribution::total(100), - }; + let visualizer = Visualizer::::builder() + .data_tree(&data_tree) + .bytes_format(BytesFormat::MetricUnits) + .direction(Direction::BottomUp) + .bar_alignment(BarAlignment::Left) + .column_width_distribution(ColumnWidthDistribution::total(100)) + .build(); let expected = format!("{visualizer}"); let expected = expected.trim_end(); eprintln!("EXPECTED:\n{expected}\n"); diff --git a/tests/visualizer.rs b/tests/visualizer.rs index 8c839f26..8eded131 100644 --- a/tests/visualizer.rs +++ b/tests/visualizer.rs @@ -35,13 +35,13 @@ macro_rules! test_case { let column_width_distribution = ColumnWidthDistribution::$column_width_function($($column_width_arguments),+); tree.par_retain(|_, depth| depth + 1 < $max_depth); - let actual = Visualizer { - column_width_distribution, - data_tree: &tree, - bytes_format: $bytes_format, - direction: Direction::$direction, - bar_alignment: BarAlignment::$bar_alignment, - } + let actual = Visualizer::builder() + .column_width_distribution(column_width_distribution) + .data_tree(&tree) + .bytes_format($bytes_format) + .direction(Direction::$direction) + .bar_alignment(BarAlignment::$bar_alignment) + .build() .to_string(); let expected = $expected; eprintln!("\nACTUAL:\n{actual}\n");