~soywod/pimalaya

email-lib: make cloning in renaming function explicit v1 APPLIED

Perma Alesheikh: 1
 make cloning in renaming function explicit

 1 files changed, 11 insertions(+), 12 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~soywod/pimalaya/patches/49340/mbox | git am -3
Learn more about email & git

[PATCH email-lib] make cloning in renaming function explicit Export this patch

Since we need an owned path, and we return that owned path, we can just
ask for an owned path. This has two advantages:
1. Generally, it is better not to hide the costs of calling a function.
2. If the caller does not need the variable any more, they can just pass
  the ownership. Which means less allocations.

Signed-off-by: Perma Alesheikh <me@prma.dev>
---
 email/src/account/config/mod.rs | 23 +++++++++++------------
 1 file changed, 11 insertions(+), 12 deletions(-)

diff --git a/email/src/account/config/mod.rs b/email/src/account/config/mod.rs
index 77197f8..3ee2587 100644
--- a/email/src/account/config/mod.rs
+++ b/email/src/account/config/mod.rs
@@ -174,7 +174,7 @@ pub fn get_download_file_path(&self, path: impl AsRef<Path>) -> Result<PathBuf>

        let final_path = self.get_downloads_dir().join(file_name);

        rename_file_if_duplicate(&final_path, |path, _count| path.is_file())
        rename_file_if_duplicate(final_path, |path, _count| path.is_file())
    }

    #[cfg(feature = "account-sync")]
@@ -581,7 +581,7 @@ fn from(val: AccountConfig) -> Self {
/// creates a new path with an auto-incremented integer suffix and
/// returs it, otherwise returs the original file path.
pub(crate) fn rename_file_if_duplicate(
    original_fpath: &PathBuf,
    mut original_fpath: PathBuf,
    is_file: impl Fn(&PathBuf, u8) -> bool,
) -> Result<PathBuf> {
    let mut count = 0;
@@ -590,20 +590,19 @@ pub(crate) fn rename_file_if_duplicate(
        .and_then(OsStr::to_str)
        .map(|fext| String::from(".") + fext)
        .unwrap_or_default();
    let mut fpath = original_fpath.clone();

    while is_file(&fpath, count) {
    while is_file(&original_fpath, count) {
        count += 1;
        fpath.set_file_name(OsStr::new(
        original_fpath.set_file_name(OsStr::new(
            &original_fpath
                .file_stem()
                .and_then(OsStr::to_str)
                .map(|fstem| format!("{}_{}{}", fstem, count, fext))
                .ok_or_else(|| Error::ParseDownloadFileNameError(fpath.to_owned()))?,
                .ok_or_else(|| Error::ParseDownloadFileNameError(original_fpath.to_owned()))?,
        ));
    }

    Ok(fpath)
    Ok(original_fpath)
}

#[cfg(test)]
@@ -616,33 +615,33 @@ fn rename_file_if_duplicate() {

        // when file path is unique
        assert!(matches!(
            super::rename_file_if_duplicate(&path, |_, _| false),
            super::rename_file_if_duplicate(path.clone(), |_, _| false),
            Ok(path) if path == PathBuf::from("downloads/file.ext")
        ));

        // when 1 file path already exist
        assert!(matches!(
            super::rename_file_if_duplicate(&path, |_, count| count <  1),
            super::rename_file_if_duplicate(path.clone(), |_, count| count <  1),
            Ok(path) if path == PathBuf::from("downloads/file_1.ext")
        ));

        // when 5 file paths already exist
        assert!(matches!(
            super::rename_file_if_duplicate(&path, |_, count| count < 5),
            super::rename_file_if_duplicate(path, |_, count| count < 5),
            Ok(path) if path == PathBuf::from("downloads/file_5.ext")
        ));

        // when file path has no extension
        let path = PathBuf::from("downloads/file");
        assert!(matches!(
            super::rename_file_if_duplicate(&path, |_, count| count < 5),
            super::rename_file_if_duplicate(path, |_, count| count < 5),
            Ok(path) if path == PathBuf::from("downloads/file_5")
        ));

        // when file path has 2 extensions
        let path = PathBuf::from("downloads/file.ext.ext2");
        assert!(matches!(
            super::rename_file_if_duplicate(&path, |_, count| count < 5),
            super::rename_file_if_duplicate(path, |_, count| count < 5),
            Ok(path) if path == PathBuf::from("downloads/file.ext_5.ext2")
        ));
    }
-- 
2.43.0