2019-11-29 13:09:47 -08:00
|
|
|
use super::Layer;
|
2019-05-09 08:44:40 -07:00
|
|
|
use std::fmt;
|
2019-02-27 12:28:42 -08:00
|
|
|
|
|
|
|
/// Two middlewares chained together.
|
2019-05-09 08:44:40 -07:00
|
|
|
#[derive(Clone)]
|
2019-04-09 13:09:44 -07:00
|
|
|
pub struct Stack<Inner, Outer> {
|
2019-02-27 12:28:42 -08:00
|
|
|
inner: Inner,
|
|
|
|
outer: Outer,
|
|
|
|
}
|
|
|
|
|
2019-04-09 13:09:44 -07:00
|
|
|
impl<Inner, Outer> Stack<Inner, Outer> {
|
|
|
|
/// Create a new `Stack`.
|
2019-02-27 12:28:42 -08:00
|
|
|
pub fn new(inner: Inner, outer: Outer) -> Self {
|
2019-04-09 13:09:44 -07:00
|
|
|
Stack { inner, outer }
|
2019-02-27 12:28:42 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-11 13:38:45 -07:00
|
|
|
impl<S, Inner, Outer> Layer<S> for Stack<Inner, Outer>
|
2019-02-27 12:28:42 -08:00
|
|
|
where
|
2019-04-11 13:38:45 -07:00
|
|
|
Inner: Layer<S>,
|
|
|
|
Outer: Layer<Inner::Service>,
|
2019-02-27 12:28:42 -08:00
|
|
|
{
|
|
|
|
type Service = Outer::Service;
|
|
|
|
|
2019-04-11 13:38:45 -07:00
|
|
|
fn layer(&self, service: S) -> Self::Service {
|
|
|
|
let inner = self.inner.layer(service);
|
2019-02-28 14:34:00 -08:00
|
|
|
|
2019-04-11 13:38:45 -07:00
|
|
|
self.outer.layer(inner)
|
2019-02-27 12:28:42 -08:00
|
|
|
}
|
|
|
|
}
|
2019-05-09 08:44:40 -07:00
|
|
|
|
|
|
|
impl<Inner, Outer> fmt::Debug for Stack<Inner, Outer>
|
|
|
|
where
|
|
|
|
Inner: fmt::Debug,
|
|
|
|
Outer: fmt::Debug,
|
|
|
|
{
|
2019-11-29 13:09:47 -08:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-05-09 08:44:40 -07:00
|
|
|
// The generated output of nested `Stack`s is very noisy and makes
|
|
|
|
// it harder to understand what is in a `ServiceBuilder`.
|
|
|
|
//
|
|
|
|
// Instead, this output is designed assuming that a `Stack` is
|
|
|
|
// usually quite nested, and inside a `ServiceBuilder`. Therefore,
|
|
|
|
// this skips using `f.debug_struct()`, since each one would force
|
|
|
|
// a new layer of indentation.
|
|
|
|
//
|
|
|
|
// - In compact mode, a nested stack ends up just looking like a flat
|
|
|
|
// list of layers.
|
|
|
|
//
|
|
|
|
// - In pretty mode, while a newline is inserted between each layer,
|
|
|
|
// the `DebugStruct` used in the `ServiceBuilder` will inject padding
|
|
|
|
// to that each line is at the same indentation level.
|
|
|
|
//
|
|
|
|
// Also, the order of [outer, inner] is important, since it reflects
|
|
|
|
// the order that the layers were added to the stack.
|
|
|
|
if f.alternate() {
|
|
|
|
// pretty
|
|
|
|
write!(f, "{:#?},\n{:#?}", self.outer, self.inner)
|
|
|
|
} else {
|
|
|
|
write!(f, "{:?}, {:?}", self.outer, self.inner)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|