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 {
pub fn rename<S>(new_name: S) -> Self where S: AsRef<OsStr> {
Self::Rename {new_name: new_name.as_ref().to_owned()}
pub fn rename<S>(new_name: S) -> Self
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")]
}
}
OldVersionPolicy::Archive => {
//TODO avoid lossy match
present_file_names
.iter()
.filter(|file| file_matcher.matches(file))
.map(|file| {
FileAction::new(
file,
Action::rename(format!(
"{}{}",
file.to_string_lossy(),
file.to_string_lossy().chars().last().unwrap_or('1')
)),
)
.expect("path points to file")
})
.chain(once(add_action))
.collect()
},
OldVersionPolicy::Archive => present_file_names
.iter()
.filter(|file| file_matcher.matches(file))
.map(|file| {
FileAction::new(
file,
Action::rename(
osf!(file)
+ file
.to_string_lossy()
.chars()
.last()
.unwrap_or('1')
.to_string(),
),
)
.expect("path points to file")
})
.chain(once(add_action))
.collect(),
OldVersionPolicy::Delete => {
//TODO avoid lossy match
let mut actions = present_file_names.iter()
let mut actions = present_file_names
.iter()
.filter(|file| file_matcher.matches(file))
.map(|file| {
//special case -> file has the same name as current file, then we just need to replace it