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
// Copyright (c) Mysten Labs, Inc.
// Modifications Copyright (c) 2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use tower::Layer;

use super::{Callback, MakeCallbackHandler};

/// [`Layer`] that adds callbacks to a [`Service`].
///
/// See the [module docs](crate::callback) for more details.
///
/// [`Layer`]: tower::layer::Layer
/// [`Service`]: tower::Service
#[derive(Debug, Copy, Clone)]
pub struct CallbackLayer<M> {
    pub(crate) make_handler: M,
}

impl<M> CallbackLayer<M> {
    /// Create a new [`CallbackLayer`] using the given [`MakeCallbackHandler`].
    pub fn new(make_handler: M) -> Self
    where
        M: MakeCallbackHandler,
    {
        Self { make_handler }
    }
}

impl<S, M> Layer<S> for CallbackLayer<M>
where
    M: Clone,
{
    type Service = Callback<S, M>;

    fn layer(&self, inner: S) -> Self::Service {
        Callback {
            inner,
            make_callback_handler: self.make_handler.clone(),
        }
    }
}