debian_packaging/deb/
mod.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5/*! Interfaces for .deb package files.
6
7The .deb file specification lives at <https://manpages.debian.org/unstable/dpkg-dev/deb.5.en.html>.
8*/
9
10use {crate::error::Result, std::io::Read};
11
12pub mod builder;
13pub mod reader;
14
15/// Compression format to apply to `.deb` files.
16pub enum DebCompression {
17    /// Do not compress contents of `.deb` files.
18    Uncompressed,
19    /// Compress as `.gz` files.
20    Gzip,
21    /// Compress as `.xz` files using a specified compression level.
22    Xz(u32),
23    /// Compress as `.zst` files using a specified compression level.
24    Zstandard(i32),
25}
26
27impl DebCompression {
28    /// Obtain the filename extension for this compression format.
29    pub fn extension(&self) -> &'static str {
30        match self {
31            Self::Uncompressed => "",
32            Self::Gzip => ".gz",
33            Self::Xz(_) => ".xz",
34            Self::Zstandard(_) => ".zst",
35        }
36    }
37
38    /// Compress input data from a reader.
39    pub fn compress(&self, reader: &mut impl Read) -> Result<Vec<u8>> {
40        let mut buffer = vec![];
41
42        match self {
43            Self::Uncompressed => {
44                std::io::copy(reader, &mut buffer)?;
45            }
46            Self::Gzip => {
47                let header = libflate::gzip::HeaderBuilder::new().finish();
48
49                let mut encoder = libflate::gzip::Encoder::with_options(
50                    &mut buffer,
51                    libflate::gzip::EncodeOptions::new().header(header),
52                )?;
53                std::io::copy(reader, &mut encoder)?;
54                encoder.finish().into_result()?;
55            }
56            Self::Xz(level) => {
57                let mut encoder = xz2::write::XzEncoder::new(buffer, *level);
58                std::io::copy(reader, &mut encoder)?;
59                buffer = encoder.finish()?;
60            }
61            Self::Zstandard(level) => {
62                let mut encoder = zstd::Encoder::new(buffer, *level)?;
63                std::io::copy(reader, &mut encoder)?;
64                buffer = encoder.finish()?;
65            }
66        }
67
68        Ok(buffer)
69    }
70}