Skip to main content

foxtive_ntex/http/
mod.rs

1use foxtive::prelude::{AppMessage, AppResult};
2use ntex::http::error::BlockingError;
3
4pub mod extractors;
5pub mod kernel;
6pub mod middlewares;
7pub mod responder;
8pub mod response;
9pub mod server;
10
11use crate::enums::ResponseCode;
12pub use ntex::http::Method;
13use ntex::web::ServiceConfig;
14pub use ntex_cors::Cors;
15use responder::Responder;
16
17pub use crate::error::HttpError;
18
19pub type HttpResult = Result<ntex::web::HttpResponse, HttpError>;
20
21pub type HttpHandler = fn(cfg: &mut ServiceConfig);
22
23pub trait IntoAppResult<T> {
24    fn into_app_result(self) -> AppResult<T>;
25}
26
27pub trait IntoHttpResult {
28    fn into_http_result(self) -> HttpResult;
29}
30
31impl<T> IntoAppResult<T> for Result<AppResult<T>, BlockingError<AppMessage>> {
32    fn into_app_result(self) -> AppResult<T> {
33        self.unwrap_or_else(|msg| match msg {
34            BlockingError::Error(err) => err.ar(),
35            BlockingError::Canceled => AppMessage::InternalServerError.ar(),
36        })
37    }
38}
39
40impl<T> IntoAppResult<T> for Result<T, BlockingError<AppMessage>> {
41    fn into_app_result(self) -> AppResult<T> {
42        self.map_err(|err| match err {
43            BlockingError::Error(err) => err.ae(),
44            BlockingError::Canceled => AppMessage::InternalServerError.ae(),
45        })
46    }
47}
48
49impl IntoHttpResult for AppMessage {
50    fn into_http_result(self) -> HttpResult {
51        Err(self.into())
52    }
53}
54
55impl IntoHttpResult for AppResult<AppMessage> {
56    fn into_http_result(self) -> HttpResult {
57        match self {
58            Ok(res) => Ok(Responder::message(&res.message(), ResponseCode::Ok)),
59            Err(err) => Err(HttpError::AppError(err)),
60        }
61    }
62}