uplink 0.11.0

Idiomatic and safe Rust binding for the Storj Lib Uplink
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
//! All the Storj DCS options types related to a Project.

use crate::{helpers, metadata::Custom, Error, Result};

use std::ffi::CString;
use std::time::Duration;

use uplink_sys as ulksys;

/// Options for committing a multipart upload.
pub struct CommitUpload<'a> {
    /// Custom metadata to assign to a multipart upload.
    custom_metadata: &'a mut Custom,
}

impl<'a> CommitUpload<'a> {
    /// Creates an instance of commit upload options.
    ///
    /// It's mutable because converting to a Uplink-C representation requires it.
    pub fn new(custom_metadata: &'a mut Custom) -> Self {
        Self { custom_metadata }
    }

    /// Returns the FFI representation of the options.
    ///
    /// It takes a mutable reference because [`metadata::Custom.to_ffi_c`] requires a mutable
    /// reference.
    #[allow(clippy::wrong_self_convention)]
    pub(crate) fn to_ffi_commit_upload_options(&mut self) -> ulksys::UplinkCommitUploadOptions {
        ulksys::UplinkCommitUploadOptions {
            custom_metadata: self.custom_metadata.to_ffi_custom_metadata(),
        }
    }
}

/// Options for copying objects to a different bucket or/and key without downloading and uploading
/// it.
#[derive(Default)]
pub struct CopyObject {}

impl CopyObject {
    /// Returns the FFI representation of the options.
    pub(crate) fn as_ffi_copy_object_options(&self) -> ulksys::UplinkCopyObjectOptions {
        ulksys::UplinkCopyObjectOptions {}
    }
}

/// Options for downloading an object.
#[derive(Default)]
pub struct Download {
    /// The initial point of the object's blob to download.
    /// If it's negative, it will start at the suffix of the blob but it's isn't supported to be
    /// negative with a positive `length`.
    pub offset: i64,
    /// The length of the blob starting from `offset` to download.
    /// If it's negative, it will read until the end of the blob.
    pub length: i64,
}

impl Download {
    /// Returns the FFI representation of the options.
    pub(crate) fn as_ffi_download_options(&self) -> ulksys::UplinkDownloadOptions {
        ulksys::UplinkDownloadOptions {
            offset: self.offset,
            length: self.length,
        }
    }
}

/// Options for listing buckets.
#[derive(Debug, Default)]
pub struct ListBuckets {
    /// C representation of `cursor` for providing it to the FFI and guards its lifetime until
    /// `self` gets dropped.
    inner_cursor: CString,
}

impl ListBuckets {
    /// Creates options for listing buckets with the specified cursor value.
    /// It returns an error if `cursor` contains any null byte (0 byte).
    pub fn with_cursor(cursor: &str) -> Result<Self> {
        let inner_cursor = helpers::cstring_from_str_fn_arg("cursor", cursor)?;
        Ok(Self { inner_cursor })
    }

    /// Returns the FFI representation of the options.
    pub(crate) fn as_ffi_list_buckets_options(&self) -> ulksys::UplinkListBucketsOptions {
        ulksys::UplinkListBucketsOptions {
            cursor: self.inner_cursor.as_ptr(),
        }
    }
}

/// Options for listing objects.
#[derive(Debug, Default)]
pub struct ListObjects {
    /// Only list objects with this key prefix. When not empty, it must ends with slash.
    ///
    /// C representation of `prefix` for providing it to the FFI and guards its lifetime until
    /// `self` gets dropped.
    inner_prefix: CString,
    /// Specifies the starting position of the iterator by offsetting from the first object of the
    /// list.
    /// The first item of the list is the one after the cursor.
    /// The list of objects depends on the `prefix`.
    ///
    /// C representation of `cursor` for providing it to the FFI and guards its lifetime until
    /// `self` gets dropped.
    inner_cursor: CString,
    /// Iterate the objects without collapsing prefixes.
    pub recursive: bool,
    /// Include the "system metadata" associated with the objects.
    pub system: bool,
    /// Include the "custom metadata" associated with the objects.
    pub custom: bool,
}

impl ListObjects {
    /// Creates options of listing objects options with the specified prefix.
    ///
    /// `prefix` must:
    /// * not be empty.
    /// * end with '/'.
    /// * not contain any null byte (0 byte).
    pub fn with_prefix(prefix: &str) -> Result<Self> {
        if !prefix.ends_with('/') {
            return Err(Error::new_invalid_arguments(
                "prefix",
                "cannot be empty and must end with '/'",
            ));
        }

        Self::new(prefix, "")
    }

