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
114
115
116
117
118
119
120
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, 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 wrap_help<C, F>(self, f: F, help: &'static str) -> Result<T, Error>
    where
        C: Display + Send + Sync + 'static,
        F: FnOnce() -> C,
    {
        self.map_err(|err| err.ext_context_help(f(), help))
    }

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

mod ext {
    use crate::error::HelpMsg;

    use super::*;

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

        fn ext_context_help<C>(self, context: C, help: &'static str) -> Error
        where
            C: Display + Send + Sync + 'static;

        fn ext_context_help_owned<C>(self, context: C, help: String) -> Error
        where
            C: Display + Send + Sync + 'static;
    }

    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_context_help<C>(self, context: C, help: &'static str) -> Error
        where
            C: Display + Send + Sync + 'static,
        {
            let mut err = Error::from(self).wrap(context);
            err.set_help(help);
            err
        }

        fn ext_context_help_owned<C>(self, context: C, help: String) -> Error
        where
            C: Display + Send + Sync + 'static,
        {
            let mut err = Error::from(self).wrap(context);
            err.set_help_owned(help);
            err
        }
    }

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

        fn ext_context_help<C>(self, context: C, help: &'static str) -> Error
        where
            C: Display + Send + Sync + 'static,
        {
            let mut err = self.wrap(context);
            match err.help {
                Some(HelpMsg::Owned(ref mut msg)) => {
                    msg.push('\n');
                    msg.push_str(help);
                }
                Some(HelpMsg::Static(msg)) => err.set_help_owned(format!("{}\n{}", msg, help)),

                None => err.set_help(help),
            }
            err
        }

        fn ext_context_help_owned<C>(self, context: C, help: String) -> Error
        where
            C: Display + Send + Sync + 'static,
        {
            let mut err = self.wrap(context);
            match err.help() {
                Some(msg) => err.set_help_owned(format!("{}\n{}", msg, help)),
                None => err.set_help_owned(help),
            }
            err
        }
    }
}