sett 0.4.0

Rust port of sett (data compression, encryption and transfer tool).
Documentation
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
//! Error types for s3 operations

pub use aws_sdk_s3;

use crate::secret::SecretCorruptionError;

/// Reexports for foreign error types
mod aws {
    pub use super::aws_sdk_s3::{
        error::SdkError,
        operation::{
            complete_multipart_upload::CompleteMultipartUploadError,
            create_multipart_upload::CreateMultipartUploadError, get_object::GetObjectError,
            head_object::HeadObjectError, put_object::PutObjectError, upload_part::UploadPartError,
        },
        primitives::ByteStreamError,
    };
}

/// S3 get object error alias
pub type S3GetObjectError = aws::SdkError<aws::GetObjectError>;
/// S3 byte stream error alias
pub type S3ByteStreamError = aws::ByteStreamError;

/// Error during retrieval of S3 credentials from an external provider.
#[derive(Debug)]
pub struct CredentialsRetrievalError {
    pub(crate) message: String,
}

impl std::fmt::Display for CredentialsRetrievalError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Failed to retrieve S3 credentials from external provider: {}",
            self.message
        )
    }
}

impl std::error::Error for CredentialsRetrievalError {}

/// Error occurring when setting up a new S3 client.
#[derive(Debug)]
pub enum ClientError {
    /// S3 credentials decoding error.
    CredentialsCorruption(SecretCorruptionError),
    /// Error during retrieval of S3 credentials from an external provider.
    CredentialsRetrieval(CredentialsRetrievalError),
    /// Setup of the proxy error.
    Proxy(super::proxy::error::ConnectionError),
}

impl std::fmt::Display for ClientError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::CredentialsCorruption(e) => write!(f, "Credentials corruption: {e}"),
            Self::CredentialsRetrieval(e) => {
                write!(f, "{e}")
            }
            Self::Proxy(e) => write!(f, "Proxy setup failed: {e}"),
        }
    }
}

impl std::error::Error for ClientError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::CredentialsCorruption(source) => Some(source),
            Self::CredentialsRetrieval(source) => Some(source),
            Self::Proxy(source) => Some(source),
        }
    }
}

impl From<SecretCorruptionError> for ClientError {
    fn from(value: SecretCorruptionError) -> Self {
        Self::CredentialsCorruption(value)
    }
}
impl From<CredentialsRetrievalError> for ClientError {
    fn from(value: CredentialsRetrievalError) -> Self {
        Self::CredentialsRetrieval(value)
    }
}
impl From<super::proxy::error::ConnectionError> for ClientError {
    fn from(value: super::proxy::error::ConnectionError) -> Self {
        Self::Proxy(value)
    }
}

/// Error occurring while reading chunks from a stream
#[derive(Debug)]
pub enum ReadChunksError {
    /// IO error
    Io(tokio::io::Error),
    /// Error while transferring bytes
    Send(tokio::sync::mpsc::error::SendError<super::BytesMut>),
    /// Data is too large to be split into the max number of chunks allowed
    /// by the S3 protocol.
    DataTooLarge,
}

impl std::fmt::Display for ReadChunksError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Io(e) => write!(f, "Error while reading chunks from data: {e}"),
            Self::Send(e) => write!(
                f,
                "Error while transferring data to the S3 object store: {e}"
            ),
            Self::DataTooLarge => write!(
                f,
                "Data is too large to be split into the maximum number of \
                chunks allowed by the S3 protocol. If not already the case, \
                consider using a 64-bit system to increase supported data size"
            ),
        }
    }
}

impl std::error::Error for ReadChunksError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Io(source) => Some(source),
            Self::Send(source) => Some(source),
            Self::DataTooLarge => None,
        }
    }
}

impl From<tokio::io::Error> for ReadChunksError {
    fn from(value: tokio::io::Error) -> Self {
        Self::Io(value)
    }
}

impl From<tokio::sync::mpsc::error::SendError<super::BytesMut>> for ReadChunksError {
    fn from(value: tokio::sync::mpsc::error::SendError<super::BytesMut>) -> Self {
        Self::Send(value)
    }
}

impl From<std::num::TryFromIntError> for ReadChunksError {
    fn from(_: std::num::TryFromIntError) -> Self {
        Self::DataTooLarge
    }
}

type MetadataError = crate::package::error::MetadataError<
    <std::path::PathBuf as crate::package::source::PackageStream>::Error,
>;

/// Error occurring while uploading data
#[derive(Debug)]
pub enum UploadError {
    /// Error while creating a client object to connect with the S3 instance.
    ClientError(ClientError),
    /// IO error
    Io(tokio::io::Error),
    /// Invalid package
    InvalidPackage(MetadataError),
    /// Error during put
    PutObject(Box<put::Error>),
    /// Error while reading chunks
    ReadChunks(ReadChunksError),
    /// Async task join error
    AsyncTask(tokio::task::JoinError),
}

