Add Result type with custom Error
This commit is contained in:
@@ -43,6 +43,8 @@ impl fmt::Display for ErrorLocation {
|
||||
}
|
||||
}
|
||||
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, thiserror::Error)]
|
||||
pub struct Error {
|
||||
#[source]
|
||||
@@ -247,9 +249,9 @@ pub trait Contextualize<R> {
|
||||
fn err_context(self, context: impl Into<String>) -> R;
|
||||
}
|
||||
|
||||
impl<T, E: Into<Error>> Contextualize<Result<T, Error>> for Result<T, E> {
|
||||
impl<T, E: Into<Error>> Contextualize<Result<T>> for std::result::Result<T, E> {
|
||||
#[track_caller]
|
||||
fn err_context(self, context: impl Into<String>) -> Result<T, Error> {
|
||||
fn err_context(self, context: impl Into<String>) -> Result<T> {
|
||||
// Closures can't (currently) `track_caller`, so a simple map_err doesn't work
|
||||
// See https://github.com/rust-lang/rust/issues/87417
|
||||
match self {
|
||||
@@ -259,16 +261,16 @@ impl<T, E: Into<Error>> Contextualize<Result<T, Error>> for Result<T, E> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Contextualize<Result<T, Error>> for Option<T> {
|
||||
impl<T> Contextualize<Result<T>> for Option<T> {
|
||||
#[track_caller]
|
||||
fn err_context(self, context: impl Into<String>) -> Result<T, Error> {
|
||||
fn err_context(self, context: impl Into<String>) -> Result<T> {
|
||||
self.ok_or(Error::new_here(ErrorType::Error(context.into())))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, E: Into<Error>> Contextualize<Result<T, Error>> for E {
|
||||
impl<T, E: Into<Error>> Contextualize<Result<T>> for E {
|
||||
#[track_caller]
|
||||
fn err_context(self, context: impl Into<String>) -> Result<T, Error> {
|
||||
fn err_context(self, context: impl Into<String>) -> Result<T> {
|
||||
Err(self.into().with_context(context))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user