photon_protocol/compressor/
lz4.rs1use bytes::BytesMut;
2
3use crate::ports::compress::{CompressionError, Compressor};
4
5#[derive(Clone, Debug)]
6pub struct Lz4Compressor;
7
8impl Lz4Compressor {
9 pub const NAME: &str = "lz4";
10}
11
12impl Compressor for Lz4Compressor {
13 fn compress(&self, input: &[u8], output: &mut BytesMut) -> Result<(), CompressionError> {
14 let compressed = lz4_flex::compress_prepend_size(input);
15 output.extend_from_slice(&compressed);
16
17 Ok(())
18 }
19
20 fn decompress(&self, input: &[u8], output: &mut BytesMut) -> Result<(), CompressionError> {
21 let decompressed = lz4_flex::decompress_size_prepended(input).map_err(|_| {
22 CompressionError::CorruptPayload {
23 compressor_name: self.name().to_owned(),
24 }
25 })?;
26
27 output.extend_from_slice(&decompressed);
28
29 Ok(())
30 }
31
32 fn name(&self) -> &'static str {
33 Self::NAME
34 }
35}