alpm_compress/compression/
settings.rs1use 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#[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 pub fn new(threads: u32) -> Self {
27 Self(threads)
28 }
29
30 pub fn all() -> Self {
34 Self(0)
35 }
36}
37
38impl Default for ZstdThreads {
39 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#[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 Bzip2 {
80 compression_level: Bzip2CompressionLevel,
82 },
83
84 Gzip {
86 compression_level: GzipCompressionLevel,
88 },
89
90 Xz {
92 compression_level: XzCompressionLevel,
94 },
95
96 Zstd {
98 compression_level: ZstdCompressionLevel,
100 threads: ZstdThreads,
102 },
103
104 None,
106}
107
108impl Default for CompressionSettings {
109 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 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 #[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}