foxtive-ntex 0.31.0

Micro-Framework Based on Ntex
Documentation
use foxtive::internal_server_error;
use foxtive::prelude::{AppMessage, AppResult};
use ntex::http::error::BlockingError;

pub mod extractors;
pub mod kernel;
pub mod middlewares;
pub mod responder;
pub mod response;
pub mod server;
pub mod shutdown;

use crate::enums::ResponseCode;
pub use extractors::{ByteBody, ClientInfo, JsonBody, StringBody};
#[cfg(feature = "jwt")]
pub use extractors::JwtAuthToken;
pub use ntex::http::Method;
use ntex::web::ServiceConfig;
pub use ntex_cors::Cors;
pub use server::BodyConfig;
use responder::Responder;

pub use crate::error::HttpError;

pub type HttpResult = Result<ntex::web::HttpResponse, HttpError>;

pub type HttpHandler = fn(cfg: &mut ServiceConfig);

pub trait IntoAppResult<T> {
    fn into_app_result(self) -> AppResult<T>;
}

pub trait IntoHttpResult {
    fn into_http_result(self) -> HttpResult;
}

impl<T> IntoAppResult<T> for Result<AppResult<T>, BlockingError<AppMessage>> {
    fn into_app_result(self) -> AppResult<T> {
        self.unwrap_or_else(|msg| match msg {
            BlockingError::Error(err) => err.into_result(),
            BlockingError::Canceled => Err(internal_server_error!("Internal Server Error")),
        })
    }
}

impl<T> IntoAppResult<T> for Result<T, BlockingError<AppMessage>> {
    fn into_app_result(self) -> AppResult<T> {
        self.map_err(|err| match err {
            BlockingError::Error(err) => err.into_anyhow(),
            BlockingError::Canceled => internal_server_error!("Internal Server Error"),
        })
    }
}

impl IntoHttpResult for AppMessage {
    fn into_http_result(self) -> HttpResult {
        Err(self.into())
    }
}

impl IntoHttpResult for AppResult<AppMessage> {
    fn into_http_result(self) -> HttpResult {
        match self {
            Ok(res) => Ok(Responder::message(&res.message(), ResponseCode::Ok)),
            Err(err) => Err(HttpError::AppError(err)),
        }
    }
}