use crate::encryptor::DataEncryptor; use anyhow::Error; use bytes::Bytes; use serde::de::DeserializeOwned; use serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize)] pub struct EncryptionData(Bytes); impl EncryptionData { pub fn from_data(data: &T, encryptor: &E) -> Result { let data = bincode::serialize(data)?; let encryption_data = encryptor.encrypt(Bytes::from(data))?; Ok(EncryptionData(encryption_data)) } pub fn decrypt(self, encryptor: &E) -> Result { let decryption_data = encryptor.decrypt(self.0)?; bincode::deserialize(&decryption_data).map_err(|err| err.into()) } } impl TryFrom for EncryptionData { type Error = Error; fn try_from(value: Bytes) -> Result { bincode::deserialize(&value).map_err(|err| err.into()) } } impl TryFrom for Vec { type Error = Error; fn try_from(value: EncryptionData) -> Result { bincode::serialize(&value).map_err(|err| err.into()) } }