use std::fs::File;
use std::io;
use std::io::{BufReader, Read, Write};
use std::path::PathBuf;
use zstd::stream::{Decoder, Encoder};
const C_LEVEL: i32 = 3;

pub fn decompress_reader(file: impl Into<PathBuf>) -> Result<impl Read, io::Error> {
    Ok(Decoder::with_buffer(BufReader::new(File::open(
        file.into(),
    )?))?)
}

pub fn decompress_file_vec(file: impl Into<PathBuf>) -> Result<Vec<u8>, io::Error> {
    let mut buf = Vec::new();
    decompress_reader(file)?.read_to_end(&mut buf)?;
    Ok(buf)
}

pub fn compress_writer(file: impl Into<PathBuf>) -> Result<impl Write, io::Error> {
    // has an internal buffer so we dont need bufwriter
    Ok(Encoder::new(File::create(file.into())?, C_LEVEL)?.auto_finish())
}
