webrune 0.1.2

A composable web server.
Documentation
use crate::{FromRequest, Request};
use crate::{IntoResponse, Response};
use http_body_util::Full;
use hyper::body::Bytes;
use std::fmt::{Display, Formatter};

/// An empty request or response body.
///
/// `Empty` represents the absence of a body.
///
/// - **As a response**: produces an HTTP response with an empty body.
///
/// This is useful for endpoints that return responses with no content.
///
/// # Examples
///
/// ## Empty response
///
/// ```ignore
/// async fn handler(request: Request, state: ()) -> Result<Empty, ()> {
///     Ok(Empty)
/// }
/// ```
///
pub struct Empty;

/// Converts `Empty` into an HTTP response with an empty body.
impl IntoResponse for Empty {
    fn into_response(self) -> Response {
        Response::new(Full::new(Bytes::new()))
    }
}

impl Display for Empty {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "")
    }
}

/// Extracts an empty body from a request.
///
/// This extractor always succeeds and discards the request body.
impl FromRequest for Empty {
    type Error = Empty;
    type Output = Self;

    async fn from_request(_: Request) -> Result<Self, Self::Error> {
        Ok(Empty)
    }
}