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
use {ExitStatus, EXIT_ERROR};
use error::IsFatalError;
use env::{LastStatusEnvironment, ReportErrorEnvironment};
use future::{Async, EnvFuture, Poll};

/// A future representing a word evaluation and conditionally splitting it afterwards.
#[must_use = "futures do nothing unless polled"]
#[derive(Debug)]
pub struct SwallowNonFatal<F> {
    inner: F,
}

/// Creates a future adapter that will swallow (and report) non-fatal errors
/// and resolve to `EXIT_ERROR` if they arise.
///
/// All other responses are propagated through as is.
pub fn swallow_non_fatal_errors<F>(inner: F) -> SwallowNonFatal<F> {
    SwallowNonFatal {
        inner: inner,
    }
}

impl<F, E: ?Sized> EnvFuture<E> for SwallowNonFatal<F>
    where F: EnvFuture<E>,
          F::Item: From<ExitStatus>,
          F::Error: IsFatalError,
          E: LastStatusEnvironment + ReportErrorEnvironment,
{
    type Item = F::Item;
    type Error = F::Error;

    fn poll(&mut self, env: &mut E) -> Poll<Self::Item, Self::Error> {
        match self.inner.poll(env) {
            Ok(Async::Ready(ret)) => Ok(Async::Ready(ret)),
            Ok(Async::NotReady) => Ok(Async::NotReady),
            Err(e) => {
                if e.is_fatal() {
                    Err(e)
                } else {
                    env.report_error(&e);
                    Ok(Async::Ready(EXIT_ERROR.into()))
                }
            },
        }
    }

    fn cancel(&mut self, env: &mut E) {
        self.inner.cancel(env);
    }
}

impl<F> AsRef<F> for SwallowNonFatal<F> {
    fn as_ref(&self) -> &F {
        &self.inner
    }
}

impl<F> AsMut<F> for SwallowNonFatal<F> {
    fn as_mut(&mut self) -> &mut F {
        &mut self.inner
    }
}