-
Notifications
You must be signed in to change notification settings - Fork 241
/
enums.rs
184 lines (166 loc) · 5.54 KB
/
enums.rs
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
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#![allow(dead_code)]
use aws_sdk_s3 as s3;
use std::error::Error;
// snippet-start:[testing.rust.enums-struct]
pub struct ListObjectsResult {
pub objects: Vec<s3::model::Object>,
pub continuation_token: Option<String>,
pub has_more: bool,
}
// snippet-end:[testing.rust.enums-struct]
// snippet-start:[testing.rust.enums-enum]
pub enum ListObjects {
Real(s3::Client),
#[cfg(test)]
Test {
expected_bucket: String,
expected_prefix: String,
pages: Vec<Vec<s3::model::Object>>,
},
}
// snippet-end:[testing.rust.enums-enum]
// snippet-start:[testing.rust.enums-list_objects]
impl ListObjects {
pub async fn list_objects(
&self,
bucket: &str,
prefix: &str,
continuation_token: Option<String>,
) -> Result<ListObjectsResult, Box<dyn Error + Send + Sync + 'static>> {
match self {
Self::Real(s3) => {
Self::real_list_objects(s3.clone(), bucket, prefix, continuation_token).await
}
#[cfg(test)]
Self::Test {
expected_bucket,
expected_prefix,
pages,
} => {
assert_eq!(expected_bucket, bucket);
assert_eq!(expected_prefix, prefix);
Self::test_list_objects(pages, continuation_token)
}
}
}
// snippet-end:[testing.rust.enums-list_objects]
// snippet-start:[testing.rust.enums-real-list-objects]
async fn real_list_objects(
s3: s3::Client,
bucket: &str,
prefix: &str,
continuation_token: Option<String>,
) -> Result<ListObjectsResult, Box<dyn Error + Send + Sync + 'static>> {
let response = s3
.list_objects_v2()
.bucket(bucket)
.prefix(prefix)
.set_continuation_token(continuation_token)
.send()
.await?;
Ok(ListObjectsResult {
objects: response.contents().unwrap_or_default().to_vec(),
continuation_token: response.continuation_token().map(|t| t.to_string()),
has_more: response.is_truncated(),
})
}
// snippet-end:[testing.rust.enums-real-list-objects]
// snippet-start:[testing.rust.enums-test]
#[cfg(test)]
fn test_list_objects(
pages: &[Vec<s3::model::Object>],
continuation_token: Option<String>,
) -> Result<ListObjectsResult, Box<dyn Error + Send + Sync + 'static>> {
use std::str::FromStr;
let index = continuation_token
.map(|t| usize::from_str(&t).expect("valid token"))
.unwrap_or_default();
if pages.is_empty() {
Ok(ListObjectsResult {
objects: Vec::new(),
continuation_token: None,
has_more: false,
})
} else {
Ok(ListObjectsResult {
objects: pages[index].clone(),
continuation_token: Some(format!("{}", index + 1)),
has_more: index + 1 < pages.len(),
})
}
}
}
// snippet-end:[testing.rust.enums-test]
// snippet-start:[testing.rust.enums-function]
async fn determine_prefix_file_size(
// Now we take an instance of our enum rather than the S3 client
list_objects_impl: ListObjects,
bucket: &str,
prefix: &str,
) -> Result<usize, Box<dyn Error + Send + Sync + 'static>> {
let mut next_token: Option<String> = None;
let mut total_size_bytes = 0;
loop {
let result = list_objects_impl
.list_objects(bucket, prefix, next_token.take())
.await?;
// Add up the file sizes we got back
for object in result.objects {
total_size_bytes += object.size() as usize;
}
// Handle pagination, and break the loop if there are no more pages
next_token = result.continuation_token;
if !result.has_more {
break;
}
}
Ok(total_size_bytes)
}
// snippet-end:[testing.rust.enums-function]
// snippet-start:[testing.rust.enums-tests]
#[tokio::test]
async fn test_single_page() {
use s3::model::Object;
// Create a TestListObjects instance with just one page of two objects in it
let fake = ListObjects::Test {
expected_bucket: "some-bucket".into(),
expected_prefix: "some-prefix".into(),
pages: vec![[5, 2i64]
.iter()
.map(|size| Object::builder().size(*size).build())
.collect()],
};
// Run the code we want to test with it
let size = determine_prefix_file_size(fake, "some-bucket", "some-prefix")
.await
.unwrap();
// Verify we got the correct total size back
assert_eq!(7, size);
}
#[tokio::test]
async fn test_multiple_pages() {
use s3::model::Object;
// This time, we add a helper function for making pages
fn make_page(sizes: &[i64]) -> Vec<Object> {
sizes
.iter()
.map(|size| Object::builder().size(*size).build())
.collect()
}
// Create the TestListObjects instance with two pages of objects now
let fake = ListObjects::Test {
expected_bucket: "some-bucket".into(),
expected_prefix: "some-prefix".into(),
pages: vec![make_page(&[5, 2]), make_page(&[3, 9])],
};
// And now test and verify
let size = determine_prefix_file_size(fake, "some-bucket", "some-prefix")
.await
.unwrap();
assert_eq!(19, size);
}
// snippet-end:[testing.rust.enums-tests]