impl std::fmt::Display for UploadError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Error while reading chunks")
    }
}

impl std::error::Error for UploadError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::ClientError(source) => Some(source),
            Self::Io(source) => Some(source),
            Self::InvalidPackage(source) => Some(source),
            Self::PutObject(source) => Some(source),
            Self::ReadChunks(source) => Some(source),
            Self::AsyncTask(source) => Some(source),
        }
    }
}

impl From<ClientError> for UploadError {
    fn from(value: ClientError) -> Self {
        Self::ClientError(value)
    }
}
impl From<tokio::io::Error> for UploadError {
    fn from(value: tokio::io::Error) -> Self {
        Self::Io(value)
    }
}
impl From<MetadataError> for UploadError {
    fn from(value: MetadataError) -> Self {
        Self::InvalidPackage(value)
    }
}
impl From<put::Error> for UploadError {
    fn from(value: put::Error) -> Self {
        Self::PutObject(Box::new(value))
    }
}
impl From<ReadChunksError> for UploadError {
    fn from(value: ReadChunksError) -> Self {
        Self::ReadChunks(value)
    }
}
impl From<tokio::task::JoinError> for UploadError {
    fn from(value: tokio::task::JoinError) -> Self {
        Self::AsyncTask(value)
    }
}

/// Namespace for [put::Error]
pub mod put {
    use super::aws;

    /// Error type for s3 put operations
    #[non_exhaustive]
    #[derive(Debug)]
    pub enum Error {
        /// Error during put.
        Put(aws::SdkError<aws::PutObjectError>),
        /// Error during multipart put
        PutMultipart(aws::SdkError<aws::CreateMultipartUploadError>),
        /// Error while uploading a part
        UploadPart(aws::SdkError<aws::UploadPartError>),
        /// Fetching multipart id fails
        FetchMultipartId,
        /// Fetching entity tag fails
        FetchEntityTag,
        /// When nothing has been uploaded
        EmptyUpload,
        /// Completing multipart upload fails
        CompleteMultipartUpload(aws::SdkError<aws::CompleteMultipartUploadError>),
        /// Semaphore acquisition error
        Semaphore(tokio::sync::AcquireError),
        /// Async task join error
        Join(tokio::task::JoinError),
    }
    impl std::fmt::Display for Error {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            match self {
                Self::Put(source) => std::fmt::Display::fmt(&super::Error(source), f),
                Self::PutMultipart(source) => std::fmt::Display::fmt(&super::Error(source), f),
                Self::FetchMultipartId => write!(f, "Multipart upload ID could not be fetched"),
                Self::UploadPart(source) => source.fmt(f),
                Self::Semaphore(_) => write!(f, "Semaphore error"),
                Self::EmptyUpload => write!(f, "No parts have been uploaded"),
                Self::FetchEntityTag => write!(
                    f,
                    "Could not retrieve the entity tag for the uploaded object"
                ),
                Self::CompleteMultipartUpload(source) => source.fmt(f),
                Self::Join(source) => source.fmt(f),
            }
        }
    }

    impl From<aws::SdkError<aws::PutObjectError>> for Error {
        fn from(value: aws::SdkError<aws::PutObjectError>) -> Self {
            Self::Put(value)
        }
    }

    impl From<aws::SdkError<aws::UploadPartError>> for Error {
        fn from(value: aws::SdkError<aws::UploadPartError>) -> Self {
            Self::UploadPart(value)
        }
    }

    impl From<aws::SdkError<aws::CreateMultipartUploadError>> for Error {
        fn from(value: aws::SdkError<aws::CreateMultipartUploadError>) -> Self {
            Self::PutMultipart(value)
        }
    }

    impl From<tokio::sync::AcquireError> for Error {
        fn from(value: tokio::sync::AcquireError) -> Self {
            Self::Semaphore(value)
        }
    }

    impl From<aws::SdkError<aws::CompleteMultipartUploadError>> for Error {
        fn from(value: aws::SdkError<aws::CompleteMultipartUploadError>) -> Self {
            Self::CompleteMultipartUpload(value)
        }
    }

    impl From<tokio::task::JoinError> for Error {
        fn from(value: tokio::task::JoinError) -> Self {
            Self::Join(value)
        }
    }

    impl std::error::Error for Error {
        fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
            match self {
                Self::Put(source) => Some(source),
                Self::PutMultipart(source) => Some(source),
                Self::UploadPart(source) => Some(source),
                Self::FetchMultipartId => None,
                Self::Semaphore(source) => Some(source),
                Self::EmptyUpload => None,
                Self::FetchEntityTag => None,
                Self::CompleteMultipartUpload(source) => Some(source),
                Self::Join(source) => Some(source),
            }
        }
    }
}

