From a3ab5bfe0f6131c3956f70fcf1f3a6490c3e5b88 Mon Sep 17 00:00:00 2001 From: "andy.boot" Date: Thu, 5 Jan 2023 00:13:08 +0000 Subject: [PATCH] refactor create AggregateData for filter.rs --- src/filter.rs | 92 +++++++++++++++++++++++++++------------------------ src/main.rs | 24 ++++++++------ 2 files changed, 62 insertions(+), 54 deletions(-) diff --git a/src/filter.rs b/src/filter.rs index dda0f71..d8df86e 100644 --- a/src/filter.rs +++ b/src/filter.rs @@ -5,14 +5,15 @@ use std::collections::HashSet; use std::path::Path; use std::path::PathBuf; -pub fn get_biggest( - top_level_nodes: Vec, - min_size: Option, - only_dir: bool, - n: usize, - depth: usize, - using_a_filter: bool, -) -> Option { +pub struct AggregateData { + pub min_size: Option, + pub only_dir: bool, + pub number_of_lines: usize, + pub depth: usize, + pub using_a_filter: bool, +} + +pub fn get_biggest(top_level_nodes: Vec, display_data: AggregateData) -> Option { if top_level_nodes.is_empty() { // perhaps change this, bring back Error object? return None; @@ -31,69 +32,72 @@ pub fn get_biggest( depth: 0, }; // Always include the base nodes if we add a 'parent' (total) node - heap = add_children(using_a_filter, min_size, only_dir, &root, usize::MAX, heap); + heap = always_add_children(&display_data, &root, heap); } else { root = top_level_nodes.into_iter().next().unwrap(); - heap = add_children(using_a_filter, min_size, only_dir, &root, depth, heap); + heap = add_children(&display_data, &root, heap); } - let remaining = n.checked_sub(number_top_level_nodes).unwrap_or(0); - fill_remaining_lines( - heap, - &root, - min_size, - only_dir, - remaining, - depth, - using_a_filter, - ) + let nol = display_data.number_of_lines; + let remaining = nol.saturating_sub(number_top_level_nodes); + fill_remaining_lines(heap, &root, remaining, display_data) } pub fn fill_remaining_lines<'a>( mut heap: BinaryHeap<&'a Node>, root: &'a Node, - min_size: Option, - only_dir: bool, - remaining: usize, - depth: usize, - using_a_filter: bool, + remaining_lines: usize, + display_data: AggregateData, ) -> Option { let mut allowed_nodes = HashSet::new(); allowed_nodes.insert(root.name.as_path()); - for _ in 0..remaining { + for _ in 0..remaining_lines { let line = heap.pop(); match line { Some(line) => { allowed_nodes.insert(line.name.as_path()); - heap = add_children(using_a_filter, min_size, only_dir, line, depth, heap); + heap = add_children(&display_data, line, heap); } None => break, } } - recursive_rebuilder(&allowed_nodes, &root) + recursive_rebuilder(&allowed_nodes, root) } fn add_children<'a>( - using_a_filter: bool, - min_size: Option, - only_dir: bool, + display_data: &AggregateData, + file_or_folder: &'a Node, + heap: BinaryHeap<&'a Node>, +) -> BinaryHeap<&'a Node> { + if display_data.depth > file_or_folder.depth { + always_add_children(display_data, file_or_folder, heap) + } else { + heap + } +} + +fn always_add_children<'a>( + display_data: &AggregateData, file_or_folder: &'a Node, - depth: usize, mut heap: BinaryHeap<&'a Node>, ) -> BinaryHeap<&'a Node> { - if depth > file_or_folder.depth { - heap.extend( - file_or_folder - .children - .iter() - .filter(|c| match min_size { - Some(ms) => c.size > ms as u64, - None => !using_a_filter || c.name.is_file() || c.size > 0, - }) - .filter(|c| if only_dir { c.name.is_dir() } else { true }), - ) - } + heap.extend( + file_or_folder + .children + .iter() + .filter(|c| match display_data.min_size { + Some(ms) => c.size > ms as u64, + None => !display_data.using_a_filter || c.name.is_file() || c.size > 0, + }) + .filter(|c| { + if display_data.only_dir { + c.name.is_dir() + } else { + true + } + }), + ); heap } diff --git a/src/main.rs b/src/main.rs index 19f69d5..6f9077b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,8 @@ mod platform; mod utils; use crate::cli::build_cli; +use dir_walker::WalkData; +use filter::AggregateData; use std::collections::HashSet; use std::io::BufRead; use std::process; @@ -18,7 +20,7 @@ use sysinfo::{System, SystemExt}; use self::display::draw_it; use clap::Values; use config::get_config; -use dir_walker::{walk_it, WalkData}; +use dir_walker::walk_it; use filter::get_biggest; use filter_type::get_all_file_types; use rayon::ThreadPoolBuildError; @@ -175,17 +177,19 @@ fn main() { let iso = config.get_iso(&options); let (top_level_nodes, has_errors) = walk_it(simplified_dirs, walk_data); - let tree = match summarize_file_types { true => get_all_file_types(&top_level_nodes, number_of_lines), - false => get_biggest( - top_level_nodes, - config.get_min_size(&options, iso), - config.get_only_dir(&options), - number_of_lines, - depth, - options.values_of("filter").is_some() || options.value_of("invert_filter").is_some(), - ), + false => { + let agg_data = AggregateData { + min_size: config.get_min_size(&options, iso), + only_dir: config.get_only_dir(&options), + number_of_lines, + depth, + using_a_filter: options.values_of("filter").is_some() + || options.value_of("invert_filter").is_some(), + }; + get_biggest(top_level_nodes, agg_data) + } }; if has_errors {