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
use async_std::channel::RecvError;
use displaydoc::Display;
use serde_derive::{Deserialize, Serialize};
use std::{io::Error as IoError, time::Duration};
use thiserror::Error;

/// All of the errors that can result while managing the child process.
#[derive(Error, Display, Debug)]
#[non_exhaustive]
pub enum Error {
    /// IO error
    Io(#[source] IoError),
    /// {0}
    Recv(RecvError),
    /// Failed to send {0}
    Send(&'static str),
    /// Timed out after {0:?}
    Timeout(Duration),
    /// Bincode
    Bincode(#[source] bincode::Error),
    /// Panic: {0}
    Panic(String),
    /// Failed to start child process
    InitFailure(#[source] IoError),
    /// Failed to read from child
    ReadFailed(#[source] IoError),
    /// Failed to write to child
    WriteFailed(#[source] IoError),
    /// Failed to start child process: {0}
    HandshakeFailure(String),
    /// Child process crashed
    Crashed,
    /// Interrupted
    Interrupted,
}

impl From<IoError> for Error {
    fn from(err: IoError) -> Self {
        Error::Io(err)
    }
}

impl From<bincode::Error> for Error {
    fn from(err: bincode::Error) -> Self {
        Error::Bincode(err)
    }
}

impl From<RecvError> for Error {
    fn from(err: RecvError) -> Self {
        Error::Recv(err)
    }
}

impl From<ErrorResponse> for Error {
    fn from(err: ErrorResponse) -> Self {
        match err {
            ErrorResponse::Panic(message) => Error::Panic(message),
        }
    }
}

impl From<Error> for IoError {
    fn from(err: Error) -> IoError {
        IoError::new(std::io::ErrorKind::Other, format!("{}", err))
    }
}

#[derive(Serialize, Deserialize)]
pub(crate) enum ErrorResponse {
    Panic(String),
}