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
use std::fmt::{self, Display, Formatter};

#[derive(Debug)]
pub enum Error {
  IoError(std::io::Error),
  SshError(ssh2::Error),

  AuthenticationError(String),
  KnownHostCheckError(String),
  SftpError(String),
}

pub type Result<T> = std::result::Result<T, Error>;

impl std::error::Error for Error {}

impl Display for Error {
  fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
    match self {
      Error::IoError(error) => write!(f, "IO error: {}", error),
      Error::SshError(error) => write!(f, "SSH error: {}", error),
      Error::AuthenticationError(error) => write!(f, "Authentication failure: {}", error),
      Error::KnownHostCheckError(error) => write!(f, "Known host check failure: {}", error),
      Error::SftpError(error) => write!(f, "SFTP error: {}", error),
    }
  }
}

impl From<std::io::Error> for Error {
  fn from(error: std::io::Error) -> Self {
    Error::IoError(error)
  }
}

impl From<ssh2::Error> for Error {
  fn from(error: ssh2::Error) -> Self {
    Error::SshError(error)
  }
}

impl Into<std::io::Error> for Error {
  fn into(self) -> std::io::Error {
    use std::io::ErrorKind;
    match self {
      Error::IoError(error) => error,
      Error::SshError(error) => std::io::Error::new(ErrorKind::Other, format!("SSH: {}", error)),
      Error::AuthenticationError(error) => {
        std::io::Error::new(ErrorKind::Other, format!("Auth: {}", error))
      }
      Error::KnownHostCheckError(error) => {
        std::io::Error::new(ErrorKind::Other, format!("Host: {}", error))
      }
      Error::SftpError(error) => std::io::Error::new(ErrorKind::Other, format!("SFTP: {}", error)),
    }
  }
}