mas_storage/
utils.rs

1// Copyright 2024, 2025 New Vector Ltd.
2// Copyright 2023, 2024 The Matrix.org Foundation C.I.C.
3//
4// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
5// Please see LICENSE files in the repository root for full details.
6
7//! Wrappers and useful type aliases
8
9/// A wrapper which is used to map the error type of a repository to another
10pub struct MapErr<R, F> {
11    pub(crate) inner: R,
12    pub(crate) mapper: F,
13    _private: (),
14}
15
16impl<R, F> MapErr<R, F> {
17    /// Create a new [`MapErr`] wrapper from an inner repository and a mapper
18    /// function
19    #[must_use]
20    pub fn new(inner: R, mapper: F) -> Self {
21        Self {
22            inner,
23            mapper,
24            _private: (),
25        }
26    }
27}
28
29/// A macro to implement a repository trait for the [`MapErr`] wrapper and for
30/// [`Box<R>`]
31#[macro_export]
32macro_rules! repository_impl {
33    ($repo_trait:ident:
34        $(
35            async fn $method:ident (
36                &mut self
37                $(, $arg:ident: $arg_ty:ty )*
38                $(,)?
39            ) -> Result<$ret_ty:ty, Self::Error>;
40        )*
41    ) => {
42        #[::async_trait::async_trait]
43        impl<R: ?Sized> $repo_trait for ::std::boxed::Box<R>
44        where
45            R: $repo_trait,
46        {
47            type Error = <R as $repo_trait>::Error;
48
49            $(
50                async fn $method (&mut self $(, $arg: $arg_ty)*) -> Result<$ret_ty, Self::Error> {
51                    (**self).$method ( $($arg),* ).await
52                }
53            )*
54        }
55
56        #[::async_trait::async_trait]
57        impl<R, F, E> $repo_trait for $crate::MapErr<R, F>
58        where
59            R: $repo_trait,
60            F: FnMut(<R as $repo_trait>::Error) -> E + ::std::marker::Send + ::std::marker::Sync,
61        {
62            type Error = E;
63
64            $(
65                async fn $method (&mut self $(, $arg: $arg_ty)*) -> Result<$ret_ty, Self::Error> {
66                    self.inner.$method ( $($arg),* ).await.map_err(&mut self.mapper)
67                }
68            )*
69        }
70    };
71}