Skip to content

Commit

Permalink
feat(into_diagnostic): .into_diagnostic() is now generic across any i…
Browse files Browse the repository at this point in the history
…mpl fmt::Display instead of expecting a `dyn`
  • Loading branch information
zkat committed Aug 22, 2021
1 parent 5c077d3 commit c1da4a0
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,17 @@ impl Diagnostic for DiagnosticError {
}
}

/**
Convenience trait that adds a `.into_diagnostic()` method that converts a type to a `Result<T, DiagnosticError>`.
*/
pub trait IntoDiagnostic<T, E> {
/// Converts [Result]-like types that return regular errors into a
/// `Result` that returns a [Diagnostic].
fn into_diagnostic(self, code: &(dyn fmt::Display)) -> Result<T, DiagnosticError>;
fn into_diagnostic(self, code: impl fmt::Display) -> Result<T, DiagnosticError>;
}

impl<T, E: std::error::Error + Send + Sync + 'static> IntoDiagnostic<T, E> for Result<T, E> {
fn into_diagnostic(self, code: &(dyn fmt::Display)) -> Result<T, DiagnosticError> {
fn into_diagnostic(self, code: impl fmt::Display) -> Result<T, DiagnosticError> {
self.map_err(|e| DiagnosticError {
error: Box::new(e),
code: format!("{}", code),
Expand Down

0 comments on commit c1da4a0

Please sign in to comment.