Generate filenames until there isn't a collision
This would be cool, but only if there was actually filename collisions.
I asked chatgpt, it says I should add a max_attempts variable through to generate_filename that, and it keeps generating until the max_attempts are done, or there is no collision.
You can modify the generate_filename() function to take an additional argument that specifies the maximum number of attempts to generate a unique filename. Here's an example implementation:
async fn generate_unique_filename(
config: Config,
basename: String,
max_attempts: u32,
) -> Result<String, Error> {
let mut attempts = 0;
let mut filename = engine::generate_filename(config.clone(), basename).await?;
while engine::filename_exists(config.clone(), &filename).await? {
attempts += 1;
if attempts >= max_attempts {
return Err(Error::new(
ErrorKind::Other,
"Maximum number of attempts reached while generating a unique filename",
));
}
filename = engine::generate_filename(config.clone(), basename).await?;
}
Ok(filename)
}
I could also rewrite the function to not use the config directly, and have it's variables passed through like max_attempts.