summaryrefslogtreecommitdiff
path: root/vendor/miette/src/eyreish/into_diagnostic.rs
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2024-01-08 00:21:28 +0300
committerValentin Popov <valentin@popov.link>2024-01-08 00:21:28 +0300
commit1b6a04ca5504955c571d1c97504fb45ea0befee4 (patch)
tree7579f518b23313e8a9748a88ab6173d5e030b227 /vendor/miette/src/eyreish/into_diagnostic.rs
parent5ecd8cf2cba827454317368b68571df0d13d7842 (diff)
downloadfparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.tar.xz
fparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.zip
Initial vendor packages
Signed-off-by: Valentin Popov <valentin@popov.link>
Diffstat (limited to 'vendor/miette/src/eyreish/into_diagnostic.rs')
-rw-r--r--vendor/miette/src/eyreish/into_diagnostic.rs33
1 files changed, 33 insertions, 0 deletions
diff --git a/vendor/miette/src/eyreish/into_diagnostic.rs b/vendor/miette/src/eyreish/into_diagnostic.rs
new file mode 100644
index 0000000..6480013
--- /dev/null
+++ b/vendor/miette/src/eyreish/into_diagnostic.rs
@@ -0,0 +1,33 @@
+use thiserror::Error;
+
+use crate::{Diagnostic, Report};
+
+/// Convenience [`Diagnostic`] that can be used as an "anonymous" wrapper for
+/// Errors. This is intended to be paired with [`IntoDiagnostic`].
+#[derive(Debug, Error)]
+#[error(transparent)]
+struct DiagnosticError(Box<dyn std::error::Error + Send + Sync + 'static>);
+impl Diagnostic for DiagnosticError {}
+
+/**
+Convenience trait that adds a [`.into_diagnostic()`](IntoDiagnostic::into_diagnostic) method that converts a type implementing
+[`std::error::Error`] to a [`Result<T, Report>`].
+
+## Warning
+
+Calling this on a type implementing [`Diagnostic`] will reduce it to the common denominator of
+[`std::error::Error`]. Meaning all extra information provided by [`Diagnostic`] will be
+inaccessible. If you have a type implementing [`Diagnostic`] consider simply returning it or using
+[`Into`] or the [`Try`](std::ops::Try) operator (`?`).
+*/
+pub trait IntoDiagnostic<T, E> {
+ /// Converts [`Result`] types that return regular [`std::error::Error`]s
+ /// into a [`Result`] that returns a [`Diagnostic`].
+ fn into_diagnostic(self) -> Result<T, Report>;
+}
+
+impl<T, E: std::error::Error + Send + Sync + 'static> IntoDiagnostic<T, E> for Result<T, E> {
+ fn into_diagnostic(self) -> Result<T, Report> {
+ self.map_err(|e| DiagnosticError(Box::new(e)).into())
+ }
+}