loco-rs 1.1.0

The one-person framework for Rust
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
//! # Single Storage Strategy Implementation
//!
//! This module provides an implementation of the [`StorageStrategy`] for a
//! single storage strategy.
use std::path::Path;

use bytes::Bytes;

use crate::storage::{drivers::ListEntry, strategies::StorageStrategy, Storage, StorageResult};

/// Represents a single storage strategy.
#[derive(Clone)]
pub struct SingleStrategy {
    pub primary: String,
}

impl SingleStrategy {
    /// Creates a new instance of `SingleStrategy` with the specified primary
    /// storage identifier.
    #[must_use]
    pub fn new(primary: &str) -> Self {
        Self {
            primary: primary.to_string(),
        }
    }
}

/// Implementation of `StorageStrategy` for a single storage strategy.
#[async_trait::async_trait]
impl StorageStrategy for SingleStrategy {
    /// Uploads content to the primary storage.
    ///
    /// # Errors
    ///
    /// Returns a [`StorageResult`] indicating of the operation status.
    async fn upload(&self, storage: &Storage, path: &Path, content: &Bytes) -> StorageResult<()> {
        storage
            .as_store_err(&self.primary)?
            .upload(path, content)
            .await?;
        Ok(())
    }

    /// Downloads content
    ///
    /// # Errors
    ///
    /// Returns a [`StorageResult`] indicating of the operation status.
    async fn download(&self, storage: &Storage, path: &Path) -> StorageResult<Bytes> {
        let store = storage.as_store_err(&self.primary)?;
        Ok(store.get(path).await?.bytes().await?)
    }

    /// Deletes the given path
    ///
    /// # Errors
    ///
    /// Returns a [`StorageResult`] indicating of the operation status.
    async fn delete(&self, storage: &Storage, path: &Path) -> StorageResult<()> {
        Ok(storage.as_store_err(&self.primary)?.delete(path).await?)
    }

    /// Renames the file name
    ///
    /// # Errors
    ///
    /// Returns a [`StorageResult`] indicating of the operation status.
    async fn rename(&self, storage: &Storage, from: &Path, to: &Path) -> StorageResult<()> {
        Ok(storage
            .as_store_err(&self.primary)?
            .rename(from, to)
            .await?)
    }

    /// Copy file from the given path to the new path
    ///
    /// # Errors
    ///
    /// Returns a [`StorageResult`] indicating of the operation status.
    async fn copy(&self, storage: &Storage, from: &Path, to: &Path) -> StorageResult<()> {
        Ok(storage.as_store_err(&self.primary)?.copy(from, to).await?)
    }

    /// Checks if content exists at the given path in the primary storage.
    ///
    /// # Errors
    ///
    /// Returns a [`StorageResult`] indicating of the operation status.
    async fn exists(&self, storage: &Storage, path: &Path) -> StorageResult<bool> {
        storage.as_store_err(&self.primary)?.exists(path).await
    }

    /// Lists entries under the given prefix in the primary storage.
    ///
    /// # Errors
    ///
    /// Returns a [`StorageResult`] indicating of the operation status.
    async fn list(
        &self,
        storage: &Storage,
        path: &Path,
        recursive: bool,
    ) -> StorageResult<Vec<ListEntry>> {
        storage
            .as_store_err(&self.primary)?
            .list(path, recursive)
            .await
    }

    /// Retrieves metadata for a single path in the primary storage.
    ///
    /// # Errors
    ///
    /// Returns a [`StorageResult`] indicating of the operation status.
    async fn stat(&self, storage: &Storage, path: &Path) -> StorageResult<ListEntry> {
        storage.as_store_err(&self.primary)?.stat(path).await
    }

    /// Downloads content as a stream from the primary storage
    ///
    /// # Errors
    ///
    /// Returns a [`StorageResult`] with the stream
    async fn download_stream(
        &self,
        storage: &Storage,
        path: &Path,
    ) -> StorageResult<super::super::stream::BytesStream> {
        storage.as_store_err(&self.primary)?.get_stream(path).await
    }

    /// Uploads content from a stream to the primary storage
    ///
    /// # Errors
    ///
    /// Returns a [`StorageResult`] indicating of the operation status.
    async fn upload_stream(
        &self,
        storage: &Storage,
        path: &Path,
        stream: super::super::stream::BytesStream,
    ) -> StorageResult<()> {
        storage
            .as_store_err(&self.primary)?
            .upload_stream(path, stream)
            .await?;
        Ok(())
    }
}

#[cfg(test)]
mod tests {

    use std::{
        collections::BTreeMap,
        path::{Path, PathBuf},
    };

    use super::*;
    use crate::storage::{drivers, Storage};

