1use crate::coding::{Decode, DecodeError, Encode, EncodeError};
6use byteorder::{ReadBytesExt, WriteBytesExt};
7use std::io::{Read, Write};
8
9#[derive(Copy, Clone, Debug, Eq, PartialEq)]
11#[allow(clippy::module_name_repetitions)]
12pub enum CompressionType {
13 None,
17
18 #[cfg(feature = "lz4")]
23 Lz4,
24}
25
26impl Encode for CompressionType {
27 fn encode_into<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
28 match self {
29 Self::None => {
30 writer.write_u8(0)?;
31 }
32
33 #[cfg(feature = "lz4")]
34 Self::Lz4 => {
35 writer.write_u8(1)?;
36 }
37 }
38
39 Ok(())
40 }
41}
42
43impl Decode for CompressionType {
44 fn decode_from<R: Read>(reader: &mut R) -> Result<Self, DecodeError> {
45 let tag = reader.read_u8()?;
46
47 match tag {
48 0 => Ok(Self::None),
49
50 #[cfg(feature = "lz4")]
51 1 => Ok(Self::Lz4),
52
53 tag => Err(DecodeError::InvalidTag(("CompressionType", tag))),
54 }
55 }
56}
57
58impl std::fmt::Display for CompressionType {
59 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60 write!(
61 f,
62 "{}",
63 match self {
64 Self::None => "none",
65
66 #[cfg(feature = "lz4")]
67 Self::Lz4 => "lz4",
68 }
69 )
70 }
71}
72
73#[cfg(test)]
74mod tests {
75 use super::*;
76 use test_log::test;
77
78 #[test]
79 fn compression_serialize_none() {
80 let serialized = CompressionType::None.encode_into_vec();
81 assert_eq!(1, serialized.len());
82 }
83
84 #[cfg(feature = "lz4")]
85 mod lz4 {
86 use super::*;
87 use test_log::test;
88
89 #[test]
90 fn compression_serialize_none() {
91 let serialized = CompressionType::Lz4.encode_into_vec();
92 assert_eq!(1, serialized.len());
93 }
94 }
95}