Avoid lossy strings when renaming and matching files

This commit is contained in:
Leonard Steppy 2025-02-03 18:06:57 +01:00
parent d1924aa758
commit c143b29a3d
2 changed files with 29 additions and 23 deletions

View File

@ -72,7 +72,12 @@ pub enum Action {
} }
impl Action { impl Action {
pub fn rename<S>(new_name: S) -> Self where S: AsRef<OsStr> { pub fn rename<S>(new_name: S) -> Self
Self::Rename {new_name: new_name.as_ref().to_owned()} where
S: AsRef<OsStr>,
{
Self::Rename {
new_name: new_name.as_ref().to_owned(),
}
} }
} }

View File

@ -355,28 +355,29 @@ fn main() -> Result<(), String> {
.expect("path points to file")] .expect("path points to file")]
} }
} }
OldVersionPolicy::Archive => { OldVersionPolicy::Archive => present_file_names
//TODO avoid lossy match .iter()
present_file_names .filter(|file| file_matcher.matches(file))
.iter() .map(|file| {
.filter(|file| file_matcher.matches(file)) FileAction::new(
.map(|file| { file,
FileAction::new( Action::rename(
file, osf!(file)
Action::rename(format!( + file
"{}{}", .to_string_lossy()
file.to_string_lossy(), .chars()
file.to_string_lossy().chars().last().unwrap_or('1') .last()
)), .unwrap_or('1')
) .to_string(),
.expect("path points to file") ),
}) )
.chain(once(add_action)) .expect("path points to file")
.collect() })
}, .chain(once(add_action))
.collect(),
OldVersionPolicy::Delete => { OldVersionPolicy::Delete => {
//TODO avoid lossy match let mut actions = present_file_names
let mut actions = present_file_names.iter() .iter()
.filter(|file| file_matcher.matches(file)) .filter(|file| file_matcher.matches(file))
.map(|file| { .map(|file| {
//special case -> file has the same name as current file, then we just need to replace it //special case -> file has the same name as current file, then we just need to replace it