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