|
| 1 | +use std::fs; |
| 2 | +use std::path::Path; |
| 3 | + |
| 4 | +/// A wrapper around [`std::fs::remove_file`] which includes the file path in the panic message.. |
| 5 | +#[track_caller] |
| 6 | +pub fn remove_file<P: AsRef<Path>>(path: P) { |
| 7 | + fs::remove_file(path.as_ref()) |
| 8 | + .expect(&format!("the file in path \"{}\" could not be removed", path.as_ref().display())); |
| 9 | +} |
| 10 | + |
| 11 | +/// A wrapper around [`std::fs::copy`] which includes the file path in the panic message. |
| 12 | +#[track_caller] |
| 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 wrapper around [`std::fs::File::create`] which includes the file path in the panic message.. |
| 22 | +#[track_caller] |
| 23 | +pub fn create_file<P: AsRef<Path>>(path: P) { |
| 24 | + fs::File::create(path.as_ref()) |
| 25 | + .expect(&format!("the file in path \"{}\" could not be created", path.as_ref().display())); |
| 26 | +} |
| 27 | + |
| 28 | +/// A wrapper around [`std::fs::read`] which includes the file path in the panic message.. |
| 29 | +#[track_caller] |
| 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 wrapper around [`std::fs::read_to_string`] which includes the file path in the panic message.. |
| 36 | +#[track_caller] |
| 37 | +pub fn read_to_string<P: AsRef<Path>>(path: P) -> String { |
| 38 | + fs::read_to_string(path.as_ref()).expect(&format!( |
| 39 | + "the file in path \"{}\" could not be read into a String", |
| 40 | + path.as_ref().display() |
| 41 | + )) |
| 42 | +} |
| 43 | + |
| 44 | +/// A wrapper around [`std::fs::read_dir`] which includes the file path in the panic message.. |
| 45 | +#[track_caller] |
| 46 | +pub fn read_dir<P: AsRef<Path>>(path: P) -> fs::ReadDir { |
| 47 | + fs::read_dir(path.as_ref()) |
| 48 | + .expect(&format!("the directory in path \"{}\" could not be read", path.as_ref().display())) |
| 49 | +} |
| 50 | + |
| 51 | +/// A wrapper around [`std::fs::write`] which includes the file path in the panic message.. |
| 52 | +#[track_caller] |
| 53 | +pub fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) { |
| 54 | + fs::write(path.as_ref(), contents.as_ref()).expect(&format!( |
| 55 | + "the file in path \"{}\" could not be written to", |
| 56 | + path.as_ref().display() |
| 57 | + )); |
| 58 | +} |
| 59 | + |
| 60 | +/// A wrapper around [`std::fs::remove_dir_all`] which includes the file path in the panic message.. |
| 61 | +#[track_caller] |
| 62 | +pub fn remove_dir_all<P: AsRef<Path>>(path: P) { |
| 63 | + fs::remove_dir_all(path.as_ref()).expect(&format!( |
| 64 | + "the directory in path \"{}\" could not be removed alongside all its contents", |
| 65 | + path.as_ref().display(), |
| 66 | + )); |
| 67 | +} |
| 68 | + |
| 69 | +/// A wrapper around [`std::fs::create_dir`] which includes the file path in the panic message.. |
| 70 | +#[track_caller] |
| 71 | +pub fn create_dir<P: AsRef<Path>>(path: P) { |
| 72 | + fs::create_dir(path.as_ref()).expect(&format!( |
| 73 | + "the directory in path \"{}\" could not be created", |
| 74 | + path.as_ref().display() |
| 75 | + )); |
| 76 | +} |
| 77 | + |
| 78 | +/// A wrapper around [`std::fs::create_dir_all`] which includes the file path in the panic message.. |
| 79 | +#[track_caller] |
| 80 | +pub fn create_dir_all<P: AsRef<Path>>(path: P) { |
| 81 | + fs::create_dir_all(path.as_ref()).expect(&format!( |
| 82 | + "the directory (and all its parents) in path \"{}\" could not be created", |
| 83 | + path.as_ref().display() |
| 84 | + )); |
| 85 | +} |
| 86 | + |
| 87 | +/// A wrapper around [`std::fs::metadata`] which includes the file path in the panic message.. |
| 88 | +#[track_caller] |
| 89 | +pub fn metadata<P: AsRef<Path>>(path: P) -> fs::Metadata { |
| 90 | + fs::metadata(path.as_ref()).expect(&format!( |
| 91 | + "the file's metadata in path \"{}\" could not be read", |
| 92 | + path.as_ref().display() |
| 93 | + )) |
| 94 | +} |
| 95 | + |
| 96 | +/// A wrapper around [`std::fs::rename`] which includes the file path in the panic message. |
| 97 | +#[track_caller] |
| 98 | +pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) { |
| 99 | + fs::rename(from.as_ref(), to.as_ref()).expect(&format!( |
| 100 | + "the file \"{}\" could not be moved over to \"{}\"", |
| 101 | + from.as_ref().display(), |
| 102 | + to.as_ref().display(), |
| 103 | + )); |
| 104 | +} |
| 105 | + |
| 106 | +/// A wrapper around [`std::fs::set_permissions`] which includes the file path in the panic message. |
| 107 | +#[track_caller] |
| 108 | +pub fn set_permissions<P: AsRef<Path>>(path: P, perm: fs::Permissions) { |
| 109 | + fs::set_permissions(path.as_ref(), perm).expect(&format!( |
| 110 | + "the file's permissions in path \"{}\" could not be changed", |
| 111 | + path.as_ref().display() |
| 112 | + )); |
| 113 | +} |
0 commit comments