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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under both the MIT license found in the
 * LICENSE-MIT file in the root directory of this source tree and the Apache
 * License, Version 2.0 found in the LICENSE-APACHE file in the root directory
 * of this source tree.
 */

use std::fmt::Display;

use futures::Future;
use futures::Poll;

/// "Context" support for futures.
pub trait FutureErrorContext: Future + Sized {
    /// Add context to the error returned by this future
    fn context<D>(self, context: D) -> ContextFut<Self, D>
    where
        D: Display + Send + Sync + 'static;

    /// Add context created by provided function to the error returned by this future
    fn with_context<D, F>(self, f: F) -> WithContextFut<Self, F>
    where
        D: Display + Send + Sync + 'static,
        F: FnOnce() -> D;
}

impl<F, E> FutureErrorContext for F
where
    F: Future<Error = E> + Sized,
    E: Into<anyhow::Error>,
{
    fn context<D>(self, displayable: D) -> ContextFut<Self, D>
    where
        D: Display + Send + Sync + 'static,
    {
        ContextFut::new(self, displayable)
    }

    fn with_context<D, O>(self, f: O) -> WithContextFut<Self, O>
    where
        D: Display + Send + Sync + 'static,
        O: FnOnce() -> D,
    {
        WithContextFut::new(self, f)
    }
}

pub struct ContextFut<A, D> {
    inner: A,
    displayable: Option<D>,
}

impl<A, D> ContextFut<A, D> {
    pub fn new(future: A, displayable: D) -> Self {
        Self {
            inner: future,
            displayable: Some(displayable),
        }
    }
}

impl<A, E, D> Future for ContextFut<A, D>
where
    A: Future<Error = E>,
    E: Into<anyhow::Error>,
    D: Display + Send + Sync + 'static,
{
    type Item = A::Item;
    type Error = anyhow::Error;

    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        match self.inner.poll() {
            Err(err) => Err(err.into().context(
                self.displayable
                    .take()
                    .expect("poll called after future completion"),
            )),
            Ok(item) => Ok(item),
        }
    }
}

pub struct WithContextFut<A, F> {
    inner: A,
    displayable: Option<F>,
}

impl<A, F> WithContextFut<A, F> {
    pub fn new(future: A, displayable: F) -> Self {
        Self {
            inner: future,
            displayable: Some(displayable),
        }
    }
}

impl<A, E, F, D> Future for WithContextFut<A, F>
where
    A: Future<Error = E>,
    E: Into<anyhow::Error>,
    D: Display + Send + Sync + 'static,
    F: FnOnce() -> D,
{
    type Item = A::Item;
    type Error = anyhow::Error;

    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        match self.inner.poll() {
            Err(err) => {
                let f = self
                    .displayable
                    .take()
                    .expect("poll called after future completion");

                let context = f();
                Err(err.into().context(context))
            }
            Ok(item) => Ok(item),
        }
    }
}

#[cfg(test)]
mod test {
    use anyhow::format_err;
    use futures::future::err;

    use super::*;

    #[test]
    #[should_panic]
    fn poll_after_completion_fail() {
        let err = err::<(), _>(format_err!("foo").context("bar"));
        let mut err = err.context("baz");
        let _ = err.poll();
        let _ = err.poll();
    }

    #[test]
    #[should_panic]
    fn poll_after_completion_fail_with_context() {
        let err = err::<(), _>(format_err!("foo").context("bar"));
        let mut err = err.with_context(|| "baz");
        let _ = err.poll();
        let _ = err.poll();
    }

    #[test]
    #[should_panic]
    fn poll_after_completion_error() {
        let err = err::<(), _>(format_err!("foo"));
        let mut err = err.context("baz");
        let _ = err.poll();
        let _ = err.poll();
    }

    #[test]
    #[should_panic]
    fn poll_after_completion_error_with_context() {
        let err = err::<(), _>(format_err!("foo"));
        let mut err = err.with_context(|| "baz");
        let _ = err.poll();
        let _ = err.poll();
    }
}