evobench_tools/io_utils/
div.rs1use 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
13pub 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}