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