mas_context/
layer.rs

1// Copyright 2025 New Vector Ltd.
2//
3// SPDX-License-Identifier: AGPL-3.0-only
4// Please see LICENSE in the repository root for full details.
5
6use std::borrow::Cow;
7
8use tower_layer::Layer;
9use tower_service::Service;
10
11use crate::LogContextService;
12
13/// A layer which creates a log context for each request.
14pub struct LogContextLayer<R> {
15    tagger: fn(&R) -> Cow<'static, str>,
16}
17
18impl<R> Clone for LogContextLayer<R> {
19    fn clone(&self) -> Self {
20        Self {
21            tagger: self.tagger,
22        }
23    }
24}
25
26impl<R> LogContextLayer<R> {
27    pub fn new(tagger: fn(&R) -> Cow<'static, str>) -> Self {
28        Self { tagger }
29    }
30}
31
32impl<S, R> Layer<S> for LogContextLayer<R>
33where
34    S: Service<R>,
35{
36    type Service = LogContextService<S, R>;
37
38    fn layer(&self, inner: S) -> Self::Service {
39        LogContextService::new(inner, self.tagger)
40    }
41}