1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#![allow(non_snake_case)]
use crate::{Response, Status};
/// Represents "can be handlers' return value".
///
/// <br>
///
/// *example.rs*
/// ```no_run
/// use ohkami::prelude::*;
///
/// struct MyResponse {
/// message: String,
/// }
/// impl IntoResponse for MyResponse {
/// fn into_response(self) -> Response {
/// Response::OK().with_text(self.message)
/// }
/// }
///
/// async fn handler() -> MyResponse {
/// MyResponse {
/// message: String::from("Hello!")
/// }
/// }
///
/// #[tokio::main]
/// async fn main() {
/// Ohkami::new(
/// "/".GET(handler)
/// ).howl("localhost:5050").await
/// }
/// ```
pub trait IntoResponse {
fn into_response(self) -> Response;
}
impl IntoResponse for Response {
#[inline] fn into_response(self) -> Response {
self
}
}
impl IntoResponse for Status {
#[inline(always)] fn into_response(self) -> Response {
Response::of(self)
}
}
impl<T:IntoResponse, E:IntoResponse> IntoResponse for Result<T, E> {
#[inline(always)] fn into_response(self) -> Response {
match self {
Ok(ok) => ok.into_response(),
Err(e) => e.into_response(),
}
}
}
impl IntoResponse for std::convert::Infallible {
#[cold]
fn into_response(self) -> Response {
unsafe {std::hint::unreachable_unchecked()}
}
}