| 1 |
//! # directory |
| 2 |
//! |
| 3 |
//! Directory utility functions. |
| 4 |
//! |
| 5 |
|
| 6 |
#[cfg(test)] |
| 7 |
#[path = "./directory_test.rs"] |
| 8 |
mod directory_test; |
| 9 |
|
| 10 |
use crate::error::FsIOError; |
| 11 |
use crate::path::as_path::AsPath; |
| 12 |
use crate::path::get_parent_directory; |
| 13 |
use crate::types::FsIOResult; |
| 14 |
use std::fs::{create_dir_all, remove_dir_all}; |
| 15 |
|
| 16 |
/// Creates the directory (and if needed the parent directories) for the provided path. |
| 17 |
/// |
| 18 |
/// # Arguments |
| 19 |
/// |
| 20 |
/// * `path` - The directory path |
| 21 |
/// |
| 22 |
/// # Example |
| 23 |
/// |
| 24 |
/// ``` |
| 25 |
/// use crate::fsio::directory; |
| 26 |
/// use std::path::Path; |
| 27 |
/// |
| 28 |
/// fn main() { |
| 29 |
/// let result = directory::create("./target/__test/directory_test/dir1/dir2"); |
| 30 |
/// assert!(result.is_ok()); |
| 31 |
/// |
| 32 |
/// let path = Path::new("./target/__test/directory_test/dir1/dir2"); |
| 33 |
/// assert!(path.exists()); |
| 34 |
/// } |
| 35 |
/// ``` |
| 36 |
pub fn create<T: AsPath + ?Sized>(path: &T) -> FsIOResult<()> { |
| 37 |
let directory_path = path.as_path(); |
| 38 |
|
| 39 |
if directory_path.is_dir() && directory_path.exists() { |
| 40 |
return Ok(()); |
| 41 |
} |
| 42 |
|
| 43 |
match create_dir_all(&directory_path) { |
| 44 |
Ok(_) => Ok(()), |
| 45 |
Err(error) => Err(FsIOError::IOError( |
| 46 |
format!("Unable to create directory: {:?}.", &directory_path).to_string(), |
| 47 |
Some(error), |
| 48 |
)), |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
/// Creates the parent directory (and if needed the parent directories) for the provided path. |
| 53 |
/// In case no parent directory path component exists, this function will return ok result. |
| 54 |
/// |
| 55 |
/// # Arguments |
| 56 |
/// |
| 57 |
/// * `path` - The child path |
| 58 |
/// |
| 59 |
/// # Example |
| 60 |
/// |
| 61 |
/// ``` |
| 62 |
/// use crate::fsio::directory; |
| 63 |
/// use std::path::Path; |
| 64 |
/// |
| 65 |
/// fn main() { |
| 66 |
/// let result = directory::create_parent("./target/__test/directory_test/dir1/files/file.txt"); |
| 67 |
/// assert!(result.is_ok()); |
| 68 |
/// |
| 69 |
/// let path = Path::new("./target/__test/directory_test/dir1/files"); |
| 70 |
/// assert!(path.exists()); |
| 71 |
/// } |
| 72 |
/// ``` |
| 73 |
pub fn create_parent<T: AsPath + ?Sized>(path: &T) -> FsIOResult<()> { |
| 74 |
match get_parent_directory(path) { |
| 75 |
Some(directory) => create(&directory), |
| 76 |
None => Ok(()), |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
/// Deletes the directory and any child file/directory. |
| 81 |
/// |
| 82 |
/// # Arguments |
| 83 |
/// |
| 84 |
/// * `path` - The directory path |
| 85 |
/// |
| 86 |
/// # Example |
| 87 |
/// |
| 88 |
/// ``` |
| 89 |
/// use crate::fsio::{directory, file}; |
| 90 |
/// use std::path::Path; |
| 91 |
/// |
| 92 |
/// fn main() { |
| 93 |
/// file::ensure_exists("./target/__test/directory_test/delete_directory/dir1/dir2/file.txt").unwrap(); |
| 94 |
/// let path = Path::new("./target/__test/directory_test/delete_directory"); |
| 95 |
/// assert!(path.exists()); |
| 96 |
/// |
| 97 |
/// let result = directory::delete("./target/__test/directory_test/delete_directory"); |
| 98 |
/// assert!(result.is_ok()); |
| 99 |
/// |
| 100 |
/// assert!(!path.exists()); |
| 101 |
/// } |
| 102 |
/// ``` |
| 103 |
pub fn delete<T: AsPath + ?Sized>(path: &T) -> FsIOResult<()> { |
| 104 |
let directory_path = path.as_path(); |
| 105 |
|
| 106 |
if directory_path.exists() { |
| 107 |
if directory_path.is_dir() { |
| 108 |
match remove_dir_all(directory_path) { |
| 109 |
Ok(_) => Ok(()), |
| 110 |
Err(error) => Err(FsIOError::IOError( |
| 111 |
format!("Unable to delete directory: {:?}", &directory_path).to_string(), |
| 112 |
Some(error), |
| 113 |
)), |
| 114 |
} |
| 115 |
} else { |
| 116 |
Err(FsIOError::NotFile( |
| 117 |
format!("Path: {:?} is not a directory.", &directory_path).to_string(), |
| 118 |
)) |
| 119 |
} |
| 120 |
} else { |
| 121 |
Ok(()) |
| 122 |
} |
| 123 |
} |