| 
 | 1 | +use std::fs;  | 
 | 2 | +use std::path::Path;  | 
 | 3 | + | 
 | 4 | +/// A safe wrapper around `std::fs::remove_file` that prints the file path if an operation fails.  | 
 | 5 | +pub fn remove_file<P: AsRef<Path> + std::fmt::Debug>(path: P) {  | 
 | 6 | +    fs::remove_file(path.as_ref())  | 
 | 7 | +        .expect(&format!("The file in path \"{:?}\" could not be removed.", path.as_ref()));  | 
 | 8 | +}  | 
 | 9 | + | 
 | 10 | +/// A safe wrapper around `std::fs::copy` that prints the file paths if an operation fails.  | 
 | 11 | +pub fn copy<P: AsRef<Path> + std::fmt::Debug, Q: AsRef<Path> + std::fmt::Debug>(from: P, to: Q) {  | 
 | 12 | +    fs::copy(from.as_ref(), to.as_ref()).expect(&format!(  | 
 | 13 | +        "The file \"{:?}\" could not be copied over to \"{:?}\".",  | 
 | 14 | +        from.as_ref(),  | 
 | 15 | +        to.as_ref(),  | 
 | 16 | +    ));  | 
 | 17 | +}  | 
 | 18 | + | 
 | 19 | +/// A safe wrapper around `std::fs::File::create` that prints the file path if an operation fails.  | 
 | 20 | +pub fn create_file<P: AsRef<Path> + std::fmt::Debug>(path: P) {  | 
 | 21 | +    fs::File::create(path.as_ref())  | 
 | 22 | +        .expect(&format!("The file in path \"{:?}\" could not be created.", path.as_ref()));  | 
 | 23 | +}  | 
 | 24 | + | 
 | 25 | +/// A safe wrapper around `std::fs::read` that prints the file path if an operation fails.  | 
 | 26 | +pub fn read<P: AsRef<Path> + std::fmt::Debug>(path: P) -> Vec<u8> {  | 
 | 27 | +    fs::read(path.as_ref())  | 
 | 28 | +        .expect(&format!("The file in path \"{:?}\" could not be read.", path.as_ref()))  | 
 | 29 | +}  | 
 | 30 | + | 
 | 31 | +/// A safe wrapper around `std::fs::read_to_string` that prints the file path if an operation fails.  | 
 | 32 | +pub fn read_to_string<P: AsRef<Path> + std::fmt::Debug>(path: P) -> String {  | 
 | 33 | +    fs::read_to_string(path.as_ref()).expect(&format!(  | 
 | 34 | +        "The file in path \"{:?}\" could not be read into a String.",  | 
 | 35 | +        path.as_ref()  | 
 | 36 | +    ))  | 
 | 37 | +}  | 
 | 38 | + | 
 | 39 | +/// A safe wrapper around `std::fs::read_dir` that prints the file path if an operation fails.  | 
 | 40 | +pub fn read_dir<P: AsRef<Path> + std::fmt::Debug>(path: P) -> fs::ReadDir {  | 
 | 41 | +    fs::read_dir(path.as_ref())  | 
 | 42 | +        .expect(&format!("The directory in path \"{:?}\" could not be read.", path.as_ref()))  | 
 | 43 | +}  | 
 | 44 | + | 
 | 45 | +/// A safe wrapper around `std::fs::write` that prints the file path if an operation fails.  | 
 | 46 | +pub fn write<P: AsRef<Path> + std::fmt::Debug, C: AsRef<[u8]>>(path: P, contents: C) {  | 
 | 47 | +    fs::read(path.as_ref(), contents.as_ref())  | 
 | 48 | +        .expect(&format!("The file in path \"{:?}\" could not be written to.", path.as_ref()));  | 
 | 49 | +}  | 
 | 50 | + | 
 | 51 | +/// A safe wrapper around `std::fs::remove_dir_all` that prints the file path if an operation fails.  | 
 | 52 | +pub fn remove_dir_all<P: AsRef<Path> + std::fmt::Debug>(path: P) {  | 
 | 53 | +    fs::remove_dir_all(path.as_ref()).expect(&format!(  | 
 | 54 | +        "The directory in path \"{:?}\" could not be removed alongside all its contents.",  | 
 | 55 | +        path.as_ref(),  | 
 | 56 | +    ));  | 
 | 57 | +}  | 
 | 58 | + | 
 | 59 | +/// A safe wrapper around `std::fs::create_dir` that prints the file path if an operation fails.  | 
 | 60 | +pub fn create_dir<P: AsRef<Path> + std::fmt::Debug>(path: P) {  | 
 | 61 | +    fs::create_dir(path.as_ref())  | 
 | 62 | +        .expect(&format!("The directory in path \"{:?}\" could not be created.", path.as_ref()));  | 
 | 63 | +}  | 
 | 64 | + | 
 | 65 | +/// A safe wrapper around `std::fs::create_dir_all` that prints the file path if an operation fails.  | 
 | 66 | +pub fn create_dir_all<P: AsRef<Path> + std::fmt::Debug>(path: P) {  | 
 | 67 | +    fs::create_dir_all(path.as_ref()).expect(&format!(  | 
 | 68 | +        "The directory (and all its parents) in path \"{:?}\" could not be created.",  | 
 | 69 | +        path.as_ref()  | 
 | 70 | +    ));  | 
 | 71 | +}  | 
 | 72 | + | 
 | 73 | +/// A safe wrapper around `std::fs::metadata` that prints the file path if an operation fails.  | 
 | 74 | +pub fn metadata<P: AsRef<Path> + std::fmt::Debug>(path: P) -> fs::Metadata {  | 
 | 75 | +    fs::metadata(path.as_ref())  | 
 | 76 | +        .expect(&format!("The file's metadata in path \"{:?}\" could not be read.", path.as_ref()))  | 
 | 77 | +}  | 
 | 78 | + | 
 | 79 | +/// A safe wrapper around `std::fs::rename` that prints the file paths if an operation fails.  | 
 | 80 | +pub fn rename<P: AsRef<Path> + std::fmt::Debug, Q: AsRef<Path> + std::fmt::Debug>(from: P, to: Q) {  | 
 | 81 | +    fs::rename(from.as_ref(), to.as_ref()).expect(&format!(  | 
 | 82 | +        "The file \"{:?}\" could not be moved over to \"{:?}\".",  | 
 | 83 | +        from.as_ref(),  | 
 | 84 | +        to.as_ref(),  | 
 | 85 | +    ));  | 
 | 86 | +}  | 
0 commit comments