Massive refactor

WIP

Replace array of (string, int) pairs with tree of nodes.

A tree of nodes more accurately represents the underlying file structure
and hence is a better fit for the problem space.

Regression: Reverse doesn't work in this commit.

I suspect more methods can be simplifed and reduced.
This commit is contained in:
andy.boot
2019-10-05 17:57:47 +01:00
parent e03094a4fa
commit db6c8a019d
3 changed files with 76 additions and 127 deletions
+11 -10
View File
@@ -7,6 +7,13 @@ use walkdir::WalkDir;
mod platform;
use self::platform::*;
#[derive(Debug)]
pub struct Node {
pub name: String,
pub size: u64,
pub children: Vec<Box<Node>>,
}
pub fn simplify_dir_names(filenames: Vec<&str>) -> HashSet<String> {
let mut top_level_names: HashSet<String> = HashSet::new();
@@ -64,14 +71,6 @@ pub fn strip_end_slash(s: &str) -> String {
new_name
}
pub fn strip_end_slash_including_root(s: &str) -> String {
let mut new_name = String::from(s);
while new_name.ends_with('/') || new_name.ends_with("/.") {
new_name.pop();
}
new_name
}
fn examine_dir(
top_dir: &str,
apparent_size: bool,
@@ -93,6 +92,7 @@ fn examine_dir(
inodes.insert(inode_dev_pair);
}
}
// This path and all its parent paths have their counter incremented
let mut e_path = e.path().to_path_buf();
loop {
let path_name = e_path.to_string_lossy().to_string();
@@ -112,7 +112,8 @@ fn examine_dir(
}
}
}
pub fn compare_tuple(a: &(String, u64), b: &(String, u64)) -> Ordering {
pub fn sort_by_size_first_name_second(a: &(String, u64), b: &(String, u64)) -> Ordering {
let result = b.1.cmp(&a.1);
if result == Ordering::Equal {
a.0.cmp(&b.0)
@@ -132,7 +133,7 @@ pub fn compare_tuple_smallest_first(a: &(String, u64), b: &(String, u64)) -> Ord
pub fn sort(data: HashMap<String, u64>) -> Vec<(String, u64)> {
let mut new_l: Vec<(String, u64)> = data.iter().map(|(a, b)| (a.clone(), *b)).collect();
new_l.sort_by(|a, b| compare_tuple(&a, &b));
new_l.sort_by(|a, b| sort_by_size_first_name_second(&a, &b));
new_l
}