rustack-s3-model 0.9.0

Rustack S3 data model types and structures
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
//! Auto-generated from AWS S3 Smithy model. DO NOT EDIT.

use std::fmt;

/// Well-known S3 error codes.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum S3ErrorCode {
    /// Default error code.
    #[default]
    /// AccessDenied error.
    AccessDenied,
    /// AccountProblem error.
    AccountProblem,
    /// BucketAlreadyExists error.
    BucketAlreadyExists,
    /// BucketAlreadyOwnedByYou error.
    BucketAlreadyOwnedByYou,
    /// BucketNotEmpty error.
    BucketNotEmpty,
    /// EntityTooLarge error.
    EntityTooLarge,
    /// EntityTooSmall error.
    EntityTooSmall,
    /// InternalError error.
    InternalError,
    /// InvalidArgument error.
    InvalidArgument,
    /// InvalidBucketName error.
    InvalidBucketName,
    /// InvalidBucketState error.
    InvalidBucketState,
    /// InvalidDigest error.
    InvalidDigest,
    /// InvalidLocationConstraint error.
    InvalidLocationConstraint,
    /// InvalidObjectState error.
    InvalidObjectState,
    /// InvalidPart error.
    InvalidPart,
    /// InvalidPartOrder error.
    InvalidPartOrder,
    /// InvalidRange error.
    InvalidRange,
    /// InvalidRequest error.
    InvalidRequest,
    /// InvalidStorageClass error.
    InvalidStorageClass,
    /// KeyTooLongError error.
    KeyTooLongError,
    /// MalformedXML error.
    MalformedXML,
    /// MetadataTooLarge error.
    MetadataTooLarge,
    /// MethodNotAllowed error.
    MethodNotAllowed,
    /// MissingContentLength error.
    MissingContentLength,
    /// NoSuchBucket error.
    NoSuchBucket,
    /// NoSuchBucketPolicy error.
    NoSuchBucketPolicy,
    /// NoSuchCORSConfiguration error.
    NoSuchCORSConfiguration,
    /// NoSuchKey error.
    NoSuchKey,
    /// NoSuchLifecycleConfiguration error.
    NoSuchLifecycleConfiguration,
    /// NoSuchUpload error.
    NoSuchUpload,
    /// NoSuchVersion error.
    NoSuchVersion,
    /// NoSuchTagSet error.
    NoSuchTagSet,
    /// NoSuchWebsiteConfiguration error.
    NoSuchWebsiteConfiguration,
    /// NotImplemented error.
    NotImplemented,
    /// ObjectNotInActiveTierError error.
    ObjectNotInActiveTierError,
    /// ObjectLockConfigurationNotFoundError error.
    ObjectLockConfigurationNotFoundError,
    /// PreconditionFailed error.
    PreconditionFailed,
    /// SignatureDoesNotMatch error.
    SignatureDoesNotMatch,
    /// TooManyBuckets error.
    TooManyBuckets,
    /// XAmzContentSHA256Mismatch error.
    XAmzContentSHA256Mismatch,
    /// BadDigest error.
    BadDigest,
    /// ConditionalRequestConflict error.
    ConditionalRequestConflict,
    /// MaxMessageLengthExceeded error.
    MaxMessageLengthExceeded,
    /// NoSuchObjectLockConfiguration error.
    NoSuchObjectLockConfiguration,
    /// NoSuchPublicAccessBlockConfiguration error.
    NoSuchPublicAccessBlockConfiguration,
    /// NotModified error (HTTP 304).
    NotModified,
    /// OwnershipControlsNotFoundError error.
    OwnershipControlsNotFoundError,
    /// ReplicationConfigurationNotFoundError error.
    ReplicationConfigurationNotFoundError,
    /// ServerSideEncryptionConfigurationNotFoundError error.
    ServerSideEncryptionConfigurationNotFoundError,
    /// A custom error code not in the standard set.
    Custom(&'static str),
}

