sevenz_rust2/
method_options.rs1#[cfg(feature = "compress")]
2use lzma_rust2::LZMA2Options;
3
4#[cfg(feature = "aes256")]
5use crate::aes256sha256::AesEncoderOptions;
6use std::fmt::Debug;
7
8#[cfg(feature = "bzip2")]
9#[cfg_attr(docsrs, doc(cfg(feature = "bzip2")))]
10#[derive(Debug, Copy, Clone)]
11pub struct Bzip2Options(pub(crate) u32);
12
13#[cfg(feature = "bzip2")]
14impl Bzip2Options {
15 pub const fn from_level(level: u32) -> Self {
16 Self(level)
17 }
18}
19
20#[cfg(feature = "bzip2")]
21impl Default for Bzip2Options {
22 fn default() -> Self {
23 Self(6)
24 }
25}
26
27#[cfg(feature = "brotli")]
28const MINIMAL_SKIPPABLE_FRAME_SIZE: u32 = 64 * 1024;
29#[cfg(feature = "brotli")]
30const DEFAULT_SKIPPABLE_FRAME_SIZE: u32 = 128 * 1024;
31
32#[cfg(feature = "brotli")]
33#[cfg_attr(docsrs, doc(cfg(feature = "brotli")))]
34#[derive(Debug, Copy, Clone)]
35pub struct BrotliOptions {
36 pub(crate) quality: u32,
37 pub(crate) window: u32,
38 pub(crate) skippable_frame_size: u32,
39}
40
41#[cfg(feature = "brotli")]
42impl BrotliOptions {
43 pub const fn from_quality_window(quality: u32, window: u32) -> Self {
44 let quality = if quality > 11 { 11 } else { quality };
45 let window = if window > 24 { 24 } else { window };
46 Self {
47 quality,
48 window,
49 skippable_frame_size: DEFAULT_SKIPPABLE_FRAME_SIZE,
50 }
51 }
52
53 pub fn with_skippable_frame_size(mut self, skippable_frame_size: u32) -> Self {
61 if skippable_frame_size == 0 {
62 self.skippable_frame_size = 0;
63 } else if skippable_frame_size < MINIMAL_SKIPPABLE_FRAME_SIZE {
64 self.skippable_frame_size = MINIMAL_SKIPPABLE_FRAME_SIZE;
65 } else {
66 self.skippable_frame_size = skippable_frame_size;
67 }
68
69 self
70 }
71}
72
73#[cfg(feature = "brotli")]
74impl Default for BrotliOptions {
75 fn default() -> Self {
76 Self {
77 quality: 11,
78 window: 22,
79 skippable_frame_size: DEFAULT_SKIPPABLE_FRAME_SIZE,
80 }
81 }
82}
83
84#[cfg(feature = "deflate")]
85#[cfg_attr(docsrs, doc(cfg(feature = "deflate")))]
86#[derive(Debug, Copy, Clone)]
87pub struct DeflateOptions(pub(crate) u32);
88
89#[cfg(feature = "deflate")]
90impl DeflateOptions {
91 pub const fn from_level(level: u32) -> Self {
92 let level = if level > 9 { 9 } else { level };
93 Self(level)
94 }
95}
96
97#[cfg(feature = "deflate")]
98impl Default for DeflateOptions {
99 fn default() -> Self {
100 Self(6)
101 }
102}
103
104#[cfg(feature = "lz4")]
105#[cfg_attr(docsrs, doc(cfg(feature = "lz4")))]
106#[derive(Debug, Copy, Clone)]
107pub struct LZ4Options(pub(crate) u32);
108
109#[cfg(feature = "lz4")]
110impl LZ4Options {
111 pub const fn from_level(level: u32) -> Self {
112 let level = if level == 0 {
113 1
114 } else if level > 12 {
115 12
116 } else {
117 level
118 };
119 Self(level)
120 }
121}
122
123#[cfg(feature = "lz4")]
124impl Default for LZ4Options {
125 fn default() -> Self {
126 Self(1)
127 }
128}
129
130#[cfg(feature = "zstd")]
131#[cfg_attr(docsrs, doc(cfg(feature = "zstd")))]
132#[derive(Debug, Copy, Clone)]
133pub struct ZStandardOptions(pub(crate) u32);
134
135#[cfg(feature = "zstd")]
136impl ZStandardOptions {
137 pub const fn from_level(level: u32) -> Self {
138 let level = if level > 22 { 22 } else { level };
139 Self(level)
140 }
141}
142
143#[cfg(feature = "zstd")]
144impl Default for ZStandardOptions {
145 fn default() -> Self {
146 Self(3)
147 }
148}
149
150#[derive(Debug, Clone)]
151pub enum MethodOptions {
152 Num(u32),
153 #[cfg(feature = "compress")]
154 #[cfg_attr(docsrs, doc(cfg(feature = "compress")))]
155 LZMA2(LZMA2Options),
156 #[cfg(feature = "brotli")]
157 #[cfg_attr(docsrs, doc(cfg(feature = "brotli")))]
158 BROTLI(BrotliOptions),
159 #[cfg(feature = "bzip2")]
160 #[cfg_attr(docsrs, doc(cfg(feature = "bzip2")))]
161 BZIP2(Bzip2Options),
162 #[cfg(feature = "deflate")]
163 #[cfg_attr(docsrs, doc(cfg(feature = "deflate")))]
164 DEFLATE(DeflateOptions),
165 #[cfg(feature = "lz4")]
166 #[cfg_attr(docsrs, doc(cfg(feature = "lz4")))]
167 LZ4(LZ4Options),
168 #[cfg(feature = "zstd")]
169 #[cfg_attr(docsrs, doc(cfg(feature = "zstd")))]
170 ZSTD(ZStandardOptions),
171 #[cfg(feature = "aes256")]
172 #[cfg_attr(docsrs, doc(cfg(feature = "aes256")))]
173 Aes(AesEncoderOptions),
174}
175
176#[cfg(feature = "aes256")]
177impl From<AesEncoderOptions> for MethodOptions {
178 fn from(value: AesEncoderOptions) -> Self {
179 Self::Aes(value)
180 }
181}
182
183#[cfg(feature = "aes256")]
184impl From<AesEncoderOptions> for crate::SevenZMethodConfiguration {
185 fn from(value: AesEncoderOptions) -> Self {
186 Self::new(crate::SevenZMethod::AES256SHA256).with_options(MethodOptions::Aes(value))
187 }
188}
189
190#[cfg(feature = "compress")]
191impl From<LZMA2Options> for crate::SevenZMethodConfiguration {
192 fn from(value: LZMA2Options) -> Self {
193 Self::new(crate::SevenZMethod::LZMA2).with_options(MethodOptions::LZMA2(value))
194 }
195}
196
197impl From<u32> for MethodOptions {
198 fn from(n: u32) -> Self {
199 Self::Num(n)
200 }
201}
202
203#[cfg(feature = "compress")]
204impl From<LZMA2Options> for MethodOptions {
205 fn from(o: LZMA2Options) -> Self {
206 Self::LZMA2(o)
207 }
208}
209
210#[cfg(feature = "bzip2")]
211impl From<Bzip2Options> for MethodOptions {
212 fn from(o: Bzip2Options) -> Self {
213 Self::BZIP2(o)
214 }
215}
216
217#[cfg(feature = "brotli")]
218impl From<BrotliOptions> for MethodOptions {
219 fn from(o: BrotliOptions) -> Self {
220 Self::BROTLI(o)
221 }
222}
223
224#[cfg(feature = "deflate")]
225impl From<DeflateOptions> for MethodOptions {
226 fn from(o: DeflateOptions) -> Self {
227 Self::DEFLATE(o)
228 }
229}
230
231#[cfg(feature = "lz4")]
232impl From<LZ4Options> for MethodOptions {
233 fn from(o: LZ4Options) -> Self {
234 Self::LZ4(o)
235 }
236}
237
238#[cfg(feature = "zstd")]
239impl From<ZStandardOptions> for MethodOptions {
240 fn from(o: ZStandardOptions) -> Self {
241 Self::ZSTD(o)
242 }
243}
244
245impl MethodOptions {
246 pub fn get_lzma2_dict_size(&self) -> u32 {
247 match self {
248 MethodOptions::Num(n) => *n,
249 #[cfg(feature = "compress")]
250 MethodOptions::LZMA2(o) => o.dict_size,
251 #[allow(unused)]
252 _ => 0,
253 }
254 }
255}