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 48 use super::Report; 49 use core::fmt::{Debug, Display}; 50 51 use crate::Diagnostic; 52 53 pub struct Adhoc; 54 55 pub trait AdhocKind: Sized { 56 #[inline] miette_kind(&self) -> Adhoc57 fn miette_kind(&self) -> Adhoc { 58 Adhoc 59 } 60 } 61 62 impl<T> AdhocKind for &T where T: ?Sized + Display + Debug + Send + Sync + 'static {} 63 64 impl Adhoc { 65 #[cfg_attr(track_caller, track_caller)] new<M>(self, message: M) -> Report where M: Display + Debug + Send + Sync + 'static,66 pub fn new<M>(self, message: M) -> Report 67 where 68 M: Display + Debug + Send + Sync + 'static, 69 { 70 Report::from_adhoc(message) 71 } 72 } 73 74 pub struct Trait; 75 76 pub trait TraitKind: Sized { 77 #[inline] miette_kind(&self) -> Trait78 fn miette_kind(&self) -> Trait { 79 Trait 80 } 81 } 82 83 impl<E> TraitKind for E where E: Into<Report> {} 84 85 impl Trait { 86 #[cfg_attr(track_caller, track_caller)] new<E>(self, error: E) -> Report where E: Into<Report>,87 pub fn new<E>(self, error: E) -> Report 88 where 89 E: Into<Report>, 90 { 91 error.into() 92 } 93 } 94 95 pub struct Boxed; 96 97 pub trait BoxedKind: Sized { 98 #[inline] miette_kind(&self) -> Boxed99 fn miette_kind(&self) -> Boxed { 100 Boxed 101 } 102 } 103 104 impl BoxedKind for Box<dyn Diagnostic + Send + Sync> {} 105 106 impl Boxed { 107 #[cfg_attr(track_caller, track_caller)] new(self, error: Box<dyn Diagnostic + Send + Sync>) -> Report108 pub fn new(self, error: Box<dyn Diagnostic + Send + Sync>) -> Report { 109 Report::from_boxed(error) 110 } 111 } 112