impl S3ErrorCode {
    /// Returns the error code as a string.
    #[must_use]
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::AccessDenied => "AccessDenied",
            Self::AccountProblem => "AccountProblem",
            Self::BucketAlreadyExists => "BucketAlreadyExists",
            Self::BucketAlreadyOwnedByYou => "BucketAlreadyOwnedByYou",
            Self::BucketNotEmpty => "BucketNotEmpty",
            Self::EntityTooLarge => "EntityTooLarge",
            Self::EntityTooSmall => "EntityTooSmall",
            Self::InternalError => "InternalError",
            Self::InvalidArgument => "InvalidArgument",
            Self::InvalidBucketName => "InvalidBucketName",
            Self::InvalidBucketState => "InvalidBucketState",
            Self::InvalidDigest => "InvalidDigest",
            Self::InvalidLocationConstraint => "InvalidLocationConstraint",
            Self::InvalidObjectState => "InvalidObjectState",
            Self::InvalidPart => "InvalidPart",
            Self::InvalidPartOrder => "InvalidPartOrder",
            Self::InvalidRange => "InvalidRange",
            Self::InvalidRequest => "InvalidRequest",
            Self::InvalidStorageClass => "InvalidStorageClass",
            Self::KeyTooLongError => "KeyTooLongError",
            Self::MalformedXML => "MalformedXML",
            Self::MetadataTooLarge => "MetadataTooLarge",
            Self::MethodNotAllowed => "MethodNotAllowed",
            Self::MissingContentLength => "MissingContentLength",
            Self::NoSuchBucket => "NoSuchBucket",
            Self::NoSuchBucketPolicy => "NoSuchBucketPolicy",
            Self::NoSuchCORSConfiguration => "NoSuchCORSConfiguration",
            Self::NoSuchKey => "NoSuchKey",
            Self::NoSuchLifecycleConfiguration => "NoSuchLifecycleConfiguration",
            Self::NoSuchUpload => "NoSuchUpload",
            Self::NoSuchVersion => "NoSuchVersion",
            Self::NoSuchTagSet => "NoSuchTagSet",
            Self::NoSuchWebsiteConfiguration => "NoSuchWebsiteConfiguration",
            Self::NotImplemented => "NotImplemented",
            Self::ObjectNotInActiveTierError => "ObjectNotInActiveTierError",
            Self::ObjectLockConfigurationNotFoundError => "ObjectLockConfigurationNotFoundError",
            Self::PreconditionFailed => "PreconditionFailed",
            Self::SignatureDoesNotMatch => "SignatureDoesNotMatch",
            Self::TooManyBuckets => "TooManyBuckets",
            Self::XAmzContentSHA256Mismatch => "XAmzContentSHA256Mismatch",
            Self::BadDigest => "BadDigest",
            Self::ConditionalRequestConflict => "ConditionalRequestConflict",
            Self::MaxMessageLengthExceeded => "MaxMessageLengthExceeded",
            Self::NoSuchObjectLockConfiguration => "NoSuchObjectLockConfiguration",
            Self::NoSuchPublicAccessBlockConfiguration => "NoSuchPublicAccessBlockConfiguration",
            Self::NotModified => "NotModified",
            Self::OwnershipControlsNotFoundError => "OwnershipControlsNotFoundError",
            Self::ReplicationConfigurationNotFoundError => "ReplicationConfigurationNotFoundError",
            Self::ServerSideEncryptionConfigurationNotFoundError => {
                "ServerSideEncryptionConfigurationNotFoundError"
            }
            Self::Custom(s) => s,
        }
    }

    /// Returns the default HTTP status code for this error.
    #[must_use]
    #[allow(clippy::match_same_arms)]
    pub fn default_status_code(&self) -> http::StatusCode {
        match self {
            Self::NotModified => http::StatusCode::NOT_MODIFIED,
            Self::BadDigest
            | Self::EntityTooLarge
            | Self::EntityTooSmall
            | Self::InvalidArgument
            | Self::InvalidBucketName
            | Self::InvalidDigest
            | Self::InvalidLocationConstraint
            | Self::InvalidPart
            | Self::InvalidPartOrder
            | Self::InvalidRequest
            | Self::InvalidStorageClass
            | Self::KeyTooLongError
            | Self::MalformedXML
            | Self::MaxMessageLengthExceeded
            | Self::MetadataTooLarge
            | Self::ServerSideEncryptionConfigurationNotFoundError
            | Self::TooManyBuckets
            | Self::XAmzContentSHA256Mismatch => http::StatusCode::BAD_REQUEST,
            Self::AccessDenied
            | Self::AccountProblem
            | Self::InvalidObjectState
            | Self::ObjectNotInActiveTierError
            | Self::SignatureDoesNotMatch => http::StatusCode::FORBIDDEN,
            Self::NoSuchBucket
            | Self::NoSuchBucketPolicy
            | Self::NoSuchCORSConfiguration
            | Self::NoSuchKey
            | Self::NoSuchLifecycleConfiguration
            | Self::ObjectLockConfigurationNotFoundError
            | Self::NoSuchObjectLockConfiguration
            | Self::NoSuchPublicAccessBlockConfiguration
            | Self::NoSuchUpload
            | Self::NoSuchVersion
            | Self::NoSuchTagSet
            | Self::NoSuchWebsiteConfiguration
            | Self::OwnershipControlsNotFoundError
            | Self::ReplicationConfigurationNotFoundError => http::StatusCode::NOT_FOUND,
            Self::MethodNotAllowed => http::StatusCode::METHOD_NOT_ALLOWED,
            Self::BucketAlreadyExists
            | Self::BucketAlreadyOwnedByYou
            | Self::BucketNotEmpty
            | Self::ConditionalRequestConflict
            | Self::InvalidBucketState => http::StatusCode::CONFLICT,
            Self::MissingContentLength => http::StatusCode::LENGTH_REQUIRED,
            Self::PreconditionFailed => http::StatusCode::PRECONDITION_FAILED,
            Self::InvalidRange => http::StatusCode::RANGE_NOT_SATISFIABLE,
            Self::InternalError => http::StatusCode::INTERNAL_SERVER_ERROR,
            Self::NotImplemented => http::StatusCode::NOT_IMPLEMENTED,
            Self::Custom(_) => http::StatusCode::INTERNAL_SERVER_ERROR,
        }
    }

    /// Returns the default message for this error.
    #[must_use]
    pub fn default_message(&self) -> &'static str {
        match self {
            Self::AccessDenied => "Access Denied",
            Self::AccountProblem => "There is a problem with the account",
            Self::BucketAlreadyExists => "The requested bucket name is not available",
            Self::BucketAlreadyOwnedByYou => "The bucket is already owned by you",
            Self::BucketNotEmpty => "The bucket you tried to delete is not empty",
            Self::EntityTooLarge => "Your proposed upload exceeds the maximum allowed size",
            Self::EntityTooSmall => "Your proposed upload is smaller than the minimum allowed size",
            Self::InternalError => "Internal server error",
            Self::InvalidArgument => "Invalid Argument",
            Self::InvalidBucketName => "The specified bucket is not valid",
            Self::InvalidBucketState => {
                "The request is not valid with the current state of the bucket"
            }
            Self::InvalidDigest => "The Content-MD5 you specified is not valid",
            Self::InvalidLocationConstraint => "The specified location constraint is not valid",
            Self::InvalidObjectState => {
                "The operation is not valid for the current state of the object"
            }
            Self::InvalidPart => "One or more of the specified parts could not be found",
            Self::InvalidPartOrder => "The list of parts was not in ascending order",
            Self::InvalidRange => "The requested range cannot be satisfied",
            Self::InvalidRequest => "Invalid Request",
            Self::InvalidStorageClass => "The storage class you specified is not valid",
            Self::KeyTooLongError => "Your key is too long",
            Self::MalformedXML => "The XML you provided was not well-formed",
            Self::MetadataTooLarge => {
                "Your metadata headers exceed the maximum allowed metadata size"
            }
            Self::MethodNotAllowed => "The specified method is not allowed against this resource",
            Self::MissingContentLength => "You must provide the Content-Length HTTP header",
            Self::NoSuchBucket => "The specified bucket does not exist",
            Self::NoSuchBucketPolicy => "The specified bucket does not have a bucket policy",
            Self::NoSuchCORSConfiguration => "The CORS configuration does not exist",
            Self::NoSuchKey => "The specified key does not exist",
            Self::NoSuchLifecycleConfiguration => "The lifecycle configuration does not exist",
            Self::NoSuchUpload => "The specified multipart upload does not exist",
            Self::NoSuchVersion => "The specified version does not exist",
            Self::NoSuchTagSet => "The TagSet does not exist",
            Self::NoSuchWebsiteConfiguration => "The website configuration does not exist",
            Self::NotImplemented => "The functionality is not implemented",
            Self::ObjectNotInActiveTierError => {
                "The source object of the COPY operation is not in the active tier"
            }
            Self::ObjectLockConfigurationNotFoundError | Self::NoSuchObjectLockConfiguration => {
                "Object Lock configuration does not exist for this bucket"
            }
            Self::PreconditionFailed => {
                "At least one of the preconditions you specified did not hold"
            }
            Self::SignatureDoesNotMatch => "The request signature does not match",
            Self::TooManyBuckets => "You have attempted to create more buckets than allowed",
            Self::XAmzContentSHA256Mismatch => {
                "The provided x-amz-content-sha256 header does not match"
            }
            Self::BadDigest => "The Content-MD5 you specified did not match what we received",
            Self::ConditionalRequestConflict => "The conditional request cannot be processed",
            Self::MaxMessageLengthExceeded => "Your request was too big",
            Self::NoSuchPublicAccessBlockConfiguration => {
                "The public access block configuration was not found"
            }
            Self::NotModified => "Not Modified",
            Self::OwnershipControlsNotFoundError => "The bucket ownership controls were not found",
            Self::ReplicationConfigurationNotFoundError => {
                "The replication configuration was not found"
            }
            Self::ServerSideEncryptionConfigurationNotFoundError => {
                "The server-side encryption configuration was not found"
            }
            Self::Custom(s) => s,
        }
    }
}

