Coverage Report

Created: 2025-02-21 07:11

/rust/registry/src/index.crates.io-6f17d22bba15001f/miette-5.10.0/src/eyreish/kind.rs
Line
Count
Source (jump to first uncovered line)
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]
57
0
    fn miette_kind(&self) -> Adhoc {
58
0
        Adhoc
59
0
    }
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)]
66
0
    pub fn new<M>(self, message: M) -> Report
67
0
    where
68
0
        M: Display + Debug + Send + Sync + 'static,
69
0
    {
70
0
        Report::from_adhoc(message)
71
0
    }
72
}
73
74
pub struct Trait;
75
76
pub trait TraitKind: Sized {
77
    #[inline]
78
0
    fn miette_kind(&self) -> Trait {
79
0
        Trait
80
0
    }
81
}
82
83
impl<E> TraitKind for E where E: Into<Report> {}
84
85
impl Trait {
86
    #[cfg_attr(track_caller, track_caller)]
87
0
    pub fn new<E>(self, error: E) -> Report
88
0
    where
89
0
        E: Into<Report>,
90
0
    {
91
0
        error.into()
92
0
    }
93
}
94
95
pub struct Boxed;
96
97
pub trait BoxedKind: Sized {
98
    #[inline]
99
0
    fn miette_kind(&self) -> Boxed {
100
0
        Boxed
101
0
    }
102
}
103
104
impl BoxedKind for Box<dyn Diagnostic + Send + Sync> {}
105
106
impl Boxed {
107
    #[cfg_attr(track_caller, track_caller)]
108
0
    pub fn new(self, error: Box<dyn Diagnostic + Send + Sync>) -> Report {
109
0
        Report::from_boxed(error)
110
0
    }
111
}