composable_tower_http/extension/
layer.rs

1use tower::Layer;
2
3use super::service::ExtensionService;
4
5#[derive(Debug, Clone)]
6pub struct ExtensionLayer<Ex> {
7    extractor: Ex,
8}
9
10impl<Ex> ExtensionLayer<Ex> {
11    pub fn new(extractor: Ex) -> Self {
12        Self { extractor }
13    }
14}
15
16impl<S, Ex> Layer<S> for ExtensionLayer<Ex>
17where
18    Ex: Clone,
19{
20    type Service = ExtensionService<S, Ex>;
21
22    fn layer(&self, service: S) -> Self::Service {
23        ExtensionService::new(service, self.extractor.clone())
24    }
25}
26
27pub trait ExtensionLayerExt: Sized {
28    fn extension_layer(self) -> ExtensionLayer<Self>;
29}
30
31impl<T> ExtensionLayerExt for T
32where
33    T: Sized + Clone,
34{
35    fn extension_layer(self) -> ExtensionLayer<Self> {
36        ExtensionLayer::new(self)
37    }
38}