rdedup_lib/
settings.rs

1//! Settings: options that user can pick
2use std::io;
3
4use crate::config;
5
6#[derive(Clone)]
7pub enum Compression {
8    #[cfg(feature = "with-deflate")]
9    Deflate,
10    #[cfg(feature = "with-xz2")]
11    Xz2,
12    #[cfg(feature = "with-bzip2")]
13    Bzip2,
14    #[cfg(feature = "with-zstd")]
15    Zstd,
16    None,
17}
18
19impl Default for Compression {
20    fn default() -> Self {
21        #[cfg(feature = "with-deflate")]
22        return Compression::Deflate;
23        #[cfg(not(feature = "with-deflate"))]
24        return Compression::None;
25    }
26}
27
28impl Compression {
29    pub fn to_config(&self, _level: i32) -> config::Compression {
30        match *self {
31            #[cfg(feature = "with-deflate")]
32            Compression::Deflate => {
33                config::Compression::Deflate(config::Deflate::new(_level))
34            }
35            #[cfg(feature = "with-xz2")]
36            Compression::Xz2 => {
37                config::Compression::Xz2(config::Xz2::new(_level))
38            }
39            #[cfg(feature = "with-bzip2")]
40            Compression::Bzip2 => {
41                config::Compression::Bzip2(config::Bzip2::new(_level))
42            }
43            #[cfg(feature = "with-zstd")]
44            Compression::Zstd => {
45                config::Compression::Zstd(config::Zstd::new(_level))
46            }
47            Compression::None => config::Compression::None,
48        }
49    }
50}
51
52#[derive(Clone)]
53pub enum Encryption {
54    Curve25519,
55    None,
56}
57
58impl Default for Encryption {
59    fn default() -> Self {
60        Encryption::Curve25519
61    }
62}
63
64// Unlike encryption, settings == config here
65#[derive(Clone, Default)]
66pub struct Chunking(pub(crate) config::Chunking);
67
68#[derive(Clone)]
69pub enum PWHash {
70    Weak,
71    Interactive,
72    Strong,
73}
74
75impl Default for PWHash {
76    fn default() -> Self {
77        PWHash::Strong
78    }
79}
80
81impl<'a> From<&'a str> for PWHash {
82    fn from(s: &str) -> Self {
83        match s {
84            "weak" => PWHash::Weak,
85            "interactive" => PWHash::Interactive,
86            "strong" => PWHash::Strong,
87            _ => panic!("Wrong pwhash strenght string"),
88        }
89    }
90}
91
92#[derive(Clone)]
93pub struct Nesting(u8);
94impl Default for Nesting {
95    fn default() -> Self {
96        Nesting(2)
97    }
98}
99
100impl Nesting {
101    pub fn to_config(&self) -> config::Nesting {
102        config::Nesting(self.0)
103    }
104}
105
106#[derive(Clone)]
107pub enum Hashing {
108    Sha256,
109    Blake2b,
110}
111
112impl Hashing {
113    pub fn to_config(&self) -> config::Hashing {
114        match *self {
115            Hashing::Sha256 => config::Hashing::Sha256,
116            Hashing::Blake2b => config::Hashing::Blake2b,
117        }
118    }
119}
120
121impl Default for Hashing {
122    fn default() -> Self {
123        Hashing::Blake2b
124    }
125}
126
127#[derive(Clone, Default)]
128pub struct Repo {
129    pub(crate) pwhash: PWHash,
130    pub(crate) encryption: Encryption,
131    pub(crate) compression: Compression,
132    pub(crate) compression_level: i32,
133    pub(crate) chunking: Chunking,
134    pub(crate) nesting: Nesting,
135    pub(crate) hashing: Hashing,
136}
137
138impl Repo {
139    pub fn new() -> Self {
140        Default::default()
141    }
142
143    pub fn set_encryption(&mut self, encryption: Encryption) -> io::Result<()> {
144        self.encryption = encryption;
145        Ok(())
146    }
147
148    pub fn set_compression(
149        &mut self,
150        compression: Compression,
151    ) -> io::Result<()> {
152        self.compression = compression;
153        Ok(())
154    }
155
156    pub fn set_pwhash(&mut self, pwhash: PWHash) {
157        self.pwhash = pwhash;
158    }
159
160    pub fn set_compression_level(&mut self, level: i32) {
161        self.compression_level = level;
162    }
163
164    pub fn set_hashing(&mut self, hashing: Hashing) -> io::Result<()> {
165        self.hashing = hashing;
166        Ok(())
167    }
168
169    pub fn use_bup_chunking(&mut self, bits: Option<u32>) -> super::Result<()> {
170        let bits = bits.unwrap_or(config::DEFAULT_BUP_CHUNK_BITS);
171        let chunking = config::Chunking::Bup { chunk_bits: bits };
172
173        if !chunking.valid() {
174            return Err(super::Error::new(
175                io::ErrorKind::InvalidInput,
176                "invalid chunking algorithm defined",
177            ));
178        }
179        self.chunking = Chunking(chunking);
180        Ok(())
181    }
182
183    pub fn use_fastcdc_chunking(
184        &mut self,
185        bits: Option<u32>,
186    ) -> super::Result<()> {
187        let bits = bits.unwrap_or(config::DEFAULT_BUP_CHUNK_BITS);
188        let chunking = config::Chunking::FastCDC { chunk_bits: bits };
189
190        if !chunking.valid() {
191            return Err(super::Error::new(
192                io::ErrorKind::InvalidInput,
193                "invalid chunking algorithm defined",
194            ));
195        }
196        self.chunking = Chunking(chunking);
197        Ok(())
198    }
199
200    pub fn use_gear_chunking(
201        &mut self,
202        bits: Option<u32>,
203    ) -> super::Result<()> {
204        let bits = bits.unwrap_or(config::DEFAULT_BUP_CHUNK_BITS);
205        let chunking = config::Chunking::Gear { chunk_bits: bits };
206
207        if !chunking.valid() {
208            return Err(super::Error::new(
209                io::ErrorKind::InvalidInput,
210                "invalid chunking algorithm defined",
211            ));
212        }
213        self.chunking = Chunking(chunking);
214        Ok(())
215    }
216
217    pub fn set_nesting(&mut self, level: u8) -> super::Result<()> {
218        if level > 31 {
219            return Err(super::Error::new(
220                io::ErrorKind::InvalidInput,
221                "nesting can't be greater than or equal to 32",
222            ));
223        }
224        self.nesting = Nesting(level);
225        Ok(())
226    }
227}