cindy 0.2.1

Managing infrastructure at breakneck speed.
Documentation
//! Decompress a byte stream with a pure-Rust codec.
//!
//! The decompression half of the de/compression pair, factored out of
//! [`unarchive`](super::unarchive) so the codec logic lives in one
//! place and is independently usable. The
//! [`unarchive`](super::unarchive) module reuses it through
//! `decompress::decompress::inner` for the tar-based formats.
//!
//! All codecs are pure Rust — `flate2`/miniz_oxide (gzip), `lzma-rs`
//! (xz), `ruzstd` (zstd) — so no C or dynamic libraries are pulled in.

use std::io::Read as _;

// Only referenced by the macro-generated RPC/dispatch code under the
// `remote`/`orchestrator` features; unused in the plain build.
#[allow(unused_imports)]
use crate as cindy;
use crate::Context;

use super::Codec;

/// Decompress an in-memory byte buffer on the remote machine.
///
/// In-crate callers (e.g. [`unarchive`](super::unarchive)) invoke this
/// directly as `decompress::decompress::inner(data, codec)`; the
/// orchestrator can also drive it over RPC.
#[crate::remote]
pub fn decompress(data: Vec<u8>, codec: Codec) -> crate::Result<Vec<u8>> {
    let mut out = Vec::new();
    match codec {
        Codec::Store => out = data,
        Codec::Gzip => {
            flate2::read::GzDecoder::new(&data[..])
                .read_to_end(&mut out)
                .context("gzip decompression failed")?;
        }
        Codec::Xz => {
            let mut reader = std::io::BufReader::new(&data[..]);
            lzma_rs::xz_decompress(&mut reader, &mut out).context("xz decompression failed")?;
        }
        Codec::Zstd => {
            let mut decoder = ruzstd::decoding::StreamingDecoder::new(&data[..]).map_err(|e| {
                anyhow_serde::Error::msg(format!("Couldn't start zstd decoder: {e}"))
            })?;
            decoder
                .read_to_end(&mut out)
                .context("zstd decompression failed")?;
        }
    }
    Ok(out)
}