impl fmt::Display for S3ErrorCode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// An S3 error response.
#[derive(Debug)]
pub struct S3Error {
    /// The error code.
    pub code: S3ErrorCode,
    /// A human-readable error message.
    pub message: String,
    /// The resource that caused the error.
    pub resource: Option<String>,
    /// The request ID.
    pub request_id: Option<String>,
    /// The HTTP status code.
    pub status_code: http::StatusCode,
    /// The underlying source error, if any.
    pub source: Option<Box<dyn std::error::Error + Send + Sync>>,
    /// Extra HTTP headers to include in the error response.
    ///
    /// Boxed to minimize the size of `S3Error` on the common (header-free) path.
    pub headers: Option<Box<Vec<(String, String)>>>,
}

impl fmt::Display for S3Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "S3Error({}): {}", self.code, self.message)
    }
}

impl std::error::Error for S3Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        self.source
            .as_ref()
            .map(|e| e.as_ref() as &(dyn std::error::Error + 'static))
    }
}

impl S3Error {
    /// Create a new S3Error from an error code.
    #[must_use]
    pub fn new(code: S3ErrorCode) -> Self {
        let status_code = code.default_status_code();
        let message = code.default_message().to_owned();
        Self {
            code,
            message,
            resource: None,
            request_id: None,
            status_code,
            source: None,
            headers: None,
        }
    }

