miette/eyreish/
kind.rs

1#![allow(missing_debug_implementations, missing_docs)]
2// Tagged dispatch mechanism for resolving the behavior of `miette!($expr)`.
3//
4// When miette! is given a single expr argument to turn into miette::Report, we
5// want the resulting Report to pick up the input's implementation of source()
6// and backtrace() if it has a std::error::Error impl, otherwise require nothing
7// more than Display and Debug.
8//
9// Expressed in terms of specialization, we want something like:
10//
11//     trait EyreNew {
12//         fn new(self) -> Report;
13//     }
14//
15//     impl<T> EyreNew for T
16//     where
17//         T: Display + Debug + Send + Sync + 'static,
18//     {
19//         default fn new(self) -> Report {
20//             /* no std error impl */
21//         }
22//     }
23//
24//     impl<T> EyreNew for T
25//     where
26//         T: std::error::Error + Send + Sync + 'static,
27//     {
28//         fn new(self) -> Report {
29//             /* use std error's source() and backtrace() */
30//         }
31//     }
32//
33// Since specialization is not stable yet, instead we rely on autoref behavior
34// of method resolution to perform tagged dispatch. Here we have two traits
35// AdhocKind and TraitKind that both have an miette_kind() method. AdhocKind is
36// implemented whether or not the caller's type has a std error impl, while
37// TraitKind is implemented only when a std error impl does exist. The ambiguity
38// is resolved by AdhocKind requiring an extra autoref so that it has lower
39// precedence.
40//
41// The miette! macro will set up the call in this form:
42//
43//     #[allow(unused_imports)]
44//     use $crate::private::{AdhocKind, TraitKind};
45//     let error = $msg;
46//     (&error).miette_kind().new(error)
47
48use super::Report;
49use core::fmt::{Debug, Display};
50
51use crate::Diagnostic;
52
53pub struct Adhoc;
54
55pub trait AdhocKind: Sized {
56    #[inline]
57    fn miette_kind(&self) -> Adhoc {
58        Adhoc
59    }
60}
61
62impl<T> AdhocKind for &T where T: ?Sized + Display + Debug + Send + Sync + 'static {}
63
64impl Adhoc {
65    #[cfg_attr(track_caller, track_caller)]
66    #[cold]
67    pub fn new<M>(self, message: M) -> Report
68    where
69        M: Display + Debug + Send + Sync + 'static,
70    {
71        Report::from_adhoc(message)
72    }
73}
74
75pub struct Trait;
76
77pub trait TraitKind: Sized {
78    #[inline]
79    fn miette_kind(&self) -> Trait {
80        Trait
81    }
82}
83
84impl<E> TraitKind for E where E: Into<Report> {}
85
86impl Trait {
87    #[cfg_attr(track_caller, track_caller)]
88    #[cold]
89    pub fn new<E>(self, error: E) -> Report
90    where
91        E: Into<Report>,
92    {
93        error.into()
94    }
95}
96
97pub struct Boxed;
98
99pub trait BoxedKind: Sized {
100    #[inline]
101    fn miette_kind(&self) -> Boxed {
102        Boxed
103    }
104}
105
106impl BoxedKind for Box<dyn Diagnostic + Send + Sync> {}
107
108impl Boxed {
109    #[cfg_attr(track_caller, track_caller)]
110    #[cold]
111    pub fn new(self, error: Box<dyn Diagnostic + Send + Sync>) -> Report {
112        Report::from_boxed(error)
113    }
114}