evobench_tools/io_utils/
div.rs

1use std::{
2    fs::{create_dir, rename},
3    path::Path,
4};
5
6use anyhow::{Context, Result, anyhow};
7
8pub fn xrename(from: &Path, to: &Path) -> Result<()> {
9    rename(from, to).with_context(|| anyhow!("renaming {from:?} to {to:?}"))?;
10    Ok(())
11}
12
13/// Returns true if the directory was created, false if already
14/// existed. `ctx` should be something like "queues base directory",
15/// made part of the error message.
16pub fn create_dir_if_not_exists<P: AsRef<Path>>(path: P, ctx: &str) -> Result<bool> {
17    let path = path.as_ref();
18    match create_dir(path) {
19        Ok(()) => Ok(true),
20        Err(e) => match e.kind() {
21            std::io::ErrorKind::AlreadyExists => Ok(false),
22            _ => Err(e).with_context(|| anyhow!("creating {ctx} {path:?}")),
23        },
24    }
25}