git-box 0.2.0

git-box executable implementation
#[cfg(any(feature = "artifactory", feature = "s3"))]
use curl::ErrCode;
use docopt;
use git2;
#[cfg(feature = "mysql")]
use mysql;
#[cfg(feature = "postgres")]
use openssl;
#[cfg(feature = "postgres")]
use postgres;
#[cfg(feature = "rusqlite")]
use rusqlite;
use sodium_sys::SSError;
#[cfg(feature = "ssh2")]
use ssh2::Error as Ssh2Error;
use std::error::Error;
use std::fmt;
use std::io;
use std::num::ParseIntError;
use std::string::FromUtf8Error;
#[cfg(feature = "s3")]
use warheadhateus;

use self::GitBoxError::*;

#[derive(Debug)]
pub enum ParseErrorType {
    Int(ParseIntError),
    #[cfg(feature = "s3")]
    Region(warheadhateus::ParseRegionError),
    Str(FromUtf8Error),
}

impl fmt::Display for ParseErrorType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            ParseErrorType::Int(ref e) => writeln!(f, "{}", e),
            #[cfg(feature = "s3")]
            ParseErrorType::Region(ref e) => writeln!(f, "{}", e),
            ParseErrorType::Str(ref e) => writeln!(f, "{}", e),
        }
    }
}

#[derive(Debug)]
/// An error thrown by the git-box process.
pub enum GitBoxError {
    #[cfg(any(feature = "artifactory", feature = "s3"))]
    /// Error thrown during Curl operations.
    Curl(ErrCode),
    /// Error thrown parsing arguments.
    Docopt(docopt::Error),
    /// Error thrown during git operations.
    Git(git2::Error),
    /// Error thrown during I/O operations.
    IO(io::Error),
    #[cfg(feature = "mysql")]
    /// Error thrown during Mysql operations.
    Mysql(mysql::error::MyError),
    #[cfg(feature = "postgres")]
    /// Error thrown during Posgtres operations.
    Postgres(postgres::error::Error),
    #[cfg(feature = "postgres")]
    /// Error thrown during Posgtres connect operations.
    PostgresConnect(postgres::error::ConnectError),
    /// Error thrown during sodium_sys operations.
    Sodium(SSError),
    /// Error thrown during parsing operations.
    Parse(ParseErrorType),
    #[cfg(feature = "s3")]
    /// Error thrown during S3 operations.
    S3(warheadhateus::AWSAuthError),
    #[cfg(feature = "rusqlite")]
    /// Error thrown during Sqlite operations.
    Sqlite(rusqlite::Error),
    #[cfg(feature = "ssh2")]
    /// Error thrown during ssh operations.
    SSH(Ssh2Error),
    #[cfg(feature = "postgres")]
    /// Error thrown during ssl operations over postgres connections.
    SSL(openssl::ssl::error::SslError),
    /// Generic Error.
    Other(&'static str),
}

impl fmt::Display for GitBoxError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            #[cfg(feature = "artifactory")]
            Curl(ref e) => writeln!(f, "{}", e),
            Docopt(ref e) => writeln!(f, "{}", e),
            Git(ref e) => writeln!(f, "{}", e),
            IO(ref e) => writeln!(f, "{}", e),
            #[cfg(feature = "mysql")]
            Mysql(ref e) => writeln!(f, "{}", e),
            Sodium(ref e) => writeln!(f, "{:?}", e),
            Parse(ref e) => writeln!(f, "{}", e),
            #[cfg(feature = "postgres")]
            PostgresConnect(ref e) => writeln!(f, "{}", e),
            #[cfg(feature = "postgres")]
            Postgres(ref e) => writeln!(f, "{}", e),
            #[cfg(feature = "s3")]
            S3(ref e) => writeln!(f, "{}", e),
            #[cfg(feature = "rusqlite")]
            Sqlite(ref e) => writeln!(f, "{}", e),
            #[cfg(feature = "ssh2")]
            SSH(ref e) => writeln!(f, "{}", e),
            #[cfg(feature = "postgres")]
            SSL(ref e) => writeln!(f, "{}", e),
            Other(ref e) => writeln!(f, "{}", e),
        }
    }
}

