1
use crate::{error::SendSyncError, ErrorKind};
2

            
3
/// A provider of encryption for blocks of data.
4
pub trait Vault: std::fmt::Debug + Send + Sync + 'static {
5
    /// The error type that the vault can produce.
6
    type Error: SendSyncError;
7

            
8
    /// Encrypts `payload`, returning a new buffer that contains all information
9
    /// necessary to decrypt it in the future.
10
    fn encrypt(&self, payload: &[u8]) -> Result<Vec<u8>, Self::Error>;
11

            
12
    /// Decrypts a previously encrypted `payload`, returning the decrypted
13
    /// information.
14
    fn decrypt(&self, payload: &[u8]) -> Result<Vec<u8>, Self::Error>;
15
}
16

            
17
/// A [`Vault`] that can be boxed.
18
pub trait AnyVault: std::fmt::Debug + Send + Sync + 'static {
19
    /// Encrypts `payload`, returning a new buffer that contains all information
20
    /// necessary to decrypt it in the future.
21
    fn encrypt(&self, payload: &[u8]) -> Result<Vec<u8>, crate::Error>;
22

            
23
    /// Decrypts a previously encrypted `payload`, returning the decrypted
24
    /// information.
25
    fn decrypt(&self, payload: &[u8]) -> Result<Vec<u8>, crate::Error>;
26
}
27

            
28
impl<T, E> AnyVault for T
29
where
30
    T: Vault<Error = E> + std::fmt::Debug,
31
    E: SendSyncError,
32
{
33
12008
    fn encrypt(&self, payload: &[u8]) -> Result<Vec<u8>, crate::Error> {
34
12008
        self.encrypt(payload)
35
12008
            .map_err(|err| crate::Error::from(ErrorKind::Vault(Box::new(err))))
36
12008
    }
37

            
38
75786
    fn decrypt(&self, payload: &[u8]) -> Result<Vec<u8>, crate::Error> {
39
75786
        Vault::decrypt(self, payload)
40
75786
            .map_err(|err| crate::Error::from(ErrorKind::Vault(Box::new(err))))
41
75786
    }
42
}