-
Notifications
You must be signed in to change notification settings - Fork 13.4k
run-make-support: add wrapper for fs
operations
#125736
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,113 @@ | ||
use std::fs; | ||
use std::path::Path; | ||
|
||
/// A wrapper around [`std::fs::remove_file`] which includes the file path in the panic message.. | ||
#[track_caller] | ||
pub fn remove_file<P: AsRef<Path>>(path: P) { | ||
fs::remove_file(path.as_ref()) | ||
.expect(&format!("the file in path \"{}\" could not be removed", path.as_ref().display())); | ||
} | ||
|
||
/// A wrapper around [`std::fs::copy`] which includes the file path in the panic message. | ||
#[track_caller] | ||
pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(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().display(), | ||
to.as_ref().display(), | ||
)); | ||
} | ||
|
||
/// A wrapper around [`std::fs::File::create`] which includes the file path in the panic message.. | ||
#[track_caller] | ||
pub fn create_file<P: AsRef<Path>>(path: P) { | ||
fs::File::create(path.as_ref()) | ||
.expect(&format!("the file in path \"{}\" could not be created", path.as_ref().display())); | ||
} | ||
|
||
/// A wrapper around [`std::fs::read`] which includes the file path in the panic message.. | ||
#[track_caller] | ||
pub fn read<P: AsRef<Path>>(path: P) -> Vec<u8> { | ||
fs::read(path.as_ref()) | ||
.expect(&format!("the file in path \"{}\" could not be read", path.as_ref().display())) | ||
} | ||
|
||
/// A wrapper around [`std::fs::read_to_string`] which includes the file path in the panic message.. | ||
#[track_caller] | ||
pub fn read_to_string<P: AsRef<Path>>(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().display() | ||
)) | ||
} | ||
|
||
/// A wrapper around [`std::fs::read_dir`] which includes the file path in the panic message.. | ||
#[track_caller] | ||
pub fn read_dir<P: AsRef<Path>>(path: P) -> fs::ReadDir { | ||
fs::read_dir(path.as_ref()) | ||
.expect(&format!("the directory in path \"{}\" could not be read", path.as_ref().display())) | ||
} | ||
|
||
/// A wrapper around [`std::fs::write`] which includes the file path in the panic message.. | ||
#[track_caller] | ||
pub fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) { | ||
fs::write(path.as_ref(), contents.as_ref()).expect(&format!( | ||
"the file in path \"{}\" could not be written to", | ||
path.as_ref().display() | ||
)); | ||
} | ||
|
||
/// A wrapper around [`std::fs::remove_dir_all`] which includes the file path in the panic message.. | ||
#[track_caller] | ||
pub fn remove_dir_all<P: AsRef<Path>>(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().display(), | ||
)); | ||
} | ||
|
||
/// A wrapper around [`std::fs::create_dir`] which includes the file path in the panic message.. | ||
#[track_caller] | ||
pub fn create_dir<P: AsRef<Path>>(path: P) { | ||
fs::create_dir(path.as_ref()).expect(&format!( | ||
"the directory in path \"{}\" could not be created", | ||
path.as_ref().display() | ||
)); | ||
} | ||
|
||
/// A wrapper around [`std::fs::create_dir_all`] which includes the file path in the panic message.. | ||
#[track_caller] | ||
pub fn create_dir_all<P: AsRef<Path>>(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().display() | ||
)); | ||
} | ||
|
||
/// A wrapper around [`std::fs::metadata`] which includes the file path in the panic message.. | ||
#[track_caller] | ||
pub fn metadata<P: AsRef<Path>>(path: P) -> fs::Metadata { | ||
fs::metadata(path.as_ref()).expect(&format!( | ||
"the file's metadata in path \"{}\" could not be read", | ||
path.as_ref().display() | ||
)) | ||
} | ||
|
||
/// A wrapper around [`std::fs::rename`] which includes the file path in the panic message. | ||
#[track_caller] | ||
pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(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().display(), | ||
to.as_ref().display(), | ||
)); | ||
} | ||
|
||
/// A wrapper around [`std::fs::set_permissions`] which includes the file path in the panic message. | ||
#[track_caller] | ||
pub fn set_permissions<P: AsRef<Path>>(path: P, perm: fs::Permissions) { | ||
fs::set_permissions(path.as_ref(), perm).expect(&format!( | ||
"the file's permissions in path \"{}\" could not be changed", | ||
path.as_ref().display() | ||
)); | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.