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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use std::ffi;
use std::fmt;
use std::io;

use failure;

fn format_cmd(cmd: &[ffi::OsString]) -> String {
    let result: Vec<String> = cmd.iter()
        .map(|s| s.to_string_lossy().into_owned())
        .collect();
    result.join(" ")
}

pub trait ChainFail {
    fn chain<E>(self, cause: E) -> Self
    where
        E: Into<failure::Error>;
}

pub trait ResultChainExt<T> {
    fn chain<C>(self, chainable: C) -> Result<T, C>
    where
        C: ChainFail;

    fn chain_with<F, C>(self, chainable: F) -> Result<T, C>
    where
        F: FnOnce() -> C,
        C: ChainFail;
}

impl<T> ResultChainExt<T> for Result<T, failure::Error> {
    fn chain<C>(self, chainable: C) -> Result<T, C>
    where
        C: ChainFail,
    {
        self.map_err(|e| chainable.chain(e))
    }

    fn chain_with<F, C>(self, chainable: F) -> Result<T, C>
    where
        F: FnOnce() -> C,
        C: ChainFail,
    {
        self.map_err(|e| chainable().chain(e))
    }
}

impl<T> ResultChainExt<T> for Result<T, io::Error> {
    fn chain<C>(self, chainable: C) -> Result<T, C>
    where
        C: ChainFail,
    {
        self.map_err(|e| chainable.chain(e))
    }

    fn chain_with<F, C>(self, chainable: F) -> Result<T, C>
    where
        F: FnOnce() -> C,
        C: ChainFail,
    {
        self.map_err(|e| chainable().chain(e))
    }
}

/// Failure when processing assertions.
#[derive(Debug)]
pub struct AssertionError {
    cmd: Vec<ffi::OsString>,
    cause: Option<failure::Error>,
}

impl AssertionError {
    pub(crate) fn new(cmd: Vec<ffi::OsString>) -> Self {
        Self { cmd, cause: None }
    }
}

impl failure::Fail for AssertionError {
    fn cause(&self) -> Option<&failure::Fail> {
        self.cause.as_ref().map(failure::Error::cause)
    }

    fn backtrace(&self) -> Option<&failure::Backtrace> {
        None
    }
}

impl ChainFail for AssertionError {
    fn chain<E>(mut self, error: E) -> Self
    where
        E: Into<failure::Error>,
    {
        self.cause = Some(error.into());
        self
    }
}

impl fmt::Display for AssertionError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Assertion failed for `{}`", format_cmd(&self.cmd))
    }
}

#[derive(Debug)]
pub struct StatusError {
    unexpected: bool,
    stdout: Vec<u8>,
    stderr: Vec<u8>,
    cause: Option<failure::Error>,
}

impl StatusError {
    pub fn new(unexpected: bool, stdout: Vec<u8>, stderr: Vec<u8>) -> Self {
        Self {
            unexpected,
            stdout,
            stderr,
            cause: None,
        }
    }
}

impl failure::Fail for StatusError {
    fn cause(&self) -> Option<&failure::Fail> {
        self.cause.as_ref().map(failure::Error::cause)
    }

    fn backtrace(&self) -> Option<&failure::Backtrace> {
        None
    }
}

impl ChainFail for StatusError {
    fn chain<E>(mut self, error: E) -> Self
    where
        E: Into<failure::Error>,
    {
        self.cause = Some(error.into());
        self
    }
}

impl fmt::Display for StatusError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let out = String::from_utf8_lossy(&self.stdout);
        let err = String::from_utf8_lossy(&self.stderr);
        writeln!(
            f,
            "Unexpected return status: {}",
            if self.unexpected {
                "success"
            } else {
                "failure"
            }
        )?;
        writeln!(f, "stdout=```{}```", out)?;
        write!(f, "stderr=```{}```", err)
    }
}

#[derive(Debug)]
pub struct ExitCodeError {
    expected: Option<i32>,
    got: Option<i32>,
    stdout: Vec<u8>,
    stderr: Vec<u8>,
    cause: Option<failure::Error>,
}

impl ExitCodeError {
    pub fn new(expected: Option<i32>, got: Option<i32>, stdout: Vec<u8>, stderr: Vec<u8>) -> Self {
        Self {
            expected,
            got,
            stdout,
            stderr,
            cause: None,
        }
    }
}

impl failure::Fail for ExitCodeError {
    fn cause(&self) -> Option<&failure::Fail> {
        self.cause.as_ref().map(failure::Error::cause)
    }

    fn backtrace(&self) -> Option<&failure::Backtrace> {
        None
    }
}

impl ChainFail for ExitCodeError {
    fn chain<E>(mut self, error: E) -> Self
    where
        E: Into<failure::Error>,
    {
        self.cause = Some(error.into());
        self
    }
}

impl fmt::Display for ExitCodeError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let out = String::from_utf8_lossy(&self.stdout);
        let err = String::from_utf8_lossy(&self.stderr);
        writeln!(f, "expected={:?}", self.expected)?;
        writeln!(f, "got={:?}", self.got)?;
        writeln!(f, "stdout=```{}```", out)?;
        write!(f, "stderr=```{}```", err)
    }
}