From 120b4e16e7560d8ac464837d72a2a5003a56ddc5 Mon Sep 17 00:00:00 2001 From: bootandy Date: Tue, 3 Apr 2018 17:05:28 +0100 Subject: [PATCH] Squash Node and DirEnt objects into single object --- src/display.rs | 10 +++++----- src/lib.rs | 41 ++++++++++++----------------------------- src/utils/mod.rs | 13 ++++++------- 3 files changed, 23 insertions(+), 41 deletions(-) diff --git a/src/display.rs b/src/display.rs index c0c5fc2..cdd9e99 100644 --- a/src/display.rs +++ b/src/display.rs @@ -29,11 +29,11 @@ fn display_node>( is = is.replace("├──", "│ "); is = is.replace("├─┬", "│ "); - let printable_node_slashes = node_to_print.entry().name().matches('/').count(); + let printable_node_slashes = node_to_print.name().matches('/').count(); let mut num_siblings = to_display.iter().fold(0, |a, b| { if node_to_print.children().contains(b) - && b.entry().name().matches('/').count() == printable_node_slashes + 1 + && b.name().matches('/').count() == printable_node_slashes + 1 { a + 1 } else { @@ -49,7 +49,7 @@ fn display_node>( .fold(false, |has_kids, n| has_kids || to_display.contains(&n)); let has_children = node.children().len() > 0 && has_display_children; - if node.entry().name().matches('/').count() == printable_node_slashes + 1 { + if node.name().matches('/').count() == printable_node_slashes + 1 { num_siblings -= 1; let tree_chars = { @@ -81,7 +81,7 @@ fn display_node>( } fn print_this_node(node_to_print: &Node, is_biggest: bool, depth: u8, indentation_str: &str) { - let padded_size = format!("{:>5}", human_readable_number(node_to_print.entry().size()),); + let padded_size = format!("{:>5}", human_readable_number(node_to_print.size()),); println!( "{} {} {}", if is_biggest { @@ -92,7 +92,7 @@ fn print_this_node(node_to_print: &Node, is_biggest: bool, depth: u8, indentatio indentation_str, Fixed(7) .on(Fixed(cmp::min(8, (depth) as u8) + 231)) - .paint(node_to_print.entry().name().to_string()) + .paint(node_to_print.name().to_string()) ); } diff --git a/src/lib.rs b/src/lib.rs index 68555e0..16f2295 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,21 +2,17 @@ use std::cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd}; #[derive(Clone, Debug)] pub struct Node { - entry: DirEnt, + name: String, + size: u64, children: Vec, } -#[derive(Clone, Debug)] -pub struct DirEnt { - name: String, - size: u64, -} - impl Node { - pub fn new(entry: DirEnt, children: Vec) -> Self { + pub fn new>(name: S, size: u64, children: Vec) -> Self { Node { - entry: entry, children: children, + name: name.into(), + size: size, } } @@ -24,19 +20,6 @@ impl Node { &self.children } - pub fn entry(&self) -> &DirEnt { - &self.entry - } -} - -impl DirEnt { - pub fn new(name: &str, size: u64) -> Self { - DirEnt { - name: String::from(name), - size: size, - } - } - pub fn name(&self) -> &String { &self.name } @@ -48,22 +31,22 @@ impl DirEnt { impl Ord for Node { fn cmp(&self, other: &Self) -> Ordering { - if self.entry.size > other.entry.size { + if self.size > other.size { Ordering::Less - } else if self.entry.size < other.entry.size { + } else if self.size < other.size { Ordering::Greater } else { - let my_slashes = self.entry.name.matches('/').count(); - let other_slashes = other.entry.name.matches('/').count(); + let my_slashes = self.name.matches('/').count(); + let other_slashes = other.name.matches('/').count(); if my_slashes > other_slashes { Ordering::Greater } else if my_slashes < other_slashes { Ordering::Less } else { - if self.entry.name < other.entry.name { + if self.name < other.name { Ordering::Less - } else if self.entry.name > other.entry.name { + } else if self.name > other.name { Ordering::Greater } else { Ordering::Equal @@ -79,7 +62,7 @@ impl PartialOrd for Node { } impl PartialEq for Node { fn eq(&self, other: &Self) -> bool { - (&self.entry.name, self.entry.size) == (&other.entry.name, other.entry.size) + (&self.name, self.size) == (&other.name, other.size) } } impl Eq for Node {} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 631ec4d..0851906 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -3,7 +3,7 @@ use std::collections::HashSet; use std::fs::{self, ReadDir}; use std::io; -use dust::{DirEnt, Node}; +use dust::Node; mod platform; use self::platform::*; @@ -28,8 +28,8 @@ fn examine_dir_str(loc: &str, apparent_size: bool) -> (bool, Node) { let (hp, result) = examine_dir(fs::read_dir(loc), apparent_size, &mut inodes); // This needs to be folded into the below recursive call somehow - let new_size = result.iter().fold(0, |a, b| a + b.entry().size()); - (hp, Node::new(DirEnt::new(loc, new_size), result)) + let new_size = result.iter().fold(0, |a, b| a + b.size()); + (hp, Node::new(loc, new_size, result)) } fn examine_dir( @@ -64,11 +64,10 @@ fn examine_dir( let (hp, recursive) = examine_dir(fs::read_dir(d.path()), apparent_size, inodes); have_permission = have_permission && hp; - let new_size = - recursive.iter().fold(size, |a, b| a + b.entry().size()); - result.push(Node::new(DirEnt::new(&s, new_size), recursive)) + let new_size = recursive.iter().fold(size, |a, b| a + b.size()); + result.push(Node::new(s, new_size, recursive)) } else { - result.push(Node::new(DirEnt::new(&s, size), vec![])) + result.push(Node::new(s, size, vec![])) } } (_, None) => have_permission = false,