    /// Create a new S3Error with a custom message.
    #[must_use]
    pub fn with_message(code: S3ErrorCode, message: impl Into<String>) -> Self {
        Self {
            status_code: code.default_status_code(),
            message: message.into(),
            code,
            resource: None,
            request_id: None,
            source: None,
            headers: None,
        }
    }

    /// Set the resource that caused this error.
    #[must_use]
    pub fn with_resource(mut self, resource: impl Into<String>) -> Self {
        self.resource = Some(resource.into());
        self
    }

    /// Set the request ID.
    #[must_use]
    pub fn with_request_id(mut self, request_id: impl Into<String>) -> Self {
        self.request_id = Some(request_id.into());
        self
    }

    /// Set the source error.
    #[must_use]
    pub fn with_source(mut self, source: impl std::error::Error + Send + Sync + 'static) -> Self {
        self.source = Some(Box::new(source));
        self
    }

    /// Add an extra HTTP header to the error response.
    #[must_use]
    pub fn with_header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.headers
            .get_or_insert_with(|| Box::new(Vec::new()))
            .push((name.into(), value.into()));
        self
    }

    /// Create a NoSuchBucket error.
    #[must_use]
    pub fn no_such_bucket(bucket_name: impl Into<String>) -> Self {
        Self::new(S3ErrorCode::NoSuchBucket).with_resource(bucket_name)
    }

    /// Create a NoSuchKey error.
    #[must_use]
    pub fn no_such_key(key: impl Into<String>) -> Self {
        Self::new(S3ErrorCode::NoSuchKey).with_resource(key)
    }

    /// Create a NoSuchUpload error.
    #[must_use]
    pub fn no_such_upload(upload_id: impl Into<String>) -> Self {
        Self::new(S3ErrorCode::NoSuchUpload).with_resource(upload_id)
    }

    /// Create a NoSuchVersion error.
    #[must_use]
    pub fn no_such_version(version_id: impl Into<String>) -> Self {
        Self::new(S3ErrorCode::NoSuchVersion).with_resource(version_id)
    }

    /// Create a BucketAlreadyExists error.
    #[must_use]
    pub fn bucket_already_exists(bucket_name: impl Into<String>) -> Self {
        Self::new(S3ErrorCode::BucketAlreadyExists).with_resource(bucket_name)
    }

    /// Create a BucketAlreadyOwnedByYou error.
    #[must_use]
    pub fn bucket_already_owned_by_you(bucket_name: impl Into<String>) -> Self {
        Self::new(S3ErrorCode::BucketAlreadyOwnedByYou).with_resource(bucket_name)
    }

    /// Create a BucketNotEmpty error.
    #[must_use]
    pub fn bucket_not_empty(bucket_name: impl Into<String>) -> Self {
        Self::new(S3ErrorCode::BucketNotEmpty).with_resource(bucket_name)
    }

    /// Create a AccessDenied error.
    #[must_use]
    pub fn access_denied(resource: impl Into<String>) -> Self {
        Self::new(S3ErrorCode::AccessDenied).with_resource(resource)
    }

    /// Create a InternalError error.
    #[must_use]
    pub fn internal_error(message: impl Into<String>) -> Self {
        Self::with_message(S3ErrorCode::InternalError, message)
    }

    /// Create a InvalidArgument error.
    #[must_use]
    pub fn invalid_argument(message: impl Into<String>) -> Self {
        Self::with_message(S3ErrorCode::InvalidArgument, message)
    }

    /// Create a InvalidBucketName error.
    #[must_use]
    pub fn invalid_bucket_name(bucket_name: impl Into<String>) -> Self {
        Self::new(S3ErrorCode::InvalidBucketName).with_resource(bucket_name)
    }

    /// Create a InvalidRange error.
    #[must_use]
    pub fn invalid_range(range: impl Into<String>) -> Self {
        Self::new(S3ErrorCode::InvalidRange).with_resource(range)
    }

    /// Create a InvalidPart error.
    #[must_use]
    pub fn invalid_part(part_info: impl Into<String>) -> Self {
        Self::new(S3ErrorCode::InvalidPart).with_resource(part_info)
    }

    /// Create a InvalidPartOrder error.
    #[must_use]
    pub fn invalid_part_order(detail: impl Into<String>) -> Self {
        Self::new(S3ErrorCode::InvalidPartOrder).with_resource(detail)
    }

    /// Create a MalformedXML error.
    #[must_use]
    pub fn malformed_xml(detail: impl Into<String>) -> Self {
        Self::new(S3ErrorCode::MalformedXML).with_resource(detail)
    }

    /// Create a MethodNotAllowed error.
    #[must_use]
    pub fn method_not_allowed(method: impl Into<String>) -> Self {
        Self::new(S3ErrorCode::MethodNotAllowed).with_resource(method)
    }

    /// Create a NotImplemented error.
    #[must_use]
    pub fn not_implemented(detail: impl Into<String>) -> Self {
        Self::new(S3ErrorCode::NotImplemented).with_resource(detail)
    }

    /// Create a PreconditionFailed error.
    #[must_use]
    pub fn precondition_failed(condition: impl Into<String>) -> Self {
        Self::new(S3ErrorCode::PreconditionFailed).with_resource(condition)
    }

    /// Create a SignatureDoesNotMatch error.
    #[must_use]
    pub fn signature_does_not_match(detail: impl Into<String>) -> Self {
        Self::new(S3ErrorCode::SignatureDoesNotMatch).with_resource(detail)
    }
}

/// Create an S3Error from an error code.
///
/// # Examples
///
/// ```
/// use rustack_s3_model::s3_error;
/// use rustack_s3_model::error::S3ErrorCode;
///
/// let err = s3_error!(NoSuchBucket);
/// assert_eq!(err.code, S3ErrorCode::NoSuchBucket);
///
/// let err = s3_error!(NoSuchKey, "The key does not exist");
/// assert_eq!(err.message, "The key does not exist");
/// ```
#[macro_export]
macro_rules! s3_error {
    ($code:ident) => {
        $crate::error::S3Error::new($crate::error::S3ErrorCode::$code)
    };
    ($code:ident, $msg:expr) => {
        $crate::error::S3Error::with_message($crate::error::S3ErrorCode::$code, $msg)
    };
}