1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
use crate::{Error, Result};
use std::fs;
use std::path::Path;

pub fn ensure_dir(dir: impl AsRef<Path>) -> Result<bool> {
	let dir = dir.as_ref();
	if dir.is_dir() {
		Ok(false)
	} else {
		fs::create_dir_all(dir).map_err(|e| Error::DirCantCreateAll((dir, e).into()))?;
		Ok(true)
	}
}

pub fn ensure_file_dir(file_path: impl AsRef<Path>) -> Result<bool> {
	let file_path = file_path.as_ref();
	let dir = file_path
		.parent()
		.ok_or_else(|| Error::FileHasNoParent(file_path.to_string_lossy().to_string()))?;

	if dir.is_dir() {
		Ok(false)
	} else {
		fs::create_dir_all(dir).map_err(|e| Error::DirCantCreateAll((dir, e).into()))?;
		Ok(true)
	}
}