uplink 0.6.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
//! 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(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(Default)]
pub struct ListObjects<'a> {
    /// Only list objects with this key prefix. When not empty, it must ends with slash.
    prefix: &'a str,
    /// 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`.
    cursor: &'a str,
    /// 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<'a> ListObjects<'a> {
    /// 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: &'a 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: &'a 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: &'a str, cursor: &'a 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: &'a str, cursor: &'a 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 {
            prefix,
            inner_prefix,
            cursor,
            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(Default)]
pub struct ListUploads<'a> {
    /// Only list uncommitted uploads with this key prefix. When not empty, it must ends with slash.
    prefix: &'a str,
    /// 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`.
    cursor: &'a str,
    /// 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<'a> ListUploads<'a> {
    /// 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: &'a 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: &'a 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: &'a str, cursor: &'a 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: &'a str, cursor: &'a 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 {
            prefix,
            inner_prefix,
            cursor,
            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 {
    #[test]
    fn test_list_buckets_with_cursor() {
        // TODO: implement test for checking OK and error cases.
    }

    #[test]
    fn test_list_objects_with_prefix() {
        // TODO: implement test for checking OK and error cases.
    }

    #[test]
    fn test_list_objects_with_cursor() {
        // TODO: implement test for checking OK and error cases.
    }

    #[test]
    fn test_list_objects_with_prefix_and_cursor() {
        // TODO: implement test for checking OK and error cases.
    }

    #[test]
    fn test_list_uploads_with_prefix() {
        // TODO: implement test for checking OK and error cases.
    }

    #[test]
    fn test_list_uploads_with_cursor() {
        // TODO: implement test for checking OK and error cases.
    }

    #[test]
    fn test_list_uploads_with_prefix_and_cursor() {
        // TODO: implement test for checking OK and error cases.
    }
}