/// Namespace for [get::Error]
pub mod get {
    use super::aws;

    /// Error type for s3 fetch operations
    #[non_exhaustive]
    #[derive(Debug)]
    pub struct Error(pub aws::SdkError<aws::HeadObjectError>);
    impl std::fmt::Display for Error {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            std::fmt::Display::fmt(&super::Error(self.source()), f)
        }
    }

    impl Error {
        /// Access the source of the error
        pub fn source(&self) -> &aws::SdkError<aws::HeadObjectError> {
            &self.0
        }
    }

    impl From<aws::SdkError<aws::HeadObjectError>> for Error {
        fn from(value: aws::SdkError<aws::HeadObjectError>) -> Self {
            Self(value)
        }
    }

    impl std::error::Error for Error {
        fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
            Some(&self.0)
        }
    }
}

/// Generic errors for getting / putting an s3 object.
#[derive(Debug)]
struct Error<E>(E);

impl<E> std::fmt::Display for Error<&aws::SdkError<E>>
where
    E: ExtractMessage + MainMessage,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self.0 {
            aws::SdkError::DispatchFailure(e) => {
                use std::error::Error;
                if let Some(connector_error) = e.as_connector_error()
                    && let Some(source) = connector_error.source()
                {
                    return write!(f, "could not connect to s3 store: {source}");
                }
            }
            aws::SdkError::ServiceError(e) => {
                if let Some(description) = e.err().extract_message() {
                    return write!(f, "{}: {description}", E::MAIN_MESSAGE);
                }
                if let Some(description) = e.raw().headers().get("x-minio-error-desc") {
                    return write!(f, "{}: {description}", E::MAIN_MESSAGE);
                }
            }
            _ => {}
        }
        write!(f, "{}: {}", E::MAIN_MESSAGE, self.0)
    }
}

trait MainMessage {
    const MAIN_MESSAGE: &'static str;
}

impl MainMessage for aws::HeadObjectError {
    const MAIN_MESSAGE: &'static str = "unable to access s3 object";
}

impl MainMessage for aws::PutObjectError {
    const MAIN_MESSAGE: &'static str = "unable to put s3 object";
}

impl MainMessage for aws::CreateMultipartUploadError {
    const MAIN_MESSAGE: &'static str = "unable to put s3 object";
}

trait ExtractMessage {
    fn extract_message(&self) -> Option<&str>;
}

impl ExtractMessage for aws::HeadObjectError {
    fn extract_message(&self) -> Option<&str> {
        self.meta().message()
    }
}

impl ExtractMessage for aws::PutObjectError {
    fn extract_message(&self) -> Option<&str> {
        self.meta().message()
    }
}

impl ExtractMessage for aws::CreateMultipartUploadError {
    fn extract_message(&self) -> Option<&str> {
        self.meta().message()
    }
}

/// Error occurring when trying to open an S3 stream
#[derive(Debug)]
pub enum OpenError {
    /// I/O error
    Io(tokio::io::Error),
    /// Get error
    Get(get::Error),
    /// Zero object size
    ObjectTooSmall,
    /// No size info available
    MissingSizeInfo,
    /// Get object
    GetObject(S3GetObjectError),
    /// Read stream error
    ReadStream(S3ByteStreamError),
    /// S3 client error
    Client(ClientError),
}

impl std::fmt::Display for OpenError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Error while opening zip reader{}",
            match self {
                Self::ObjectTooSmall => ": s3 object size is too small",
                Self::MissingSizeInfo => ": unable to fetch s3 object size",
                Self::ReadStream(_) => ": error while reading from stream",
                _ => "",
            }
        )
    }
}

impl std::error::Error for OpenError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Io(source) => Some(source),
            Self::Get(source) => Some(source),
            Self::ObjectTooSmall => None,
            Self::MissingSizeInfo => None,
            Self::GetObject(source) => Some(source),
            Self::ReadStream(source) => Some(source),
            Self::Client(source) => Some(source),
        }
    }
}

impl From<tokio::io::Error> for OpenError {
    fn from(value: tokio::io::Error) -> Self {
        Self::Io(value)
    }
}

impl From<get::Error> for OpenError {
    fn from(value: get::Error) -> Self {
        Self::Get(value)
    }
}

impl From<S3GetObjectError> for OpenError {
    fn from(value: S3GetObjectError) -> Self {
        Self::GetObject(value)
    }
}

impl From<S3ByteStreamError> for OpenError {
    fn from(value: S3ByteStreamError) -> Self {
        Self::ReadStream(value)
    }
}

impl From<ClientError> for OpenError {
    fn from(value: ClientError) -> Self {
        Self::Client(value)
    }
}