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