fast-down 4.0.3

Download everything fast
Documentation
use std::path::{Path, PathBuf};
use tokio::{fs, io};

/// # Errors
/// 当 `fs::try_exists()` 失败时返回 Error
pub async fn gen_unique_path(path: impl AsRef<Path>) -> io::Result<PathBuf> {
    let path = path.as_ref();
    if !fs::try_exists(path).await? {
        return Ok(path.into());
    }
    let stem = path.file_stem().unwrap_or_default();
    let ext = path.extension();
    for i in 1.. {
        let mut new_name = stem.to_os_string();
        new_name.push(" (");
        new_name.push(i.to_string());
        if let Some(ext) = ext {
            new_name.push(").");
            new_name.push(ext);
        } else {
            new_name.push(")");
        }
        let new_path = path.with_file_name(new_name);
        if !fs::try_exists(&new_path).await? {
            return Ok(new_path);
        }
    }
    unreachable!()
}