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
use std::fmt::{Debug, Display, Formatter};
use std::{fmt, io};
use std::error::Error;

pub struct SshError {
    inner: SshErrorKind
}

impl SshError {
    pub fn kind(&self) -> SshErrorKind {
        match &self.inner {
            SshErrorKind::IoError(ie) => {
                SshErrorKind::IoError(io::Error::from(ie.kind()))
            }
            _ => {
                unsafe {std::ptr::read(&self.inner as *const SshErrorKind)}
            }
        }
    }
}


impl Debug for SshError {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match &self.inner {
            SshErrorKind::IoError(ie) => {
                write!(f, r"IoError: {{ Kind({:?}), Message({})}}", ie.kind(), ie.to_string())
            }
            _ => { write!(f, r"Error: {{ Kind({:?}), Message({}) }}", self.inner, self.inner.as_str()) }
        }
    }
}

impl Display for SshError {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match &self.inner {
            SshErrorKind::IoError(ie) => {
                write!(f, r"IoError: {{ Kind({:?}) }}", ie.kind())
            }
            _ => { write!(f, r"Error: {{ Kind({:?}) }}", self.inner) }
        }

    }
}


#[derive(Debug)]
pub enum SshErrorKind {
    IoError(io::Error),
    EncryptionError,
    FromUtf8Error,
    ChannelFailureError,
    PasswordError,
    UserNullError,
    PasswordNullError,
    SignatureError,
    VersionError,
    KeyExchangeError
}



impl PartialEq<Self> for SshErrorKind {
    fn eq(&self, other: &Self) -> bool {
       match (&self, &other) {
           (&SshErrorKind::IoError(io1), &SshErrorKind::IoError(io2)) => io1.kind() == io2.kind(),
           (&SshErrorKind::EncryptionError, &SshErrorKind::EncryptionError) => true,
           (&SshErrorKind::FromUtf8Error, &SshErrorKind::FromUtf8Error) => true,
           (&SshErrorKind::ChannelFailureError, &SshErrorKind::ChannelFailureError) => true,
           (&SshErrorKind::PasswordError, &SshErrorKind::PasswordError) => true,
           (&SshErrorKind::UserNullError, &SshErrorKind::UserNullError) => true,
           (&SshErrorKind::PasswordNullError, &SshErrorKind::PasswordNullError) => true,
           (&SshErrorKind::SignatureError, &SshErrorKind::SignatureError) => true,
           (&SshErrorKind::VersionError, &SshErrorKind::VersionError) => true,
           (&SshErrorKind::KeyExchangeError, &SshErrorKind::KeyExchangeError) => true,
           _ => false
       }
    }
}




impl SshErrorKind {
    fn as_str(&self) -> &'static str {
        match self {
            SshErrorKind::EncryptionError => "Key generation or encryption or decryption is abnormal",
            SshErrorKind::FromUtf8Error => "The UTF8 conversion is abnormal",
            SshErrorKind::ChannelFailureError => "Connection channel failure",
            SshErrorKind::PasswordError => "Password authentication failed",
            SshErrorKind::UserNullError => "The user cannot be empty",
            SshErrorKind::PasswordNullError => "The password cannot be empty",
            SshErrorKind::SignatureError => "Signature verification failure",
            SshErrorKind::VersionError => "Version not supported",
            SshErrorKind::KeyExchangeError => "The key exchange algorithm does not match",
            _ => ""
        }
    }
}

impl Error for SshError {
    fn description(&self) -> &str {
        match &self.inner {
            SshErrorKind::IoError(io) => io.description(),
            _ => self.inner.as_str()
        }
    }
}

impl From<SshErrorKind> for SshError {
    fn from(kind: SshErrorKind) -> SshError {
        SshError {
            inner: kind
        }
    }
}

impl From<io::Error> for SshError {
    fn from(kind: io::Error) -> Self {
        SshError {
            inner: SshErrorKind::IoError(io::Error::from(kind.kind()))
        }
    }
}

#[test]
fn test() {
    match get_error() {
        Ok(_) => {}
        Err(e) => {
            if e.kind() == SshErrorKind::SignatureError { }
        }
    }
    //get_error().unwrap();
}

fn get_error() -> Result<(), SshError> {
    return Err(SshError::from(SshErrorKind::EncryptionError))
    // return Err(SshError::from(SshErrorKind::IoError(io::Error::from(io::ErrorKind::WouldBlock))))
}