let clippy help

This commit is contained in:
Ward Wouts 2024-11-20 15:28:30 +01:00
parent b00b5e9352
commit a823844538
2 changed files with 15 additions and 16 deletions

2
Cargo.lock generated
View file

@ -99,7 +99,7 @@ checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89"
[[package]]
name = "mvw"
version = "0.1.0"
version = "0.1.3"
dependencies = [
"dialoguer",
"rand",

View file

@ -48,7 +48,7 @@ fn lock_dir() {
fn unlock_dir() {
if Path::new(LOCK_FILE).is_file() {
let _result = fs::remove_file(LOCK_FILE).expect("Could not remove LOCK_FILE");
fs::remove_file(LOCK_FILE).expect("Could not remove LOCK_FILE");
}
}
@ -68,7 +68,7 @@ fn create_temp_file(paths: &Vec<String>) -> String {
fn remove_temp_file(tmpfile: &String) {
if Path::new(tmpfile).is_file() {
let _result = fs::remove_file(tmpfile).expect("Could not remove tempfile");
fs::remove_file(tmpfile).expect("Could not remove tempfile");
}
}
@ -88,12 +88,12 @@ fn read_temp_file(tmpfile: &String) -> Vec<String> {
.collect()
}
fn unique_length(dst_paths: &Vec<String>) -> usize {
let map = dst_paths.into_iter().map(|x| (x, x)).collect::<HashMap<_, _>>();
fn unique_length(dst_paths: &[String]) -> usize {
let map = dst_paths.iter().map(|x| (x, x)).collect::<HashMap<_, _>>();
map.len()
}
fn unique_filename(dst_paths: &Vec<String>) -> String {
fn unique_filename(dst_paths: &[String]) -> String {
let mut string = Alphanumeric.sample_string(&mut rand::thread_rng(), 16);
// Generate unique name that does not exist in dir and is not in dst_paths
@ -103,7 +103,7 @@ fn unique_filename(dst_paths: &Vec<String>) -> String {
string
}
fn move_safely(src_paths: &Vec<String>, dst_paths: &Vec<String>) {
fn move_safely(src_paths: &[String], dst_paths: &[String]) {
let src_len = src_paths.len();
let mut intermediate_files: HashMap<String, String> = HashMap::new();
@ -112,7 +112,7 @@ fn move_safely(src_paths: &Vec<String>, dst_paths: &Vec<String>) {
// is the destination already in the source list?
// if so, an intermediate file is needed
if src_paths.iter().any(|j| j==&dst_paths[i]) {
let unique = unique_filename(&dst_paths);
let unique = unique_filename(dst_paths);
intermediate_files.insert(unique.clone(), dst_paths[i].clone());
fs::rename(&src_paths[i], &unique).expect("failed to rename file");
println!("Moving {} -> {}", src_paths[i], unique);
@ -123,12 +123,12 @@ fn move_safely(src_paths: &Vec<String>, dst_paths: &Vec<String>) {
}
}
for (src, dst) in intermediate_files.iter() {
fs::rename(&src, &dst).expect("failed to rename file");
fs::rename(src, dst).expect("failed to rename file");
println!("Moving {} -> {}", src, dst);
}
}
fn run_checks(src_paths: &Vec<String>, dst_paths: &Vec<String>) -> bool {
fn run_checks(src_paths: &[String], dst_paths: &[String]) -> bool {
// Make sure there are an equal number of sources and destinations
if src_paths.len() != dst_paths.len() {
println!("ERROR: Source and target list don't have the same number of lines.");
@ -136,26 +136,25 @@ fn run_checks(src_paths: &Vec<String>, dst_paths: &Vec<String>) -> bool {
}
// Make sure destination names are not empty
if dst_paths.iter().any(|i| i=="") {
if dst_paths.iter().any(|i| i.is_empty()) {
println!("ERROR: You can't move to empty names.");
return true
}
// Make sure all destination files are unique
let dst_paths_length_unique = unique_length(&dst_paths);
let dst_paths_length_unique = unique_length(dst_paths);
if dst_paths.len() != dst_paths_length_unique {
println!("ERROR: You're trying to move multiple files to the same name.");
show_doubles(&src_paths, &dst_paths);
show_doubles(src_paths, dst_paths);
return true
}
false
}
fn show_doubles(src_paths: &Vec<String>, dst_paths: &Vec<String>) {
fn show_doubles(src_paths: &[String], dst_paths: &[String]) {
let mut paths = HashMap::new();
let dst_len = dst_paths.len();
for i in 0..dst_len {
for (i, _) in dst_paths.iter().enumerate() {
paths.entry(&dst_paths[i]).or_insert(Vec::new());
paths.get_mut(&dst_paths[i]).unwrap().push(i);
}