1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-28 09:42:40 +01:00
actix-extras/src/httpcodes.rs

48 lines
1.5 KiB
Rust
Raw Normal View History

2017-10-07 06:48:14 +02:00
//! Basic http responses
#![allow(non_upper_case_globals)]
use std::rc::Rc;
use http::StatusCode;
use task::Task;
2017-10-09 05:16:48 +02:00
use route::RouteHandler;
use payload::Payload;
2017-10-11 01:03:32 +02:00
use httpmessage::{Body, Builder, HttpRequest, HttpResponse};
2017-10-07 06:48:14 +02:00
pub const HTTPOk: StaticResponse = StaticResponse(StatusCode::OK);
pub const HTTPCreated: StaticResponse = StaticResponse(StatusCode::CREATED);
pub const HTTPNoContent: StaticResponse = StaticResponse(StatusCode::NO_CONTENT);
pub const HTTPBadRequest: StaticResponse = StaticResponse(StatusCode::BAD_REQUEST);
pub const HTTPNotFound: StaticResponse = StaticResponse(StatusCode::NOT_FOUND);
pub const HTTPMethodNotAllowed: StaticResponse = StaticResponse(StatusCode::METHOD_NOT_ALLOWED);
2017-10-11 01:03:32 +02:00
pub const HTTPInternalServerError: StaticResponse =
StaticResponse(StatusCode::INTERNAL_SERVER_ERROR);
2017-10-07 06:48:14 +02:00
2017-10-08 06:48:00 +02:00
pub struct StaticResponse(StatusCode);
impl StaticResponse {
2017-10-11 01:03:32 +02:00
pub fn builder(&self) -> Builder {
HttpResponse::builder(self.0)
}
pub fn response(&self) -> HttpResponse {
HttpResponse::new(self.0, Body::Empty)
}
pub fn with_reason(self, reason: &'static str) -> HttpResponse {
let mut resp = HttpResponse::new(self.0, Body::Empty);
resp.set_reason(reason);
resp
2017-10-08 06:48:00 +02:00
}
}
2017-10-07 06:48:14 +02:00
impl<S> RouteHandler<S> for StaticResponse {
2017-10-09 05:16:48 +02:00
fn handle(&self, req: HttpRequest, _: Payload, _: Rc<S>) -> Task {
2017-10-11 01:03:32 +02:00
Task::reply(req, HttpResponse::new(self.0, Body::Empty))
2017-10-07 06:48:14 +02:00
}
}
2017-10-11 01:03:32 +02:00
impl From<StaticResponse> for HttpResponse {
fn from(st: StaticResponse) -> Self {
st.response()
2017-10-07 06:48:14 +02:00
}
}