    #[tokio::test]
    async fn can_upload() {
        let store = drivers::mem::new();

        let strategy = Box::new(SingleStrategy::new("default")) as Box<dyn StorageStrategy>;

        let storage = Storage::new(BTreeMap::from([("default".to_string(), store)]), strategy);

        let store = storage.as_store("default").unwrap();
        let path = PathBuf::from("users").join("data").join("1.txt");
        let file_content = Bytes::from("file content");

        assert!(storage.upload(path.as_path(), &file_content).await.is_ok());

        assert!(store.exists(path.as_path()).await.unwrap());
    }

    #[tokio::test]
    async fn can_download() {
        let store = drivers::mem::new();

        let strategy = Box::new(SingleStrategy::new("default")) as Box<dyn StorageStrategy>;

        let storage = Storage::new(BTreeMap::from([("default".to_string(), store)]), strategy);

        let path = PathBuf::from("users").join("data").join("1.txt");
        let file_content = Bytes::from("file content");

        let store = storage.as_store("default").unwrap();
        assert!(store.upload(path.as_path(), &file_content).await.is_ok());

        let download_file: String = storage.download(path.as_path()).await.unwrap();
        assert_eq!(download_file, file_content);
    }

    #[tokio::test]
    async fn can_delete() {
        let store = drivers::mem::new();

        let strategy = Box::new(SingleStrategy::new("default")) as Box<dyn StorageStrategy>;

        let storage = Storage::new(BTreeMap::from([("default".to_string(), store)]), strategy);

        let store = storage.as_store("default").unwrap();
        let path = PathBuf::from("users").join("data").join("1.txt");
        let file_content = Bytes::from("file content");

        assert!(store.upload(path.as_path(), &file_content).await.is_ok());

        assert!(store.exists(path.as_path()).await.unwrap());

        assert!(storage.delete(path.as_path()).await.is_ok());

        assert!(!store.exists(path.as_path()).await.unwrap());
    }

    #[tokio::test]
    async fn can_rename_file_path() {
        let store = drivers::mem::new();

        let strategy = Box::new(SingleStrategy::new("default")) as Box<dyn StorageStrategy>;

        let storage = Storage::new(BTreeMap::from([("default".to_string(), store)]), strategy);

        let store = storage.as_store("default").unwrap();
        let orig_path = PathBuf::from("users").join("data").join("1.txt");
        let file_content = Bytes::from("file content");

        assert!(storage
            .upload(orig_path.as_path(), &file_content)
            .await
            .is_ok());

        assert!(store.exists(orig_path.as_path()).await.unwrap());

        let new_path = PathBuf::from("users").join("data-2").join("2.txt");
        assert!(storage
            .rename(orig_path.as_path(), new_path.as_path())
            .await
            .is_ok());

        assert!(!store.exists(orig_path.as_path()).await.unwrap());
        assert!(store.exists(new_path.as_path()).await.unwrap());
    }

    #[tokio::test]
    async fn can_copy_file_path() {
        let store = drivers::mem::new();

        let strategy = Box::new(SingleStrategy::new("default")) as Box<dyn StorageStrategy>;

        let storage = Storage::new(BTreeMap::from([("default".to_string(), store)]), strategy);

        let store = storage.as_store("default").unwrap();
        let orig_path = PathBuf::from("users").join("data").join("1.txt");
        let file_content = Bytes::from("file content");

        assert!(storage
            .upload(orig_path.as_path(), &file_content)
            .await
            .is_ok());

        assert!(store.exists(orig_path.as_path()).await.unwrap());

        let new_path = PathBuf::from("users").join("data-2").join("2.txt");
        assert!(storage
            .copy(orig_path.as_path(), new_path.as_path())
            .await
            .is_ok());

        assert!(store.exists(orig_path.as_path()).await.unwrap());
        assert!(store.exists(new_path.as_path()).await.unwrap());
    }

    fn single_storage() -> Storage {
        Storage::new(
            BTreeMap::from([("default".to_string(), drivers::mem::new())]),
            Box::new(SingleStrategy::new("default")) as Box<dyn StorageStrategy>,
        )
    }

    #[tokio::test]
    async fn can_check_exists() {
        let storage = single_storage();
        let path = PathBuf::from("users").join("data").join("1.txt");
        let missing_path = PathBuf::from("users").join("data").join("missing.txt");
        let file_content = Bytes::from("file content");

        assert!(storage.upload(path.as_path(), &file_content).await.is_ok());
        assert!(storage.exists(path.as_path()).await.unwrap());
        assert!(!storage.exists(missing_path.as_path()).await.unwrap());
    }