    /// Creates options of listing objects options with the specified cursor.
    ///
    /// `cursor` must:
    /// * not be empty.
    /// * not contain any null byte (0 byte).
    pub fn with_cursor(cursor: &str) -> Result<Self> {
        if cursor.is_empty() {
            return Err(Error::new_invalid_arguments("cursor", "cannot be empty"));
        }

        Self::new("", cursor)
    }

    /// Creates options of listing objects options with the specified prefix and cursor.
    ///
    /// `prefix` and `cursor` must:
    /// * not be empty.
    /// * not contain any null byte (0 byte).
    ///
    /// `prefix` must also end with '/'.
    pub fn with_prefix_and_cursor(prefix: &str, cursor: &str) -> Result<Self> {
        if !prefix.ends_with('/') {
            return Err(Error::new_invalid_arguments(
                "prefix",
                "cannot be empty and must end with '/'",
            ));
        }

        if cursor.is_empty() {
            return Err(Error::new_invalid_arguments("cursor", "cannot be empty"));
        }

        Self::new(prefix, cursor)
    }

    /// Creates options for listing objects with only verifying that `prefix` and `cursor` don't
    /// contain any null byte (0 byte), which are essential for working fine with the FFI.
    ///
    /// This is a convenient constructor to be used by the public constructors which impose more
    /// contains  on `prefix` and `cursor`.
    fn new(prefix: &str, cursor: &str) -> Result<Self> {
        let inner_prefix = helpers::cstring_from_str_fn_arg("prefix", prefix)?;
        let inner_cursor = helpers::cstring_from_str_fn_arg("cursor", cursor)?;

        Ok(Self {
            inner_prefix,
            inner_cursor,
            ..Default::default()
        })
    }

    /// Returns the FFI representation of the options.
    pub(crate) fn as_ffi_list_objects_options(&self) -> ulksys::UplinkListObjectsOptions {
        ulksys::UplinkListObjectsOptions {
            prefix: self.inner_prefix.as_ptr(),
            cursor: self.inner_cursor.as_ptr(),
            recursive: self.recursive,
            system: self.system,
            custom: self.custom,
        }
    }
}

/// Options for listing uncommitted uploads.
#[derive(Debug, Default)]
pub struct ListUploads {
    /// Only list uncommitted uploads with this key prefix. When not empty, it must ends with slash.
    ///
    /// C representation of `prefix` for providing it to the FFI and guards its lifetime until
    /// `self` gets dropped.
    inner_prefix: CString,
    /// Specifies the starting position of the iterator by offsetting from the first object of the
    /// list.
    /// The first item of the list is the one after the cursor.
    /// The list of objects depends on the `prefix`.
    ///
    /// C representation of `cursor` for providing it to the FFI and guards its lifetime until
    /// `self` gets dropped.
    inner_cursor: CString,
    /// Iterate the objects without collapsing prefixes.
    pub recursive: bool,
    /// Include the "system metadata" associated with the objects.
    pub system: bool,
    /// Include the "custom metadata" associated with the objects.
    pub custom: bool,
}

impl ListUploads {
    /// Creates options of listing uncommitted uploads options with the specified prefix.
    ///
    /// `prefix` must:
    /// * not be empty.
    /// * end with '/'.
    /// * not contain any null byte (0 byte).
    pub fn with_prefix(prefix: &str) -> Result<Self> {
        if !prefix.ends_with('/') {
            return Err(Error::new_invalid_arguments(
                "prefix",
                "cannot be empty and must end with '/'",
            ));
        }

        Self::new(prefix, "")
    }

    /// Creates options of listing uncommitted uploads options with the specified cursor.
    ///
    /// `cursor` must:
    /// * not be empty.
    /// * not contain any null byte (0 byte).
    pub fn with_cursor(cursor: &str) -> Result<Self> {
        if cursor.is_empty() {
            return Err(Error::new_invalid_arguments("cursor", "cannot be empty"));
        }

        Self::new("", cursor)
    }

