1use std::future::Future;
20
21use super::context::Context;
22use super::handler::{Handler, Settle};
23
24pub trait Layer<H> {
26 type Handler;
28
29 fn layer(&self, inner: H) -> Self::Handler;
31}
32
33pub trait BlanketLayer: Send + Sync {
45 fn apply<M, C, S, H>(&self, handler: H) -> impl Handler<M, C, S> + 'static
48 where
49 M: Send + Sync + 'static,
50 C: Send + 'static,
51 S: Send + Sync + 'static,
52 H: Handler<M, C, S> + 'static;
53}
54
55pub trait HandlerExt<M, C = (), S = ()>: Handler<M, C, S> + Sized {
57 fn with<L>(self, layer: L) -> L::Handler
59 where
60 L: Layer<Self>,
61 {
62 layer.layer(self)
63 }
64}
65
66impl<M, C, S, H> HandlerExt<M, C, S> for H where H: Handler<M, C, S> {}
67
68#[derive(Debug, Clone, Copy, Default)]
71pub struct Identity;
72
73impl<H> Layer<H> for Identity {
74 type Handler = H;
75
76 fn layer(&self, inner: H) -> H {
77 inner
78 }
79}
80
81impl BlanketLayer for Identity {
82 fn apply<M, C, S, H>(&self, handler: H) -> impl Handler<M, C, S> + 'static
83 where
84 M: Send + Sync + 'static,
85 C: Send + 'static,
86 S: Send + Sync + 'static,
87 H: Handler<M, C, S> + 'static,
88 {
89 handler
90 }
91}
92
93#[derive(Debug, Clone, Copy, Default)]
97pub struct Stack<Inner, Outer> {
98 inner: Inner,
99 outer: Outer,
100}
101
102impl<Inner, Outer> Stack<Inner, Outer> {
103 #[must_use]
105 pub fn new(inner: Inner, outer: Outer) -> Self {
106 Self { inner, outer }
107 }
108}
109
110impl<H, Inner, Outer> Layer<H> for Stack<Inner, Outer>
111where
112 Inner: Layer<H>,
113 Outer: Layer<Inner::Handler>,
114{
115 type Handler = Outer::Handler;
116
117 fn layer(&self, inner: H) -> Self::Handler {
118 self.outer.layer(self.inner.layer(inner))
119 }
120}
121
122impl<Inner, Outer> BlanketLayer for Stack<Inner, Outer>
123where
124 Inner: BlanketLayer,
125 Outer: BlanketLayer,
126{
127 fn apply<M, C, S, H>(&self, handler: H) -> impl Handler<M, C, S> + 'static
128 where
129 M: Send + Sync + 'static,
130 C: Send + 'static,
131 S: Send + Sync + 'static,
132 H: Handler<M, C, S> + 'static,
133 {
134 self.outer
136 .apply::<M, C, S, _>(self.inner.apply::<M, C, S, _>(handler))
137 }
138}
139
140pub mod layers {
142 use tracing::{debug, info, instrument, warn};
143
144 use super::super::handler::HandlerResult;
145 use super::{BlanketLayer, Context, Future, Handler, Layer, Settle};
146
147 #[derive(Debug, Clone, Default)]
150 pub struct TracingLayer {
151 target: Option<&'static str>,
152 }
153
154 impl TracingLayer {
155 #[must_use]
157 pub const fn with_target(target: &'static str) -> Self {
158 Self {
159 target: Some(target),
160 }
161 }
162 }
163
164 impl<H> Layer<H> for TracingLayer {
165 type Handler = TracingHandler<H>;
166
167 fn layer(&self, inner: H) -> Self::Handler {
168 TracingHandler {
169 inner,
170 target: self.target,
171 }
172 }
173 }
174
175 impl BlanketLayer for TracingLayer {
176 fn apply<M, C, S, H>(&self, handler: H) -> impl Handler<M, C, S> + 'static
177 where
178 M: Send + Sync + 'static,
179 C: Send + 'static,
180 S: Send + Sync + 'static,
181 H: Handler<M, C, S> + 'static,
182 {
183 self.layer(handler)
184 }
185 }
186
187 #[derive(Debug, Clone)]
189 pub struct TracingHandler<H> {
190 inner: H,
191 target: Option<&'static str>,
192 }
193
194 impl<M, C, S, H> Handler<M, C, S> for TracingHandler<H>
195 where
196 M: Sync,
197 C: Send,
198 S: Send + Sync,
199 H: Handler<M, C, S>,
200 {
201 #[instrument(level = "trace", skip(self, msg, ctx), fields(target = self.target))]
202 fn handle(
203 &self,
204 msg: &M,
205 ctx: &mut Context<'_, C, S>,
206 ) -> impl Future<Output = Settle> + Send {
207 async move {
208 debug!(target: "ruststream::dispatch", "delivery received");
209 let settle = self.inner.handle(msg, ctx).await;
211 match settle.outcome() {
212 HandlerResult::Ack => {
213 info!(target: "ruststream::dispatch", "handler ack");
214 }
215 HandlerResult::Nack { requeue } => {
216 warn!(target: "ruststream::dispatch", requeue, "handler nack");
217 }
218 HandlerResult::NackAfter { delay } => {
219 warn!(target: "ruststream::dispatch", ?delay, "handler delayed nack");
220 }
221 }
222 settle
223 }
224 }
225 }
226}