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
use crate::{error::SendSyncError, ErrorKind};

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

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

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

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

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

impl<T, E> AnyVault for T
where
    T: Vault<Error = E> + std::fmt::Debug,
    E: SendSyncError,
{
    fn encrypt(&self, payload: &[u8]) -> Result<Vec<u8>, crate::Error> {
        self.encrypt(payload)
            .map_err(|err| crate::Error::from(ErrorKind::Vault(Box::new(err))))
    }

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