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 67 68 69 70 71 72 73 74 75 76 77
//! Defines helpers for applications that only require a single pipeline (i.e. only one set of
//! middleware for the application).
use borrow_bag::{Append, Handle};
use crate::pipeline::set::{finalize_pipeline_set, new_pipeline_set, PipelineSet};
use crate::pipeline::{NewMiddlewareChain, Pipeline};
/// A `PipelineSet` which contains only a single pipeline.
pub type SinglePipelineSet<C> = PipelineSet<<() as Append<Pipeline<C>>>::Output>;
/// A `Handle` for borrowing the only pipeline from a `SinglePipelineSet`.
pub type SinglePipelineHandle<C> = Handle<Pipeline<C>, <() as Append<Pipeline<C>>>::Navigator>;
/// A pipeline chain which contains only the single pipeline in a `SinglePipelineSet`.
pub type SinglePipelineChain<C> = (SinglePipelineHandle<C>, ());
/// Creates a single pipeline for use in applications with straightforward use cases for
/// middleware.
///
/// Returns instances of the required PipelineHandleChain and PipelineSet types ready for use with
/// `build_router`.
///
/// # Examples
///
/// ```rust
/// # extern crate gotham;
/// # #[macro_use]
/// # extern crate serde_derive;
/// # use gotham::pipeline::single::single_pipeline;
/// # use gotham::pipeline::new_pipeline;
/// # use gotham::router::builder::build_router;
/// # use gotham::middleware::session::NewSessionMiddleware;
/// #
/// # #[derive(Serialize, Deserialize, Default)]
/// # struct Session;
/// #
/// # fn main() {
/// let (chain, pipelines) = single_pipeline(
/// new_pipeline()
/// .add(NewSessionMiddleware::default().with_session_type::<Session>())
/// .build()
/// );
///
/// build_router(chain, pipelines, |route| {
/// // Implementation elided
/// # drop(route);
/// });
/// # }
/// ```
pub fn single_pipeline<C>(c: Pipeline<C>) -> (SinglePipelineChain<C>, SinglePipelineSet<C>)
where
C: NewMiddlewareChain,
{
let pipelines = new_pipeline_set();
let (pipelines, single) = pipelines.add(c);
let pipelines = finalize_pipeline_set(pipelines);
let chain = (single, ());
(chain, pipelines)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::pipeline::new_pipeline;
use crate::router::builder::*;
#[test]
fn test_pipeline_construction() {
let (chain, pipelines) = single_pipeline(new_pipeline().build());
build_router(chain, pipelines, |_route| {});
}
}