use std::borrow::Cow;
use crate::Result;
use async_trait::async_trait;
#[async_trait]
pub trait CompressionStrategy: std::fmt::Debug {
async fn compress<'a>(&self, data: Cow<'a, [u8]>) -> Result<Cow<'a, [u8]>>;
async fn decompress<'a>(&self, value: Cow<'a, [u8]>) -> Result<Cow<'a, [u8]>>;
}
#[async_trait]
impl<T: CompressionStrategy + Sync + Send> CompressionStrategy for Option<T> {
async fn compress<'a>(&self, data: Cow<'a, [u8]>) -> Result<Cow<'a, [u8]>> {
match self {
Some(compressor) => compressor.compress(data).await,
None => Ok(data),
}
}
async fn decompress<'a>(&self, value: Cow<'a, [u8]>) -> Result<Cow<'a, [u8]>> {
match self {
Some(compressor) => compressor.decompress(value).await,
None => Ok(value),
}
}
}