use common::build::BuildError;
use rocket::response::Responder;
use serde::Serialize;

#[derive(Debug, Serialize, Responder)]
pub enum ApiError {
    #[response(status = 404)]
    NotFound(String),

    #[response(status = 500)]
    InternalServerError(()),

    #[response(status = 500)]
    ServerError(String),

    #[response(status = 401)]
    Unauthorized(String),
    #[response(status = 403)]
    Forbidden(String),

    #[response(status = 400)]
    BadRequest(String),
}

impl From<std::io::Error> for ApiError {
    fn from(err: std::io::Error) -> Self {
        ApiError::InternalServerError(())
    }
}

impl From<BuildError> for ApiError {
    fn from(err: BuildError) -> Self {
        match err {
            BuildError::IoError(err) => ApiError::ServerError(err.to_string()),
            BuildError::Generic(err) => ApiError::ServerError(err),
        }
    }
}
