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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use crate::{AccountCache, CertCache};
use async_trait::async_trait;
use std::fmt::Debug;

pub struct BoxedErrCache<T: Send + Sync> {
    inner: T,
}

impl<T: Send + Sync> BoxedErrCache<T> {
    pub fn new(inner: T) -> Self {
        Self { inner }
    }
    pub fn into_inner(self) -> T {
        self.inner
    }
}

fn box_err(e: impl Debug + 'static) -> Box<dyn Debug> {
    Box::new(e)
}

#[async_trait]
impl<T: CertCache> CertCache for BoxedErrCache<T>
where
    <T as CertCache>::EC: 'static,
{
    type EC = Box<dyn Debug>;
    async fn load_cert(
        &self,
        domains: &[String],
        directory_url: &str,
    ) -> Result<Option<Vec<u8>>, Self::EC> {
        self.inner
            .load_cert(domains, directory_url)
            .await
            .map_err(box_err)
    }

    async fn store_cert(
        &self,
        domains: &[String],
        directory_url: &str,
        cert: &[u8],
    ) -> Result<(), Self::EC> {
        self.inner
            .store_cert(domains, directory_url, cert)
            .await
            .map_err(box_err)
    }
}

#[async_trait]
impl<T: AccountCache> AccountCache for BoxedErrCache<T>
where
    <T as AccountCache>::EA: 'static,
{
    type EA = Box<dyn Debug>;
    async fn load_account(
        &self,
        contact: &[String],
        directory_url: &str,
    ) -> Result<Option<Vec<u8>>, Self::EA> {
        self.inner
            .load_account(contact, directory_url)
            .await
            .map_err(box_err)
    }

    async fn store_account(
        &self,
        contact: &[String],
        directory_url: &str,
        account: &[u8],
    ) -> Result<(), Self::EA> {
        self.inner
            .store_account(contact, directory_url, account)
            .await
            .map_err(box_err)
    }
}