sevenz_rust2/
method_options.rs

1#[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#[derive(Debug, Copy, Clone)]
10pub struct Bzip2Options(pub(crate) u32);
11
12#[cfg(feature = "bzip2")]
13impl Bzip2Options {
14    pub const fn from_level(level: u32) -> Self {
15        Self(level)
16    }
17}
18
19#[cfg(feature = "brotli")]
20#[derive(Debug, Copy, Clone)]
21pub struct BrotliOptions {
22    pub(crate) quality: u32,
23    pub(crate) window: u32,
24}
25
26#[cfg(feature = "brotli")]
27impl BrotliOptions {
28    pub const fn from_quality_window(quality: u32, window: u32) -> Self {
29        Self { quality, window }
30    }
31}
32
33#[cfg(feature = "deflate")]
34#[derive(Debug, Copy, Clone)]
35pub struct DeflateOptions(pub(crate) u32);
36
37#[cfg(feature = "deflate")]
38impl DeflateOptions {
39    pub const fn from_level(level: u32) -> Self {
40        Self(level)
41    }
42}
43
44#[cfg(feature = "zstd")]
45#[derive(Debug, Copy, Clone)]
46pub struct ZStandardOptions(pub(crate) i32);
47
48#[cfg(feature = "zstd")]
49impl ZStandardOptions {
50    pub const fn from_level(level: i32) -> Self {
51        Self(level)
52    }
53}
54
55#[derive(Debug, Clone)]
56pub enum MethodOptions {
57    Num(u32),
58    #[cfg(feature = "compress")]
59    LZMA2(LZMA2Options),
60    #[cfg(feature = "brotli")]
61    BROTLI(BrotliOptions),
62    #[cfg(feature = "bzip2")]
63    BZIP2(Bzip2Options),
64    #[cfg(feature = "deflate")]
65    DEFLATE(DeflateOptions),
66    #[cfg(feature = "zstd")]
67    ZSTD(ZStandardOptions),
68    #[cfg(feature = "aes256")]
69    Aes(AesEncoderOptions),
70}
71
72#[cfg(feature = "aes256")]
73impl From<AesEncoderOptions> for MethodOptions {
74    fn from(value: AesEncoderOptions) -> Self {
75        Self::Aes(value)
76    }
77}
78
79#[cfg(feature = "aes256")]
80impl From<AesEncoderOptions> for crate::SevenZMethodConfiguration {
81    fn from(value: AesEncoderOptions) -> Self {
82        Self::new(crate::SevenZMethod::AES256SHA256).with_options(MethodOptions::Aes(value))
83    }
84}
85
86#[cfg(feature = "compress")]
87impl From<LZMA2Options> for crate::SevenZMethodConfiguration {
88    fn from(value: LZMA2Options) -> Self {
89        Self::new(crate::SevenZMethod::LZMA2).with_options(MethodOptions::LZMA2(value))
90    }
91}
92
93impl From<u32> for MethodOptions {
94    fn from(n: u32) -> Self {
95        Self::Num(n)
96    }
97}
98
99#[cfg(feature = "compress")]
100impl From<LZMA2Options> for MethodOptions {
101    fn from(o: LZMA2Options) -> Self {
102        Self::LZMA2(o)
103    }
104}
105
106#[cfg(feature = "bzip2")]
107impl From<Bzip2Options> for MethodOptions {
108    fn from(o: Bzip2Options) -> Self {
109        Self::BZIP2(o)
110    }
111}
112
113#[cfg(feature = "brotli")]
114impl From<BrotliOptions> for MethodOptions {
115    fn from(o: BrotliOptions) -> Self {
116        Self::BROTLI(o)
117    }
118}
119
120#[cfg(feature = "deflate")]
121impl From<DeflateOptions> for MethodOptions {
122    fn from(o: DeflateOptions) -> Self {
123        Self::DEFLATE(o)
124    }
125}
126
127#[cfg(feature = "zstd")]
128impl From<ZStandardOptions> for MethodOptions {
129    fn from(o: ZStandardOptions) -> Self {
130        Self::ZSTD(o)
131    }
132}
133
134impl MethodOptions {
135    pub fn get_lzma2_dict_size(&self) -> u32 {
136        match self {
137            MethodOptions::Num(n) => *n,
138            #[cfg(feature = "compress")]
139            MethodOptions::LZMA2(o) => o.dict_size,
140            #[allow(unused)]
141            _ => 0,
142        }
143    }
144}