Improved error handling
This commit is contained in:
@@ -6,6 +6,10 @@ pub mod raw;
|
||||
pub type Output = Box<dyn Write>;
|
||||
pub type Input = Box<dyn Read>;
|
||||
|
||||
pub trait ErrorString {
|
||||
fn to_string(self) -> String;
|
||||
}
|
||||
|
||||
pub struct Error {
|
||||
pub exit_code: i32,
|
||||
pub action: String,
|
||||
@@ -27,6 +31,10 @@ impl Error {
|
||||
Error::from_text(error.to_string())
|
||||
}
|
||||
|
||||
pub fn from_core_error<T>(error: T) -> Error where T: core::fmt::Display {
|
||||
Error::from_text(error.to_string())
|
||||
}
|
||||
|
||||
pub fn from_callback<F>(callback: F) -> Error where F: Fn() -> String {
|
||||
Error::from_text(callback())
|
||||
}
|
||||
@@ -86,6 +94,12 @@ impl std::convert::From<std::convert::Infallible> for Error {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ErrorString> std::convert::From<T> for Error {
|
||||
fn from(error: T) -> Self {
|
||||
Error::from_text(error.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn open_output_file(output_path: PathBuf) -> Result<BufWriter<std::fs::File>, Error> {
|
||||
Ok(std::io::BufWriter::new(std::fs::File::create(output_path)?))
|
||||
}
|
||||
|
Reference in New Issue
Block a user