1use crate::coding::{Decode, Encode};
6use byteorder::{ReadBytesExt, WriteBytesExt};
7use std::io::{Read, Write};
8
9#[derive(Copy, Clone, Debug, Eq, PartialEq)]
11pub enum CompressionType {
12 None,
16
17 #[cfg(feature = "lz4")]
22 Lz4,
23}
24
25impl Encode for CompressionType {
26 fn encode_into<W: Write>(&self, writer: &mut W) -> Result<(), crate::Error> {
27 match self {
28 Self::None => {
29 writer.write_u8(0)?;
30 }
31
32 #[cfg(feature = "lz4")]
33 Self::Lz4 => {
34 writer.write_u8(1)?;
35 }
36 }
37
38 Ok(())
39 }
40}
41
42impl Decode for CompressionType {
43 fn decode_from<R: Read>(reader: &mut R) -> Result<Self, crate::Error> {
44 let tag = reader.read_u8()?;
45
46 match tag {
47 0 => Ok(Self::None),
48
49 #[cfg(feature = "lz4")]
50 1 => Ok(Self::Lz4),
51
52 tag => Err(crate::Error::InvalidTag(("CompressionType", tag))),
53 }
54 }
55}
56
57#[cfg_attr(test, mutants::skip)]
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}