reimplement sorting; better error messages; strip ./

This commit is contained in:
Ward Wouts 2025-04-29 15:54:01 +02:00
parent 14942ed2b0
commit 12da2e2970
2 changed files with 11 additions and 13 deletions

View file

@ -1,6 +1,6 @@
[package]
name = "mvw"
version = "0.1.5"
version = "0.1.6"
edition = "2021"
[profile.release]

View file

@ -16,7 +16,7 @@ impl ToString for DirListEntry {
fn to_string(&self) -> String {
match self {
DirListEntry::Text(line) => line.clone(),
DirListEntry::Path(path) => path.display().to_string(),
DirListEntry::Path(path) => path.display().to_string()[2..].to_string(),
}
}
}
@ -38,6 +38,7 @@ impl DirList {
.collect()
};
let mut path_list : Vec<DirListEntry> = vec![];
for path in paths {
let path_string = path.display().to_string();
@ -45,8 +46,8 @@ impl DirList {
path_list.push(DirListEntry::Path(path));
}
}
// This sort is probably not /really/ needed, but just nice
//path_list.sort();
path_list.sort_by_key(|dir| dir.to_string());
Self {
safe_source: true,
@ -92,10 +93,7 @@ impl DirList {
let mut file = File::create(target_file).expect("no such file");
for entry in &self.entries {
match entry {
DirListEntry::Text(line) => writeln!(file, "{}", line).expect("Unable to write to file"),
DirListEntry::Path(path) => writeln!(file, "{}", path.display().to_string()).expect("Unable to write to file"),
}
writeln!(file, "{}", entry.to_string()).expect("Unable to write to file");
}
}
@ -117,16 +115,16 @@ impl DirList {
intermediate_files.insert(unique.clone(), target_list.entries[i].to_string().clone());
if ! self.noop {
match &self.entries[i] {
DirListEntry::Text(name) => fs::rename(name.to_string(), &unique).expect("failed to rename file"),
DirListEntry::Path(path) => fs::rename(path.as_path(), &unique).expect("failed to rename file"),
DirListEntry::Text(name) => fs::rename(name.to_string(), &unique).expect("failed to rename file (text based, unique)"),
DirListEntry::Path(path) => fs::rename(path.as_path(), &unique).expect("failed to rename file (path based, unique)"),
}
}
if self.verbose { println!("Moving {} -> {}", self.entries[i].to_string(), unique); }
} else {
if ! self.noop {
match &self.entries[i] {
DirListEntry::Text(name) => fs::rename(name.to_string(), &target_list.entries[i].to_string()).expect("failed to rename file"),
DirListEntry::Path(path) => fs::rename(path.as_path(), &target_list.entries[i].to_string()).expect("failed to rename file"),
DirListEntry::Text(name) => fs::rename(name.to_string(), &target_list.entries[i].to_string()).expect("failed to rename file (text based)"),
DirListEntry::Path(path) => fs::rename(path.as_path(), &target_list.entries[i].to_string()).expect("failed to rename file (path based)"),
}
}
if self.verbose { println!("Moving {} -> {}", self.entries[i].to_string(), target_list.entries[i].to_string()); }
@ -134,7 +132,7 @@ impl DirList {
}
}
for (src, dst) in intermediate_files.iter() {
if ! self.noop { fs::rename(src, dst).expect("failed to rename file"); }
if ! self.noop { fs::rename(src, dst).expect("failed to rename file (intermediate name)"); }
if self.verbose { println!("Moving {} -> {}", src, dst); }
}
Ok(())