lindera_dictionary/
decompress.rs

1use std::io::Read;
2
3use flate2::read::{DeflateDecoder, GzDecoder, ZlibDecoder};
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
7pub enum Algorithm {
8    Deflate,
9    Zlib,
10    Gzip,
11    Raw,
12}
13
14#[derive(Debug, Clone, Serialize, Deserialize)]
15pub struct CompressedData {
16    algorithm: Algorithm,
17    data: Vec<u8>,
18}
19
20impl CompressedData {
21    pub fn new(algorithm: Algorithm, data: Vec<u8>) -> Self {
22        CompressedData { algorithm, data }
23    }
24}
25
26pub fn decompress(data: CompressedData) -> anyhow::Result<Vec<u8>> {
27    match data.algorithm {
28        Algorithm::Deflate => {
29            let mut decoder = DeflateDecoder::new(data.data.as_slice());
30            let mut output_data = Vec::new();
31            decoder.read_to_end(&mut output_data)?;
32            Ok(output_data)
33        }
34        Algorithm::Zlib => {
35            let mut decoder = ZlibDecoder::new(data.data.as_slice());
36            let mut output_data = Vec::new();
37            decoder.read_to_end(&mut output_data)?;
38            Ok(output_data)
39        }
40        Algorithm::Gzip => {
41            let mut decoder = GzDecoder::new(data.data.as_slice());
42            let mut output_data = Vec::new();
43            decoder.read_to_end(&mut output_data)?;
44            Ok(output_data)
45        }
46        Algorithm::Raw => Ok(data.data),
47    }
48}