    #[tokio::test]
    async fn can_list_recursive() {
        let storage = single_storage();
        let file_content = Bytes::from("file content");
        let path_1 = PathBuf::from("a").join("1.txt");
        let path_2 = PathBuf::from("a").join("b").join("2.txt");

        assert!(storage
            .upload(path_1.as_path(), &file_content)
            .await
            .is_ok());
        assert!(storage
            .upload(path_2.as_path(), &file_content)
            .await
            .is_ok());

        let paths: Vec<_> = storage
            .list(Path::new("a"), true)
            .await
            .unwrap()
            .iter()
            .map(|e| e.path.as_str().to_string())
            .collect();
        assert!(paths.iter().any(|p| p == "a/1.txt"));
        assert!(paths.iter().any(|p| p == "a/b/2.txt"));
    }

    #[tokio::test]
    async fn can_list_non_recursive() {
        let storage = single_storage();
        let file_content = Bytes::from("file content");
        let path_1 = PathBuf::from("a").join("1.txt");
        let path_2 = PathBuf::from("a").join("b").join("2.txt");

        assert!(storage
            .upload(path_1.as_path(), &file_content)
            .await
            .is_ok());
        assert!(storage
            .upload(path_2.as_path(), &file_content)
            .await
            .is_ok());

        let paths: Vec<_> = storage
            .list(Path::new("a"), false)
            .await
            .unwrap()
            .iter()
            .map(|e| e.path.as_str().to_string())
            .collect();
        assert!(paths.iter().any(|p| p == "a/1.txt"));
        assert!(paths.iter().any(|p| p == "a/b/"));
        assert!(!paths.iter().any(|p| p == "a/b/2.txt"));
    }

    #[tokio::test]
    async fn can_stat() {
        let storage = single_storage();
        let path = PathBuf::from("users").join("data").join("1.txt");
        let file_content = Bytes::from("file content");

        assert!(storage.upload(path.as_path(), &file_content).await.is_ok());
        let entry = storage.stat(path.as_path()).await.unwrap();
        assert_eq!(entry.content_length, Some(file_content.len() as u64));
        assert!(!entry.is_dir);
    }

    #[tokio::test]
    async fn stat_missing_path_errors() {
        let storage = single_storage();
        let missing = PathBuf::from("users").join("data").join("missing.txt");
        assert!(storage.stat(missing.as_path()).await.is_err());
    }

    #[tokio::test]
    async fn list_missing_prefix_returns_empty() {
        let storage = single_storage();
        assert!(storage
            .list(Path::new("missing"), true)
            .await
            .unwrap()
            .is_empty());
    }

    /// `is_dir` is the only thing separating a common prefix from a key, and
    /// the recursive/non-recursive tests above match on `path` alone — which
    /// a driver returning `is_dir: false` for everything would still pass.
    #[tokio::test]
    async fn non_recursive_listing_marks_the_common_prefix_as_a_directory() {
        let storage = single_storage();
        let file_content = Bytes::from("file content");

        assert!(storage
            .upload(Path::new("a/1.txt"), &file_content)
            .await
            .is_ok());
        assert!(storage
            .upload(Path::new("a/b/2.txt"), &file_content)
            .await
            .is_ok());

        let entries = storage.list(Path::new("a"), false).await.unwrap();

        let dir = entries
            .iter()
            .find(|entry| entry.path == "a/b/")
            .expect("the child prefix is listed");
        assert!(dir.is_dir);

        let file = entries
            .iter()
            .find(|entry| entry.path == "a/1.txt")
            .expect("the key is listed");
        assert!(!file.is_dir);
    }

    /// The `*_with_policy` variants are the only way to override the strategy
    /// per call. Nothing else exercises them, so a facade method wired to
    /// `self.strategy` instead of its `strategy` argument would go unnoticed.
    #[tokio::test]
    async fn with_policy_routes_to_the_strategy_it_is_handed() {
        let storage = Storage::new(
            BTreeMap::from([
                ("default".to_string(), drivers::mem::new()),
                ("other".to_string(), drivers::mem::new()),
            ]),
            Box::new(SingleStrategy::new("default")) as Box<dyn StorageStrategy>,
        );
        let other = SingleStrategy::new("other");
        let path = PathBuf::from("only-in-default.txt");

        assert!(storage
            .upload(path.as_path(), &Bytes::from("file content"))
            .await
            .is_ok());

        assert!(storage.exists(path.as_path()).await.unwrap());
        assert!(!storage
            .exists_with_policy(path.as_path(), &other)
            .await
            .unwrap());

        assert_eq!(storage.list(Path::new(""), true).await.unwrap().len(), 1);
        assert!(storage
            .list_with_policy(Path::new(""), true, &other)
            .await
            .unwrap()
            .is_empty());

        assert!(storage.stat(path.as_path()).await.is_ok());
        assert!(storage
            .stat_with_policy(path.as_path(), &other)
            .await
            .is_err());
    }
}