alpm_compress/compression/
settings.rs

1//! Settings for a compression encoder.
2
3use alpm_types::CompressionAlgorithmFileExtension;
4
5use crate::compression::{
6    Bzip2CompressionLevel,
7    GzipCompressionLevel,
8    XzCompressionLevel,
9    ZstdCompressionLevel,
10};
11
12/// The amount of threads to use when compressing using zstd.
13///
14/// The default (1) adheres to the one selected by the [zstd] executable.
15/// If the custom amount of `0` is used, all available physical CPU cores are used.
16///
17/// [zstd]: https://man.archlinux.org/man/zstd.1
18#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
19pub struct ZstdThreads(pub(crate) u32);
20
21impl ZstdThreads {
22    /// Creates a new [`ZstdThreads`] from a [`u32`].
23    pub fn new(threads: u32) -> Self {
24        Self(threads)
25    }
26
27    /// Creates a new [`ZstdThreads`] which uses all physical CPU cores.
28    ///
29    /// This is short for calling [`ZstdThreads::new`] with `0`.
30    pub fn all() -> Self {
31        Self(0)
32    }
33}
34
35impl Default for ZstdThreads {
36    /// Returns the default thread value (1) when compressing with zstd.
37    ///
38    /// The default adheres to the one selected by the [zstd] executable.
39    ///
40    /// [zstd]: https://man.archlinux.org/man/zstd.1
41    fn default() -> Self {
42        Self(1)
43    }
44}
45
46/// Settings for a compression encoder.
47#[derive(Clone, Debug, Eq, PartialEq)]
48pub enum CompressionSettings {
49    /// Settings for the bzip2 compression algorithm.
50    Bzip2 {
51        /// The used compression level.
52        compression_level: Bzip2CompressionLevel,
53    },
54
55    /// Settings for the gzip compression algorithm.
56    Gzip {
57        /// The used compression level.
58        compression_level: GzipCompressionLevel,
59    },
60
61    /// Settings for the xz compression algorithm.
62    Xz {
63        /// The used compression level.
64        compression_level: XzCompressionLevel,
65    },
66
67    /// Settings for the zstandard compression algorithm.
68    Zstd {
69        /// The used compression level.
70        compression_level: ZstdCompressionLevel,
71        /// The amount of threads to use when compressing.
72        threads: ZstdThreads,
73    },
74
75    /// No compression.
76    None,
77}
78
79impl Default for CompressionSettings {
80    /// Returns [`CompressionSettings::Zstd`].
81    ///
82    /// Defaults for `compression_level` and `threads` follow that of the [zstd] executable.
83    ///
84    /// [zstd]: https://man.archlinux.org/man/zstd.1
85    fn default() -> Self {
86        Self::Zstd {
87            compression_level: ZstdCompressionLevel::default(),
88            threads: ZstdThreads::default(),
89        }
90    }
91}
92
93impl From<&CompressionSettings> for Option<CompressionAlgorithmFileExtension> {
94    /// Creates an [`Option<CompressionAlgorithmFileExtension>`] from a [`CompressionSettings`].
95    /// Returns [`None`] for [`CompressionSettings::None`].
96    fn from(value: &CompressionSettings) -> Self {
97        match value {
98            CompressionSettings::Bzip2 { .. } => Some(CompressionAlgorithmFileExtension::Bzip2),
99            CompressionSettings::Gzip { .. } => Some(CompressionAlgorithmFileExtension::Gzip),
100            CompressionSettings::Xz { .. } => Some(CompressionAlgorithmFileExtension::Xz),
101            CompressionSettings::Zstd { .. } => Some(CompressionAlgorithmFileExtension::Zstd),
102            CompressionSettings::None => None,
103        }
104    }
105}
106
107#[cfg(test)]
108mod tests {
109    use testresult::TestResult;
110
111    use super::*;
112
113    /// Ensures that the default [`CompressionSettings`] are those for zstd.
114    #[test]
115    fn default_compression_settings() -> TestResult {
116        assert!(matches!(
117            CompressionSettings::default(),
118            CompressionSettings::Zstd {
119                compression_level: _,
120                threads: _,
121            }
122        ));
123        Ok(())
124    }
125}