-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add safe wrappers around fs in run_make_support
- Loading branch information
Showing
28 changed files
with
141 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
use std::fs; | ||
use std::path::Path; | ||
|
||
/// A safe wrapper around `std::fs::remove_file` that prints the file path if an operation fails. | ||
pub fn remove_file<P: AsRef<Path> + std::fmt::Debug>(path: P) { | ||
fs::remove_file(path.as_ref()) | ||
.expect(&format!("The file in path \"{:?}\" could not be removed.", path.as_ref())); | ||
} | ||
|
||
/// A safe wrapper around `std::fs::copy` that prints the file paths if an operation fails. | ||
pub fn copy<P: AsRef<Path> + std::fmt::Debug, Q: AsRef<Path> + std::fmt::Debug>(from: P, to: Q) { | ||
fs::copy(from.as_ref(), to.as_ref()).expect(&format!( | ||
"The file \"{:?}\" could not be copied over to \"{:?}\".", | ||
from.as_ref(), | ||
to.as_ref(), | ||
)); | ||
} | ||
|
||
/// A safe wrapper around `std::fs::File::create` that prints the file path if an operation fails. | ||
pub fn create_file<P: AsRef<Path> + std::fmt::Debug>(path: P) { | ||
fs::File::create(path.as_ref()) | ||
.expect(&format!("The file in path \"{:?}\" could not be created.", path.as_ref())); | ||
} | ||
|
||
/// A safe wrapper around `std::fs::read` that prints the file path if an operation fails. | ||
pub fn read<P: AsRef<Path> + std::fmt::Debug>(path: P) -> Vec<u8> { | ||
fs::read(path.as_ref()) | ||
.expect(&format!("The file in path \"{:?}\" could not be read.", path.as_ref())) | ||
} | ||
|
||
/// A safe wrapper around `std::fs::read_to_string` that prints the file path if an operation fails. | ||
pub fn read_to_string<P: AsRef<Path> + std::fmt::Debug>(path: P) -> String { | ||
fs::read_to_string(path.as_ref()).expect(&format!( | ||
"The file in path \"{:?}\" could not be read into a String.", | ||
path.as_ref() | ||
)) | ||
} | ||
|
||
/// A safe wrapper around `std::fs::read_dir` that prints the file path if an operation fails. | ||
pub fn read_dir<P: AsRef<Path> + std::fmt::Debug>(path: P) -> fs::ReadDir { | ||
fs::read_dir(path.as_ref()) | ||
.expect(&format!("The directory in path \"{:?}\" could not be read.", path.as_ref())) | ||
} | ||
|
||
/// A safe wrapper around `std::fs::write` that prints the file path if an operation fails. | ||
pub fn write<P: AsRef<Path> + std::fmt::Debug, C: AsRef<[u8]>>(path: P, contents: C) { | ||
fs::read(path.as_ref(), contents.as_ref()) | ||
.expect(&format!("The file in path \"{:?}\" could not be written to.", path.as_ref())); | ||
} | ||
|
||
/// A safe wrapper around `std::fs::remove_dir_all` that prints the file path if an operation fails. | ||
pub fn remove_dir_all<P: AsRef<Path> + std::fmt::Debug>(path: P) { | ||
fs::remove_dir_all(path.as_ref()).expect(&format!( | ||
"The directory in path \"{:?}\" could not be removed alongside all its contents.", | ||
path.as_ref(), | ||
)); | ||
} | ||
|
||
/// A safe wrapper around `std::fs::create_dir` that prints the file path if an operation fails. | ||
pub fn create_dir<P: AsRef<Path> + std::fmt::Debug>(path: P) { | ||
fs::create_dir(path.as_ref()) | ||
.expect(&format!("The directory in path \"{:?}\" could not be created.", path.as_ref())); | ||
} | ||
|
||
/// A safe wrapper around `std::fs::create_dir_all` that prints the file path if an operation fails. | ||
pub fn create_dir_all<P: AsRef<Path> + std::fmt::Debug>(path: P) { | ||
fs::create_dir_all(path.as_ref()).expect(&format!( | ||
"The directory (and all its parents) in path \"{:?}\" could not be created.", | ||
path.as_ref() | ||
)); | ||
} | ||
|
||
/// A safe wrapper around `std::fs::metadata` that prints the file path if an operation fails. | ||
pub fn metadata<P: AsRef<Path> + std::fmt::Debug>(path: P) -> fs::Metadata { | ||
fs::metadata(path.as_ref()) | ||
.expect(&format!("The file's metadata in path \"{:?}\" could not be read.", path.as_ref())) | ||
} | ||
|
||
/// A safe wrapper around `std::fs::rename` that prints the file paths if an operation fails. | ||
pub fn rename<P: AsRef<Path> + std::fmt::Debug, Q: AsRef<Path> + std::fmt::Debug>(from: P, to: Q) { | ||
fs::rename(from.as_ref(), to.as_ref()).expect(&format!( | ||
"The file \"{:?}\" could not be moved over to \"{:?}\".", | ||
from.as_ref(), | ||
to.as_ref(), | ||
)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.