photon_protocol/compressor/
mod.rs1pub mod brotli;
2pub mod lz4;
3pub mod noop;
4pub mod zstd;
5
6pub use self::brotli::BrotliCompressor;
7pub use self::lz4::Lz4Compressor;
8pub use self::noop::NoopCompressor;
9pub use self::zstd::ZstdCompressor;
10
11use bytes::BytesMut;
12
13use crate::ports::compress::{CompressionError, Compressor};
14
15#[derive(Clone, Copy, Debug)]
16pub enum CompressorKind {
17 Zstd { level: i32 },
18 Lz4,
19 Brotli { quality: u32 },
20 Noop,
21}
22
23impl Default for CompressorKind {
24 fn default() -> Self {
25 Self::Zstd { level: 3 }
26 }
27}
28
29impl CompressorKind {
30 pub fn name(&self) -> &'static str {
31 Compressor::name(self)
32 }
33
34 pub fn all_variants() -> Vec<Self> {
35 vec![
36 Self::Zstd { level: 3 },
37 Self::Lz4,
38 Self::Brotli { quality: 4 },
39 Self::Noop,
40 ]
41 }
42}
43
44impl Compressor for CompressorKind {
45 fn compress(&self, input: &[u8], output: &mut BytesMut) -> Result<(), CompressionError> {
46 match self {
47 Self::Zstd { level } => ZstdCompressor::new(*level).compress(input, output),
48 Self::Lz4 => Lz4Compressor.compress(input, output),
49 Self::Brotli { quality } => BrotliCompressor::new(*quality).compress(input, output),
50 Self::Noop => NoopCompressor.compress(input, output),
51 }
52 }
53
54 fn decompress(&self, input: &[u8], output: &mut BytesMut) -> Result<(), CompressionError> {
55 match self {
56 Self::Zstd { level } => ZstdCompressor::new(*level).decompress(input, output),
57 Self::Lz4 => Lz4Compressor.decompress(input, output),
58 Self::Brotli { quality } => BrotliCompressor::new(*quality).decompress(input, output),
59 Self::Noop => NoopCompressor.decompress(input, output),
60 }
61 }
62
63 fn name(&self) -> &'static str {
64 match self {
65 Self::Zstd { .. } => ZstdCompressor::NAME,
66 Self::Lz4 => Lz4Compressor::NAME,
67 Self::Brotli { .. } => BrotliCompressor::NAME,
68 Self::Noop => NoopCompressor::NAME,
69 }
70 }
71}