simple_fs/
dir.rs

1use crate::{Error, Result};
2use std::fs;
3use std::path::Path;
4
5pub fn ensure_dir(dir: impl AsRef<Path>) -> Result<bool> {
6	let dir = dir.as_ref();
7	if dir.is_dir() {
8		Ok(false)
9	} else {
10		fs::create_dir_all(dir).map_err(|e| Error::DirCantCreateAll((dir, e).into()))?;
11		Ok(true)
12	}
13}
14
15pub fn ensure_file_dir(file_path: impl AsRef<Path>) -> Result<bool> {
16	let file_path = file_path.as_ref();
17	let dir = file_path
18		.parent()
19		.ok_or_else(|| Error::FileHasNoParent(file_path.to_string_lossy().to_string()))?;
20
21	if dir.is_dir() {
22		Ok(false)
23	} else {
24		fs::create_dir_all(dir).map_err(|e| Error::DirCantCreateAll((dir, e).into()))?;
25		Ok(true)
26	}
27}