src/create_parent_directory.rs
5/// Create the parent directory of a given path.
9/// create_parent_directory("path/to/images/index.html")
10/// ~> creates "path/to/images/"
12pub fn create_parent_directory(path: &PathBuf) -> io::Result<()> {
13 // Quoting from the Rust docs for PathBuf.parent() [1]:
15 // Returns None if the path terminates in a root or prefix,
16 // or if it’s the empty string.
18 // This function should only ever be called on paths to files, so
19 // .parent() will never return None.
21 // [1]: https://doc.rust-lang.org/std/path/struct.PathBuf.html#method.parent
22 let parent_dir = path.parent().unwrap();
24 fs::create_dir_all(&parent_dir)
28mod test_create_parent_directory {
30 use crate::test_utils::test_dir;
33 fn it_creates_a_directory() {
37 let path = t.join("path/to/images").join("index.html");
38 assert!(create_parent_directory(&path).is_ok());
41 assert!(t.join("path/to/images").exists());
42 assert!(!path.exists());