-
Couldn't load subscription status.
- Fork 7
treewide: refactor read_file and mkdir #6
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
base: main
Are you sure you want to change the base?
Conversation
c4227bc to
a37192c
Compare
|
For nicer error messages I propose to add https://crates.io/crates/anyhow to the project which is the canonical error handling library for Rust applications. It adds a bit to the final binary size but allows attaching context objects/information to the error cases and has better ergonomics for producing errors e.g.: pub(crate) fn read_file(filename: impl AsRef<Path>) -> Result<String> {
read_to_string(filename.as_ref())
.with_context(|| format!("Failed to read {}", filename.as_ref().to_string_lossy()))
}Gives in this simple error case (which can be extended easily): instead of: |
a37192c to
ed59bf5
Compare
I want to keep the binary size down and adding extra stuff for nicer error messages does not sound like a good idea. |
ed59bf5 to
d24c4e1
Compare
mkdir, read_file write_file augment the error case with the path in the error case. This is useful as we otherwise don't know which location failed. This commit refactors the functions to take any argument that coerces into a Path - which is implemented by many types. mkdir now uses create_dir_all as it is not meant to fail if the directory already exists. Signed-off-by: Stefan Kerkmann <[email protected]>
d24c4e1 to
6bb7ae5
Compare
|
So You're basically doing 3 things in this commit:
I've done some size measurements with this. I've used The size of the text section grows by 2.5k and the file size by 4k (I think there is some 4k alignment in there). A big chunk is So for now, please just factor out the stuff into a separate file and lets look at the other cleanup in a different PR and see if we can come up with something that costs less. |
mkdirandread_fileaugment the error case with the path in the error case. This is useful as we otherwise don't know which location failed. This commit refactors the functions to take any argument that coerces into a Path - which is implemented by many types.mkdirnow usescreate_dir_allas it is not meant to fail if the directory already exists.builds/depends on #5