impl Error for GitBoxError {
    fn description(&self) -> &str {
        match *self {
            #[cfg(feature = "artifactory")]
            Curl(ref e) => e.description(),
            Docopt(ref e) => e.description(),
            Git(ref e) => e.description(),
            IO(ref e) => e.description(),
            #[cfg(feature = "mysql")]
            Mysql(ref e) => e.description(),
            Sodium(_) => "sodium_sys error",
            Parse(ref e) => {
                match *e {
                    ParseErrorType::Int(ref pe) => pe.description(),
                    #[cfg(feature = "s3")]
                    ParseErrorType::Region(ref pe) => pe.description(),
                    ParseErrorType::Str(ref pe) => pe.description(),
                }
            }
            #[cfg(feature = "postgres")]
            PostgresConnect(ref e) => e.description(),
            #[cfg(feature = "postgres")]
            Postgres(ref e) => e.description(),
            #[cfg(feature = "s3")]
            S3(ref e) => e.description(),
            #[cfg(feature = "rusqlite")]
            Sqlite(ref e) => e.description(),
            #[cfg(feature = "postgres")]
            SSL(ref e) => e.description(),
            #[cfg(feature = "ssh2")]
            SSH(ref e) => e.description(),
            Other(ref e) => e,
        }
    }

    fn cause(&self) -> Option<&Error> {
        match *self {
            #[cfg(feature = "artifactory")]
            Curl(ref e) => Some(e),
            Docopt(ref e) => Some(e),
            Git(ref e) => Some(e),
            IO(ref e) => Some(e),
            #[cfg(feature = "mysql")]
            Mysql(ref e) => Some(e),
            Sodium(_) => None,
            Parse(ref e) => {
                match *e {
                    ParseErrorType::Int(ref pe) => Some(pe),
                    #[cfg(feature = "s3")]
                ParseErrorType::Region(ref pe) => Some(pe),
                    ParseErrorType::Str(ref pe) => Some(pe),
                }
            }
            #[cfg(feature = "postgres")]
            PostgresConnect(ref e) => Some(e),
            #[cfg(feature = "postgres")]
            Postgres(ref e) => Some(e),
            #[cfg(feature = "s3")]
            S3(ref e) => Some(e),
            #[cfg(feature = "rusqlite")]
            Sqlite(ref e) => Some(e),
            #[cfg(feature = "ssh2")]
            SSH(ref e) => Some(e),
            #[cfg(feature = "postgres")]
            SSL(ref e) => Some(e),
            Other(_) => None,
        }
    }
}

#[cfg(any(feature = "artifactory", feature = "s3"))]
impl From<ErrCode> for GitBoxError {
    fn from(err: ErrCode) -> GitBoxError {
        Curl(err)
    }
}

#[cfg(feature = "mysql")]
impl From<mysql::error::MyError> for GitBoxError {
    fn from(err: mysql::error::MyError) -> GitBoxError {
        Mysql(err)
    }
}

#[cfg(feature = "postgres")]
impl From<postgres::error::ConnectError> for GitBoxError {
    fn from(err: postgres::error::ConnectError) -> GitBoxError {
        PostgresConnect(err)
    }
}

#[cfg(feature = "postgres")]
impl From<postgres::error::Error> for GitBoxError {
    fn from(err: postgres::error::Error) -> GitBoxError {
        Postgres(err)
    }
}

#[cfg(feature = "postgres")]
impl From<openssl::ssl::error::SslError> for GitBoxError {
    fn from(err: openssl::ssl::error::SslError) -> GitBoxError {
        SSL(err)
    }
}

#[cfg(feature = "rusqlite")]
impl From<rusqlite::Error> for GitBoxError {
    fn from(err: rusqlite::Error) -> GitBoxError {
        Sqlite(err)
    }
}

#[cfg(feature = "s3")]
impl From<warheadhateus::AWSAuthError> for GitBoxError {
    fn from(err: warheadhateus::AWSAuthError) -> GitBoxError {
        S3(err)
    }
}

#[cfg(feature = "s3")]
impl From<warheadhateus::ParseRegionError> for GitBoxError {
    fn from(err: warheadhateus::ParseRegionError) -> GitBoxError {
        Parse(ParseErrorType::Region(err))
    }
}

impl From<docopt::Error> for GitBoxError {
    fn from(err: docopt::Error) -> GitBoxError {
        Docopt(err)
    }
}

impl From<git2::Error> for GitBoxError {
    fn from(err: git2::Error) -> GitBoxError {
        Git(err)
    }
}

impl From<io::Error> for GitBoxError {
    fn from(err: io::Error) -> GitBoxError {
        IO(err)
    }
}

impl From<SSError> for GitBoxError {
    fn from(err: SSError) -> GitBoxError {
        Sodium(err)
    }
}

#[cfg(feature = "ssh2")]
impl From<Ssh2Error> for GitBoxError {
    fn from(err: Ssh2Error) -> GitBoxError {
        SSH(err)
    }
}

impl From<ParseIntError> for GitBoxError {
    fn from(err: ParseIntError) -> GitBoxError {
        Parse(ParseErrorType::Int(err))
    }
}

impl From<FromUtf8Error> for GitBoxError {
    fn from(err: FromUtf8Error) -> GitBoxError {
        Parse(ParseErrorType::Str(err))
    }
}