use accessor and creator methods for new types in lib

This commit is contained in:
Joe Ardent
2018-03-21 00:09:23 -07:00
parent aa963defda
commit 381d286847
2 changed files with 66 additions and 52 deletions

View File

@@ -2,34 +2,68 @@ use std::cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd};
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Node { pub struct Node {
pub dir: DirEnt, entry: DirEnt,
pub children: Vec<Node>, children: Vec<Node>,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct DirEnt { pub struct DirEnt {
pub name: String, name: String,
pub size: u64, size: u64,
}
impl Node {
pub fn new(entry: DirEnt, children: Vec<Node>) -> Self {
Node {
entry: entry,
children: children,
}
}
pub fn children(&self) -> &Vec<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
}
pub fn size(&self) -> u64 {
self.size
}
} }
impl Ord for Node { impl Ord for Node {
fn cmp(&self, other: &Self) -> Ordering { fn cmp(&self, other: &Self) -> Ordering {
if self.dir.size > other.dir.size { if self.entry.size > other.entry.size {
Ordering::Less Ordering::Less
} else if self.dir.size < other.dir.size { } else if self.entry.size < other.entry.size {
Ordering::Greater Ordering::Greater
} else { } else {
let my_slashes = self.dir.name.matches('/').count(); let my_slashes = self.entry.name.matches('/').count();
let other_slashes = other.dir.name.matches('/').count(); let other_slashes = other.entry.name.matches('/').count();
if my_slashes > other_slashes { if my_slashes > other_slashes {
Ordering::Greater Ordering::Greater
} else if my_slashes < other_slashes { } else if my_slashes < other_slashes {
Ordering::Less Ordering::Less
} else { } else {
if self.dir.name < other.dir.name { if self.entry.name < other.entry.name {
Ordering::Less Ordering::Less
} else if self.dir.name > other.dir.name { } else if self.entry.name > other.entry.name {
Ordering::Greater Ordering::Greater
} else { } else {
Ordering::Equal Ordering::Equal
@@ -45,7 +79,7 @@ impl PartialOrd for Node {
} }
impl PartialEq for Node { impl PartialEq for Node {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
(&self.dir.name, self.dir.size) == (&other.dir.name, other.dir.size) (&self.entry.name, self.entry.size) == (&other.entry.name, other.entry.size)
} }
} }
impl Eq for Node {} impl Eq for Node {}

View File

@@ -13,34 +13,25 @@ use self::ansi_term::Colour::Fixed;
pub fn get_dir_tree(filenames: &Vec<&str>) -> (bool, Vec<Node>) { pub fn get_dir_tree(filenames: &Vec<&str>) -> (bool, Vec<Node>) {
let mut permissions = true; let mut permissions = true;
let mut results = vec![]; let mut results = vec![];
for b in filenames { for &b in filenames {
let mut new_name = String::from(*b); let mut new_name = String::from(b);
while new_name.chars().last() == Some('/') && new_name.len() != 1 { while new_name.chars().last() == Some('/') && new_name.len() != 1 {
new_name.pop(); new_name.pop();
} }
let (hp, data) = examine_dir_str(new_name); let (hp, data) = examine_dir_str(&new_name);
permissions = permissions && hp; permissions = permissions && hp;
results.push(data); results.push(data);
} }
(permissions, results) (permissions, results)
} }
fn examine_dir_str(loc: String) -> (bool, Node) { fn examine_dir_str(loc: &str) -> (bool, Node) {
let mut inodes: HashSet<u64> = HashSet::new(); let mut inodes: HashSet<u64> = HashSet::new();
let (hp, result) = examine_dir(fs::read_dir(&loc), &mut inodes); let (hp, result) = examine_dir(fs::read_dir(loc), &mut inodes);
// This needs to be folded into the below recursive call somehow // This needs to be folded into the below recursive call somehow
let new_size = result.iter().fold(0, |a, b| a + b.dir.size); let new_size = result.iter().fold(0, |a, b| a + b.entry().size());
( (hp, Node::new(DirEnt::new(loc, new_size), result))
hp,
Node {
dir: DirEnt {
name: loc,
size: new_size,
},
children: result,
},
)
} }
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
@@ -101,22 +92,11 @@ fn examine_dir(a_dir: io::Result<ReadDir>, inodes: &mut HashSet<u64>) -> (bool,
if d.path().is_dir() && !file_type.is_symlink() { if d.path().is_dir() && !file_type.is_symlink() {
let (hp, recursive) = examine_dir(fs::read_dir(d.path()), inodes); let (hp, recursive) = examine_dir(fs::read_dir(d.path()), inodes);
have_permission = have_permission && hp; have_permission = have_permission && hp;
let new_size = recursive.iter().fold(size, |a, b| a + b.dir.size); let new_size =
result.push(Node { recursive.iter().fold(size, |a, b| a + b.entry().size());
dir: DirEnt { result.push(Node::new(DirEnt::new(&s, new_size), recursive))
name: s,
size: new_size,
},
children: recursive,
})
} else { } else {
result.push(Node { result.push(Node::new(DirEnt::new(&s, size), vec![]))
dir: DirEnt {
name: s,
size: size,
},
children: vec![],
})
} }
} }
(_, None) => have_permission = false, (_, None) => have_permission = false,
@@ -146,7 +126,7 @@ pub fn find_big_ones<'a>(l: &'a Vec<Node>, max_to_show: usize) -> Vec<&Node> {
// Must be a list of pointers into new_l otherwise b_list will go out of scope // Must be a list of pointers into new_l otherwise b_list will go out of scope
// when it is deallocated // when it is deallocated
let mut b_list: Vec<&Node> = new_l[processed_pointer] let mut b_list: Vec<&Node> = new_l[processed_pointer]
.children .children()
.iter() .iter()
.map(|a| a) .map(|a| a)
.collect(); .collect();
@@ -184,11 +164,11 @@ fn display_node<S: Into<String>>(
is = is.replace("├──", ""); is = is.replace("├──", "");
is = is.replace("├─┬", ""); is = is.replace("├─┬", "");
let printable_node_slashes = node_to_print.dir.name.matches('/').count(); let printable_node_slashes = node_to_print.entry().name().matches('/').count();
let mut num_siblings = to_display.iter().fold(0, |a, b| { let mut num_siblings = to_display.iter().fold(0, |a, b| {
if node_to_print.children.contains(b) if node_to_print.children().contains(b)
&& b.dir.name.matches('/').count() == printable_node_slashes + 1 && b.entry().name().matches('/').count() == printable_node_slashes + 1
{ {
a + 1 a + 1
} else { } else {
@@ -199,11 +179,11 @@ fn display_node<S: Into<String>>(
let mut is_biggest = true; let mut is_biggest = true;
let mut has_display_children = false; let mut has_display_children = false;
for node in to_display { for node in to_display {
if node_to_print.children.contains(node) { if node_to_print.children().contains(node) {
let has_children = node.children.len() > 0; let has_children = node.children().len() > 0;
if node.dir.name.matches("/").count() == printable_node_slashes + 1 { if node.entry().name().matches("/").count() == printable_node_slashes + 1 {
num_siblings -= 1; num_siblings -= 1;
for ref n in node.children.iter() { for ref n in node.children().iter() {
has_display_children = has_display_children || to_display.contains(n); has_display_children = has_display_children || to_display.contains(n);
} }
let has_children = has_children && has_display_children; let has_children = has_children && has_display_children;
@@ -236,7 +216,7 @@ fn display_node<S: Into<String>>(
} }
fn print_this_node(node_to_print: &Node, is_biggest: bool, depth: u8, indentation_str: &str) { 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.dir.size),); let padded_size = format!("{:>5}", human_readable_number(node_to_print.entry().size()),);
println!( println!(
"{} {} {}", "{} {} {}",
if is_biggest { if is_biggest {
@@ -247,7 +227,7 @@ fn print_this_node(node_to_print: &Node, is_biggest: bool, depth: u8, indentatio
indentation_str, indentation_str,
Fixed(7) Fixed(7)
.on(Fixed(cmp::min(8, (depth) as u8) + 231)) .on(Fixed(cmp::min(8, (depth) as u8) + 231))
.paint(node_to_print.dir.name.to_string()) .paint(node_to_print.entry().name().to_string())
); );
} }