canary 0.3.3

provides ergonomic abstractions for network communication and more
Documentation
use crate::{
    async_snow::{Decrypt, Encrypt},
    Result,
};
use derive_more::From;
use serde::{de::DeserializeOwned, Serialize};

use crate::serialization::formats::{ReadFormat, SendFormat};

#[derive(From)]
/// helper struct that facilitates encryption
pub struct WithCipher<'a, C, F> {
    /// cipher
    pub snow: &'a mut C,
    /// serialization format
    pub format: &'a mut F,
}

impl<C: Encrypt, F: SendFormat> SendFormat for WithCipher<'_, C, F> {
    fn serialize<O: Serialize>(&mut self, obj: &O) -> Result<Vec<u8>> {
        let obj = self.format.serialize(obj)?;
        self.snow.encrypt_packets(obj)
    }
}

impl<C: Decrypt, F: ReadFormat> ReadFormat for WithCipher<'_, C, F> {
    fn deserialize<T>(&mut self, bytes: &[u8]) -> crate::Result<T>
    where
        T: DeserializeOwned,
    {
        let bytes = self.snow.decrypt(bytes)?;
        self.format.deserialize(&bytes)
    }
}