async_ssh2_lite/
error.rs

1use std::io::{Error as IoError, ErrorKind as IoErrorKind};
2
3use ssh2::Error as Ssh2Error;
4
5//
6#[derive(Debug)]
7pub enum Error {
8    Ssh2(Ssh2Error),
9    Io(IoError),
10    Other(Box<dyn std::error::Error + Send + Sync + 'static>),
11}
12
13impl core::fmt::Display for Error {
14    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
15        write!(f, "{self:?}")
16    }
17}
18impl std::error::Error for Error {}
19
20//
21impl Error {
22    pub fn as_ssh2(&self) -> Option<&Ssh2Error> {
23        match self {
24            Self::Ssh2(err) => Some(err),
25            _ => None,
26        }
27    }
28
29    pub fn as_io(&self) -> Option<&IoError> {
30        match self {
31            Self::Io(err) => Some(err),
32            _ => None,
33        }
34    }
35
36    pub fn as_other(&self) -> Option<&(dyn std::error::Error + Send + Sync + 'static)> {
37        match self {
38            Self::Other(err) => Some(err.as_ref()),
39            _ => None,
40        }
41    }
42}
43
44//
45impl From<Ssh2Error> for Error {
46    fn from(err: Ssh2Error) -> Self {
47        Self::Ssh2(err)
48    }
49}
50
51impl From<IoError> for Error {
52    fn from(err: IoError) -> Self {
53        Self::Io(err)
54    }
55}
56
57//
58impl From<Error> for IoError {
59    fn from(err: Error) -> Self {
60        match err {
61            Error::Ssh2(err) => IoError::new(IoErrorKind::Other, err),
62            Error::Io(err) => err,
63            Error::Other(err) => IoError::new(IoErrorKind::Other, err),
64        }
65    }
66}