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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright Materialize, Inc. and contributors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE file at the
// root of this repository, or online at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Result utilities.

use std::convert::Infallible;

use crate::error::ErrorExt;

/// Extension methods for [`std::result::Result`].
pub trait ResultExt<T, E> {
    /// Applies [`Into::into`] to a contained [`Err`] value, leaving an [`Ok`]
    /// value untouched.
    fn err_into<E2>(self) -> Result<T, E2>
    where
        E: Into<E2>;

    /// Formats an [`Err`] value as a detailed error message, preserving any context information.
    ///
    /// This is equivalent to `format!("{}", err.display_with_causes())`, except that it's easier to
    /// type.
    fn err_to_string_with_causes(&self) -> Option<String>
    where
        E: std::error::Error;

    /// Maps a `Result<T, E>` to `Result<T, String>` by converting the [`Err`] result into a string,
    /// along with the chain of source errors, if any.
    fn map_err_to_string_with_causes(self) -> Result<T, String>
    where
        E: std::error::Error;

    /// Safely unwraps a `Result<T, Infallible>`, where [`Infallible`] is a type that represents when
    /// an error cannot occur.
    fn infallible_unwrap(self) -> T
    where
        E: Into<Infallible>;
}

impl<T, E> ResultExt<T, E> for Result<T, E> {
    fn err_into<E2>(self) -> Result<T, E2>
    where
        E: Into<E2>,
    {
        self.map_err(|e| e.into())
    }

    fn err_to_string_with_causes(&self) -> Option<String>
    where
        E: std::error::Error,
    {
        self.as_ref().err().map(ErrorExt::to_string_with_causes)
    }

    fn map_err_to_string_with_causes(self) -> Result<T, String>
    where
        E: std::error::Error,
    {
        self.map_err(|e| ErrorExt::to_string_with_causes(&e))
    }

    fn infallible_unwrap(self) -> T
    where
        E: Into<Infallible>,
    {
        match self {
            Ok(t) => t,
            Err(e) => {
                let _infallible = e.into();

                // This code will forever be unreachable because Infallible is an enum
                // with no variants, so it's impossible to construct. If it ever does
                // become possible to construct this will become a compile time error
                // since there will be a variant we're not matching on.
                #[allow(unreachable_code)]
                match _infallible {}
            }
        }
    }
}

#[cfg(test)]
mod test {
    use std::fmt::Display;

    use anyhow::anyhow;

    use super::*;

    #[crate::test]
    fn prints_error_chain() {
        let error = anyhow!("root");
        let error = error.context("context");
        let error = TestError { inner: error };
        let res: Result<(), _> = Err(error);

        assert_eq!(
            res.err_to_string_with_causes(),
            Some("test error: context: root".to_string())
        );

        let error = anyhow!("root");
        let error = error.context("context");
        let error = TestError { inner: error };
        let res: Result<(), _> = Err(error);

        assert_eq!(
            res.map_err_to_string_with_causes(),
            Err("test error: context: root".to_string())
        );
    }

    #[derive(Debug)]
    struct TestError {
        inner: anyhow::Error,
    }

    impl Display for TestError {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            // We don't print our causes.
            write!(f, "test error")?;
            Ok(())
        }
    }

    impl std::error::Error for TestError {
        fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
            Some(self.inner.as_ref())
        }
    }
}