1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use std::fmt::Display;

use crate::{Error, ErrorWrap};

impl<T, E> ErrorWrap<T, E> for Result<T, E>
where
    E: ext::StdError + Send + Sync + 'static,
{
    fn wrap<C>(self, context: C) -> crate::Result<T, Error>
    where
        C: Display + Send + Sync + 'static,
    {
        self.map_err(|err| err.ext_context(context))
    }

    fn wrap_with<C, F>(self, f: F) -> Result<T, Error>
    where
        C: Display + Send + Sync + 'static,
        F: FnOnce() -> C,
    {
        self.map_err(|err| err.ext_context(f()))
    }

    fn add_help(self, help: &'static str) -> Result<T, Error> {
        self.map_err(|err| err.ext_add_help(help))
    }

    fn add_help_with<C, F>(self, f: F) -> Result<T, Error>
    where
        C: Display + Send + Sync + 'static,
        F: FnOnce() -> C,
    {
        self.map_err(|err| err.ext_add_help_with(f))
    }
}

mod ext {
    use super::*;

    pub trait StdError {
        fn ext_context<C>(self, context: C) -> Error
        where
            C: Display + Send + Sync + 'static;

        fn ext_add_help(self, help: &'static str) -> Error;

        fn ext_add_help_with<C, F>(self, f: F) -> Error
        where
            C: Display + Send + Sync + 'static,
            F: FnOnce() -> C;
    }

    impl<E> StdError for E
    where
        E: std::error::Error + Send + Sync + 'static,
    {
        fn ext_context<C>(self, context: C) -> Error
        where
            C: Display + Send + Sync + 'static,
        {
            Error::from(self).wrap(context)
        }

        fn ext_add_help(self, help: &'static str) -> Error {
            let mut err = Error::from(self);
            err.add_help(help);
            err
        }

        fn ext_add_help_with<C, F>(self, f: F) -> Error
        where
            C: Display + Send + Sync + 'static,
            F: FnOnce() -> C,
        {
            let mut err = Error::from(self);
            err.add_help_with(f);
            err
        }
    }

    impl StdError for Error {
        fn ext_context<C>(self, context: C) -> Error
        where
            C: Display + Send + Sync + 'static,
        {
            self.wrap(context)
        }

        fn ext_add_help(mut self, help: &'static str) -> Error {
            self.add_help(help);
            self
        }

        fn ext_add_help_with<C, F>(mut self, f: F) -> Error
        where
            C: Display + Send + Sync + 'static,
            F: FnOnce() -> C,
            F: FnOnce() -> C,
        {
            self.add_help_with(f);
            self
        }
    }
}

pub(crate) mod private {
    use super::*;

    pub trait Sealed {}

    impl<T, E> Sealed for Result<T, E> where E: ext::StdError {}
    impl<T> Sealed for Option<T> {}
}