use actix_web::dev::HttpResponseBuilder;
use actix_web::http::StatusCode;
use actix_web_httpauth::extractors::{basic, AuthenticationError};

pub enum Error {
    BadGateway(String),
    Unauthorized(String),
}

impl Error {
}

impl From<git2::Error> for Error {
    fn from(giterr: git2::Error) -> Self {
        Error::BadGateway(format!("{}", giterr))
    }
}

impl From<std::io::Error> for Error {
    fn from(ioerr: std::io::Error) -> Self {
        Error::BadGateway(format!("{}", ioerr))
    }
}

impl From<Error> for actix_web::error::Error {
    fn from(e: Error) -> Self {
        match e {
            Error::BadGateway(msg) => {HttpResponseBuilder::new(StatusCode::BAD_GATEWAY).body(msg).into()}
            Error::Unauthorized(realm) => {AuthenticationError::from(basic::Config::default().realm(realm)).into()}}
    }
}