Obey new clippy

Clippy is like having a reviewer fix your dodgy code.
This commit is contained in:
bootandy
2019-06-30 20:05:03 +01:00
parent 12775db94b
commit 876609f2cb
5 changed files with 69 additions and 43 deletions
+25 -14
View File
@@ -12,7 +12,7 @@ pub fn draw_it(
depth: Option<u64>,
base_dirs: HashSet<String>,
to_display: Vec<(String, u64)>,
) -> () {
) {
if !permissions {
eprintln!("Did not have permissions for all directories");
}
@@ -20,12 +20,20 @@ pub fn draw_it(
for &(ref k, _) in to_display.iter() {
if base_dirs.contains(k) {
display_node(&k, &mut found, &to_display, true, short_paths, depth, "─┬");
display_node(
&k,
&mut found,
&to_display,
true,
short_paths,
depth,
"─┬",
);
}
}
}
fn get_size(nodes: &Vec<(String, u64)>, node_to_print: &String) -> Option<u64> {
fn get_size(nodes: &[(String, u64)], node_to_print: &str) -> Option<u64> {
for &(ref k, ref v) in nodes.iter() {
if *k == *node_to_print {
return Some(*v);
@@ -35,9 +43,9 @@ fn get_size(nodes: &Vec<(String, u64)>, node_to_print: &String) -> Option<u64> {
}
fn display_node<S: Into<String>>(
node_to_print: &String,
node_to_print: &str,
found: &mut HashSet<String>,
to_display: &Vec<(String, u64)>,
to_display: &[(String, u64)],
is_biggest: bool,
short_paths: bool,
depth: Option<u64>,
@@ -56,7 +64,7 @@ fn display_node<S: Into<String>>(
match get_size(to_display, node_to_print) {
None => println!("Can not find path: {}", node_to_print),
Some(size) => {
let ntp: &str = node_to_print.as_ref();
let ntp: &str = node_to_print;
let num_slashes = node_to_print.matches('/').count();
let is = indentation_str.into();
@@ -96,7 +104,7 @@ fn clean_indentation_string<S: Into<String>>(s: S) -> String {
is
}
fn count_siblings(to_display: &Vec<(String, u64)>, num_slashes: usize, ntp: &str) -> u64 {
fn count_siblings(to_display: &[(String, u64)], num_slashes: usize, ntp: &str) -> u64 {
to_display.iter().fold(0, |a, b| {
if b.0.starts_with(ntp) && b.0.matches('/').count() == num_slashes + 1 {
a + 1
@@ -106,7 +114,12 @@ fn count_siblings(to_display: &Vec<(String, u64)>, num_slashes: usize, ntp: &str
})
}
fn has_children(to_display: &Vec<(String, u64)>, new_depth: Option<u64>, ntp: &str, num_slashes: usize) -> bool {
fn has_children(
to_display: &[(String, u64)],
new_depth: Option<u64>,
ntp: &str,
num_slashes: usize,
) -> bool {
if new_depth.is_none() || new_depth.unwrap() != 1 {
for &(ref k2, _) in to_display.iter() {
if k2.starts_with(ntp) && k2.matches('/').count() == num_slashes + 1 {
@@ -114,7 +127,7 @@ fn has_children(to_display: &Vec<(String, u64)>, new_depth: Option<u64>, ntp: &s
}
}
}
return false;
false
}
fn get_tree_chars(has_smaller_siblings: bool, has_children: bool) -> &'static str {
@@ -124,12 +137,10 @@ fn get_tree_chars(has_smaller_siblings: bool, has_children: bool) -> &'static st
} else {
"└──"
}
} else if has_children {
"├─┬"
} else {
if has_children {
"├─┬"
} else {
"├──"
}
"├──"
}
}
+7 -1
View File
@@ -91,7 +91,13 @@ fn main() {
Some(d) => trim_deep_ones(sorted_data, d, &simplified_dirs),
}
};
draw_it(permissions, !use_full_path, depth, simplified_dirs, biggest_ones);
draw_it(
permissions,
!use_full_path,
depth,
simplified_dirs,
biggest_ones,
);
}
#[cfg(test)]
+14 -2
View File
@@ -45,7 +45,13 @@ fn main_output(short_paths: bool) -> String {
{}
{}",
format_string("src/test_dir", true, short_paths, " 4.0K", "─┬"),
format_string("src/test_dir/many", true, short_paths, " 4.0K", " └─┬",),
format_string(
"src/test_dir/many",
true,
short_paths,
" 4.0K",
" └─┬",
),
format_string(
"src/test_dir/many/hello_file",
true,
@@ -71,7 +77,13 @@ fn main_output(short_paths: bool) -> String {
{}
{}",
format_string("src/test_dir", true, short_paths, " 12K", "─┬"),
format_string("src/test_dir/many", true, short_paths, " 8.0K", " └─┬",),
format_string(
"src/test_dir/many",
true,
short_paths,
" 8.0K",
" └─┬",
),
format_string(
"src/test_dir/many/hello_file",
true,
+22 -25
View File
@@ -50,49 +50,46 @@ pub fn get_dir_tree(
fn strip_end_slashes(s: &str) -> String {
let mut new_name = String::from(s);
while new_name.chars().last() == Some('/') && new_name.len() != 1 {
while new_name.ends_with('/') && new_name.len() != 1 {
new_name.pop();
}
new_name
}
fn examine_dir(
top_dir: &String,
top_dir: &str,
apparent_size: bool,
inodes: &mut HashSet<(u64, u64)>,
data: &mut HashMap<String, u64>,
permissions: &mut u64,
) {
for entry in WalkDir::new(top_dir) {
match entry {
Ok(e) => {
let maybe_size_and_inode = get_metadata(&e, apparent_size);
if let Ok(e) = entry {
let maybe_size_and_inode = get_metadata(&e, apparent_size);
match maybe_size_and_inode {
Some((size, maybe_inode)) => {
if !apparent_size {
if let Some(inode_dev_pair) = maybe_inode {
if inodes.contains(&inode_dev_pair) {
continue;
}
inodes.insert(inode_dev_pair);
match maybe_size_and_inode {
Some((size, maybe_inode)) => {
if !apparent_size {
if let Some(inode_dev_pair) = maybe_inode {
if inodes.contains(&inode_dev_pair) {
continue;
}
}
let mut e_path = e.path().to_path_buf();
loop {
let path_name = e_path.to_string_lossy().to_string();
let s = data.entry(path_name.clone()).or_insert(0);
*s += size;
if path_name == *top_dir {
break;
}
e_path.pop();
inodes.insert(inode_dev_pair);
}
}
None => *permissions += 1,
let mut e_path = e.path().to_path_buf();
loop {
let path_name = e_path.to_string_lossy().to_string();
let s = data.entry(path_name.clone()).or_insert(0);
*s += size;
if path_name == *top_dir {
break;
}
e_path.pop();
}
}
None => *permissions += 1,
}
_ => {}
}
}
}
+1 -1
View File
@@ -9,7 +9,7 @@ fn get_block_size() -> u64 {
#[cfg(target_family = "unix")]
pub fn get_metadata(d: &DirEntry, use_apparent_size: bool) -> Option<(u64, Option<(u64, u64)>)> {
use std::os::unix::fs::MetadataExt;
d.metadata().ok().map_or(None, |md| {
d.metadata().ok().and_then(|md| {
let inode = Some((md.ino(), md.dev()));
if use_apparent_size {
Some((md.len(), inode))