    /// Creates options of listing uncommitted options with the specified prefix and cursor.
    ///
    /// `prefix` and `cursor` must:
    /// * not be empty.
    /// * not contain any null byte (0 byte).
    ///
    /// `prefix` must also end with '/'.
    pub fn with_prefix_and_cursor(prefix: &str, cursor: &str) -> Result<Self> {
        if !prefix.ends_with('/') {
            return Err(Error::new_invalid_arguments(
                "prefix",
                "cannot be empty and must end with '/'",
            ));
        }

        if cursor.is_empty() {
            return Err(Error::new_invalid_arguments("cursor", "cannot be empty"));
        }

        Self::new(prefix, cursor)
    }

    /// Creates options for listing uncommitted with only verify that `prefix` and `cursor` don't
    /// contain any null byte (0 byte), which are essential for working fine with the FFI.
    ///
    /// This is a convenient constructor to be used by the public constructors which impose more
    /// contains  on `prefix` and `cursor`.
    fn new(prefix: &str, cursor: &str) -> Result<Self> {
        let inner_prefix = helpers::cstring_from_str_fn_arg("prefix", prefix)?;
        let inner_cursor = helpers::cstring_from_str_fn_arg("cursor", cursor)?;

        Ok(Self {
            inner_prefix,
            inner_cursor,
            ..Default::default()
        })
    }

    /// Returns the FFI representation of the options.
    pub(crate) fn as_ffi_list_uploads_options(&self) -> ulksys::UplinkListUploadsOptions {
        ulksys::UplinkListUploadsOptions {
            prefix: self.inner_prefix.as_ptr(),
            cursor: self.inner_cursor.as_ptr(),
            recursive: self.recursive,
            system: self.system,
            custom: self.custom,
        }
    }
}

/// Options for listing uploads parts.
#[derive(Default)]
pub struct ListUploadParts {
    /// Specifies the starting position of the iterator by offsetting from the first object of the
    /// list.
    ///
    /// The first item of the list is the one after the cursor.
    /// Parts start with index 1.
    pub cursor: u32,
}

impl ListUploadParts {
    /// Returns the FFI representation of the options.
    pub(crate) fn as_ffi_list_upload_parts_options(&self) -> ulksys::UplinkListUploadPartsOptions {
        ulksys::UplinkListUploadPartsOptions {
            cursor: self.cursor,
        }
    }
}

/// Options for moving objects to a different bucket or/and key.
#[derive(Default)]
pub struct MoveObject {}

impl MoveObject {
    /// Returns the FFI representation of the options.
    pub(crate) fn as_ffi_move_object_options(&self) -> ulksys::UplinkMoveObjectOptions {
        ulksys::UplinkMoveObjectOptions {}
    }
}

/// Options for uploading objects.
#[derive(Default)]
pub struct Upload {
    /// Determine when the object expires.
    ///
    /// The time is measured with the number of seconds since the Unix Epoch time. 0 is never and
    /// it's the same as `None`.
    pub expires: Option<Duration>,
}

impl Upload {
    /// Returns the FFI representation of the options.
    pub(crate) fn as_ffi_upload_options(&self) -> ulksys::UplinkUploadOptions {
        let expires = self.expires.unwrap_or(Duration::ZERO);

        ulksys::UplinkUploadOptions {
            expires: expires.as_secs() as i64,
        }
    }
}

/// Options for updating object's metadata.
///
/// Reserved for future use.
#[derive(Default)]
pub struct UploadObjectMetadata {}

