-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added file_utils to print not-found path in error (#349)
- Loading branch information
Showing
5 changed files
with
46 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use std::fs::File; | ||
use std::io; | ||
use std::path::{Path, PathBuf}; | ||
|
||
/// An extension error to the io:Error, additionally specifying the not-found path. | ||
#[derive(std::fmt::Debug)] | ||
pub struct IoErrorWithPath { | ||
source: io::Error, | ||
path: PathBuf, | ||
} | ||
impl std::error::Error for IoErrorWithPath { | ||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { | ||
Some(&self.source) | ||
} | ||
} | ||
impl std::fmt::Display for IoErrorWithPath { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
writeln!(f, "{}: {}", self.source, self.path.display()) | ||
} | ||
} | ||
|
||
/// A wrapper to `File::open`, which, in case of failure, also logs the not-found path. | ||
pub fn open_file(path: &Path) -> Result<File, IoErrorWithPath> { | ||
File::open(path).map_err(|e| IoErrorWithPath { | ||
source: e, | ||
path: path.to_path_buf(), | ||
}) | ||
} | ||
|
||
/// A wrapper to `std::fs::read_to_string`, which, in case of failure, also logs the not-found path. | ||
pub fn read_to_string(path: &Path) -> Result<String, IoErrorWithPath> { | ||
std::fs::read_to_string(path).map_err(|e| IoErrorWithPath { | ||
source: e, | ||
path: path.to_path_buf(), | ||
}) | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod binary_utils; | ||
pub mod file_utils; | ||
pub mod logging_utils; | ||
pub mod vm_utils; |