wireframe 0.4.0-alpha1

Simplify building servers and clients for custom binary protocols.
Documentation
//! Handler and middleware type aliases for `WireframeApp`.
//!
//! [`Handler`] and [`ResponseHandler`] are the buffered route handler shapes,
//! and [`Route`] records which of the two a registration used.
//! [`StreamingHandler`] is the opt-in shape for streaming routes: it receives
//! [`RequestParts`] and a [`RequestBodyStream`], and returns an optional
//! [`Bytes`] reply. Streaming handlers bypass the middleware chain, because
//! middleware transforms a whole payload while a streaming handler owns its
//! body incrementally.

use std::{future::Future, pin::Pin, sync::Arc};

use bytes::Bytes;

use super::envelope::{Envelope, Packet};
use crate::{
    middleware::{HandlerService, Transform},
    request::{RequestBodyStream, RequestParts},
    response::Response,
};

/// Alias for asynchronous route handlers.
///
/// A `Handler` wraps an `Arc` to a function returning a [`Future`]. It mutates
/// the request envelope in place, and the route service replies with the
/// envelope's final payload.
pub type Handler<E> = Arc<dyn Fn(&E) -> Pin<Box<dyn Future<Output = ()> + Send>> + Send + Sync>;

/// Alias for route handlers that build an explicit [`Response`].
///
/// Unlike [`Handler`], the returned future owns the frames the connection
/// streams to the client. Frames are [`Envelope`] values so they pass through
/// the connection's serializer, codec, and protocol hooks. Handlers may return
/// [`Response::MultiPacket`] to spawn a producer and stream frames
/// asynchronously, which is how a route keeps producing after it returns.
///
/// The returned future may borrow the request for as long as the handler call
/// lasts, so a handler can read request data after an `await` without copying
/// it first.
pub type ResponseHandler<E> = Arc<
    dyn for<'request> Fn(
            &'request E,
        )
            -> Pin<Box<dyn Future<Output = Response<Envelope, ()>> + Send + 'request>>
        + Send
        + Sync,
>;

/// Registered route kind stored by [`WireframeApp`].
///
/// [`WireframeApp`]: super::builder::WireframeApp
pub(crate) enum Route<E: Packet> {
    /// A unit handler mutates the request envelope in place.
    Unit(Handler<E>),
    /// A response handler returns the frames to stream to the client.
    Response(ResponseHandler<E>),
}

impl<E: Packet> Clone for Route<E> {
    /// Clone the route's shared handler handle, not the handler itself.
    ///
    /// Both route kinds store `Arc` aliases, so a cloned route points at the
    /// same handler. The compatibility path uses this to own a route it only
    /// borrowed; preparation consumes the original and never clones.
    fn clone(&self) -> Self {
        match self {
            Self::Unit(handler) => Self::Unit(Arc::clone(handler)),
            Self::Response(handler) => Self::Response(Arc::clone(handler)),
        }
    }
}

/// Alias for handlers that consume a streaming request body.
///
/// The handler receives routing metadata separately from the body and returns
/// an optional response payload. The payload is [`Bytes`] rather than
/// `Vec<u8>`: ADR 008 makes the zero-copy container the stable public payload
/// type, and a new public surface must not add another `Vec<u8>` hand-off that
/// the migration would later have to remove.
/// Returning `None` suppresses the response frame. Streaming handlers do not
/// run the middleware chain: middleware sees a whole payload, while a
/// streaming handler owns the body incrementally.
pub type StreamingHandler = Arc<
    dyn Fn(RequestParts, RequestBodyStream) -> Pin<Box<dyn Future<Output = Option<Bytes>> + Send>>
        + Send
        + Sync,
>;

/// Trait representing middleware components.
pub trait Middleware<E: Packet>:
    Transform<HandlerService<E>, Output = HandlerService<E>> + Send + Sync
{
}

impl<E: Packet, T> Middleware<E> for T where
    T: Transform<HandlerService<E>, Output = HandlerService<E>> + Send + Sync
{
}

#[cfg(test)]
mod tests {
    //! Compile-time and runtime coverage for handler lifetime bounds.

    use super::*;

    /// Echo the request's payload back after yielding once.
    ///
    /// The function reads the request after an `await`, so installing it as a
    /// [`ResponseHandler`] only type-checks because the alias ties the returned
    /// future's lifetime to the borrowed request.
    async fn echo_after_yield(request: &Envelope) -> Response<Envelope, ()> {
        tokio::task::yield_now().await;
        Response::Single(Envelope::new(request.id, None, request.payload.clone()))
    }

    /// A response handler may read request data after an `await` without
    /// copying it first.
    #[tokio::test]
    async fn response_handler_may_borrow_the_request() {
        let handler: ResponseHandler<Envelope> =
            Arc::new(|request: &Envelope| Box::pin(echo_after_yield(request)));
        let request = Envelope::new(7, None, vec![1]);

        let response = handler(&request).await;

        assert!(
            matches!(response, Response::Single(frame) if frame.id == 7),
            "the handler must see the borrowed request after awaiting"
        );
    }
}