impl UploadObjectMetadata {
    /// Returns the FFI representation of the options.
    pub(crate) fn as_ffi_upload_object_metadata_options(
        &self,
    ) -> ulksys::UplinkUploadObjectMetadataOptions {
        ulksys::UplinkUploadObjectMetadataOptions {}
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::error;

    #[test]
    fn test_list_buckets_with_cursor() {
        {
            // OK.
            let cursor = "some-cursor-id";
            let lo = ListBuckets::with_cursor(cursor)
                .expect("no error with a string without the NULL character");
            assert_eq!(cursor, lo.inner_cursor.to_str().unwrap(), "cursor value");
        }
        {
            // Error: invalid cursor value.
            if let Error::InvalidArguments(error::Args { names, msg }) =
                ListBuckets::with_cursor("cursor\0id")
                    .expect_err("when passing a cursor value with NULL bytes")
            {
                assert_eq!(names, "cursor", "invalid error argument name");
                assert_eq!(
                    msg, "cannot contains null bytes (0 byte). Null byte found at 6",
                    "invalid error argument message"
                );
            } else {
                panic!("expected an invalid argument error");
            }
        }
    }

    #[test]
    fn test_list_objects_with_prefix() {
        {
            // OK.
            let prefix = "a/b/";
            let lo = ListObjects::with_prefix(prefix)
                .expect("no error with a string without the NULL character and ending with '/'");
            assert_eq!(prefix, lo.inner_prefix.to_str().unwrap(), "prefix value");
        }
        {
            // Error: prefix doesn't end with `/`.
            if let Error::InvalidArguments(error::Args { names, msg }) =
                ListObjects::with_prefix("a/b")
                    .expect_err("when passing a prefix value without ending with '/'")
            {
                assert_eq!(names, "prefix", "invalid error argument name");
                assert_eq!(
                    msg, "cannot be empty and must end with '/'",
                    "invalid error argument message"
                );
            } else {
                panic!("expected an invalid argument error");
            }
        }
        {
            // Error: invalid prefix value.
            if let Error::InvalidArguments(error::Args { names, msg }) =
                ListObjects::with_prefix("a/b/\0/c/")
                    .expect_err("when passing a prefix value with NULL bytes")
            {
                assert_eq!(names, "prefix", "invalid error argument name");
                assert_eq!(
                    msg, "cannot contains null bytes (0 byte). Null byte found at 4",
                    "invalid error argument message"
                );
            } else {
                panic!("expected an invalid argument error");
            }
        }
    }

    #[test]
    fn test_list_objects_with_cursor() {
        {
            // OK.
            let cursor = "some-cursor-id";
            let lb = ListObjects::with_cursor(cursor)
                .expect("no error with a string without the NULL character");
            assert_eq!(cursor, lb.inner_cursor.to_str().unwrap(), "cursor value");
        }
        {
            // Error: invalid cursor value.
            if let Error::InvalidArguments(error::Args { names, msg }) =
                ListObjects::with_cursor("cursor\0id")
                    .expect_err("when passing a cursor value with NULL bytes")
            {
                assert_eq!(names, "cursor", "invalid error argument name");
                assert_eq!(
                    msg, "cannot contains null bytes (0 byte). Null byte found at 6",
                    "invalid error argument message"
                );
            } else {
                panic!("expected an invalid argument error");
            }
        }
    }

    #[test]
    fn test_list_objects_with_prefix_and_cursor() {
        {
            // OK.
            let prefix = "a/b/";
            let cursor = "cursor-id";
            let lo = ListObjects::with_prefix_and_cursor(prefix, cursor)
                .expect("no error with a valid prefix and cursor");
            assert_eq!(prefix, lo.inner_prefix.to_str().unwrap(), "prefix value");
            assert_eq!(cursor, lo.inner_cursor.to_str().unwrap(), "cursor value");
        }
        {
            // Error: prefix doesn't end with `/`.
            if let Error::InvalidArguments(error::Args { names, msg }) =
                ListObjects::with_prefix_and_cursor("a/b", "cursor-id")
                    .expect_err("when passing a prefix value without ending with '/'")
            {
                assert_eq!(names, "prefix", "invalid error argument name");
                assert_eq!(
                    msg, "cannot be empty and must end with '/'",
                    "invalid error argument message"
                );
            } else {
                panic!("expected an invalid argument error");
            }
        }
        {
            // Error: invalid prefix value.
            if let Error::InvalidArguments(error::Args { names, msg }) =
                ListObjects::with_prefix_and_cursor("a/b/\0/c/", "cursor-id")
                    .expect_err("when passing a cursor value with NULL bytes")
            {
                assert_eq!(names, "prefix", "invalid error argument name");
                assert_eq!(
                    msg, "cannot contains null bytes (0 byte). Null byte found at 4",
                    "invalid error argument message"
                );
            } else {
                panic!("expected an invalid argument error");
            }
        }
        {
            // Error: invalid cursor value.
            if let Error::InvalidArguments(error::Args { names, msg }) =
                ListObjects::with_prefix_and_cursor("a/b/", "cursor\0id")
                    .expect_err("when passing a cursor value with NULL bytes")
            {
                assert_eq!(names, "cursor", "invalid error argument name");
                assert_eq!(
                    msg, "cannot contains null bytes (0 byte). Null byte found at 6",
                    "invalid error argument message"
                );
            } else {
                panic!("expected an invalid argument error");
            }
        }
    }

    #[test]
    fn test_list_uploads_with_prefix() {
        {
            // OK.
            let prefix = "a/b/";
            let lu = ListUploads::with_prefix(prefix)
                .expect("no error with a string without the NULL character and ending with '/'");
            assert_eq!(prefix, lu.inner_prefix.to_str().unwrap(), "prefix value");
        }
        {
            // Error: prefix doesn't end with `/`.
            if let Error::InvalidArguments(error::Args { names, msg }) =
                ListUploads::with_prefix("a/b")
                    .expect_err("when passing a prefix value without ending with '/'")
            {
                assert_eq!(names, "prefix", "invalid error argument name");
                assert_eq!(
                    msg, "cannot be empty and must end with '/'",
                    "invalid error argument message"
                );
            } else {
                panic!("expected an invalid argument error");
            }
        }
        {
            // Error: invalid prefix value.
            if let Error::InvalidArguments(error::Args { names, msg }) =
                ListUploads::with_prefix("a/b/\0/c/")
                    .expect_err("when passing a prefix value with NULL bytes")
            {
                assert_eq!(names, "prefix", "invalid error argument name");
                assert_eq!(
                    msg, "cannot contains null bytes (0 byte). Null byte found at 4",
                    "invalid error argument message"
                );
            } else {
                panic!("expected an invalid argument error");
            }
        }
    }

    #[test]
    fn test_list_uploads_with_cursor() {
        {
            // OK.
            let cursor = "some-cursor-id";
            let lu = ListUploads::with_cursor(cursor)
                .expect("no error with a string without the NULL character");
            assert_eq!(cursor, lu.inner_cursor.to_str().unwrap(), "cursor value");
        }
        {
            // Error: invalid cursor value.
            if let Error::InvalidArguments(error::Args { names, msg }) =
                ListUploads::with_cursor("cursor\0id")
                    .expect_err("when passing a cursor value with NULL bytes")
            {
                assert_eq!(names, "cursor", "invalid error argument name");
                assert_eq!(
                    msg, "cannot contains null bytes (0 byte). Null byte found at 6",
                    "invalid error argument message"
                );
            } else {
                panic!("expected an invalid argument error");
            }
        }
    }

    #[test]
    fn test_list_uploads_with_prefix_and_cursor() {
        {
            // OK.
            let prefix = "a/b/";
            let cursor = "cursor-id";
            let lu = ListUploads::with_prefix_and_cursor(prefix, cursor)
                .expect("no error with a valid prefix and cursor");
            assert_eq!(prefix, lu.inner_prefix.to_str().unwrap(), "prefix value");
            assert_eq!(cursor, lu.inner_cursor.to_str().unwrap(), "cursor value");
        }
        {
            // Error: prefix doesn't end with `/`.
            if let Error::InvalidArguments(error::Args { names, msg }) =
                ListUploads::with_prefix_and_cursor("a/b", "cursor-id")
                    .expect_err("when passing a prefix value without ending with '/'")
            {
                assert_eq!(names, "prefix", "invalid error argument name");
                assert_eq!(
                    msg, "cannot be empty and must end with '/'",
                    "invalid error argument message"
                );
            } else {
                panic!("expected an invalid argument error");
            }
        }
        {
            // Error: invalid prefix value.
            if let Error::InvalidArguments(error::Args { names, msg }) =
                ListUploads::with_prefix_and_cursor("a/b/\0/c/", "cursor-id")
                    .expect_err("when passing a cursor value with NULL bytes")
            {
                assert_eq!(names, "prefix", "invalid error argument name");
                assert_eq!(
                    msg, "cannot contains null bytes (0 byte). Null byte found at 4",
                    "invalid error argument message"
                );
            } else {
                panic!("expected an invalid argument error");
            }
        }
        {
            // Error: invalid cursor value.
            if let Error::InvalidArguments(error::Args { names, msg }) =
                ListUploads::with_prefix_and_cursor("a/b/", "cursor\0id")
                    .expect_err("when passing a cursor value with NULL bytes")
            {
                assert_eq!(names, "cursor", "invalid error argument name");
                assert_eq!(
                    msg, "cannot contains null bytes (0 byte). Null byte found at 6",
                    "invalid error argument message"
                );
            } else {
                panic!("expected an invalid argument error");
            }
        }
    }
}