Skip to main content

alpm_compress/compression/
settings.rs

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