11use crate :: USER_AGENT ;
22use anyhow:: bail;
3- use aws_config:: { BehaviorVersion , Region , meta:: region:: RegionProviderChain } ;
4- use aws_sdk_s3:: {
5- Client ,
6- config:: { AppName , Credentials } ,
7- } ;
83use bytes:: Bytes ;
94use std:: {
105 borrow:: Cow ,
116 path:: { Path , PathBuf } ,
127} ;
138use url:: Url ;
149
10+ #[ cfg( feature = "s3" ) ]
11+ use aws_config:: { BehaviorVersion , Region , meta:: region:: RegionProviderChain } ;
12+ #[ cfg( feature = "s3" ) ]
13+ use aws_sdk_s3:: {
14+ Client ,
15+ config:: { AppName , Credentials } ,
16+ } ;
17+
1518#[ derive( Clone , Debug , PartialEq , Eq ) ]
19+ #[ non_exhaustive]
1620pub enum Source {
1721 Path ( PathBuf ) ,
1822 Http ( Url ) ,
23+ #[ cfg( feature = "s3" ) ]
1924 S3 ( S3 ) ,
2025}
2126
2227impl TryFrom < & str > for Source {
2328 type Error = anyhow:: Error ;
2429
2530 fn try_from ( value : & str ) -> Result < Self , Self :: Error > {
26- Ok (
27- if value. starts_with ( "http://" ) || value. starts_with ( "https://" ) {
28- Self :: Http ( Url :: parse ( value) ?)
29- } else if value. starts_with ( "s3://" ) {
30- Self :: S3 ( S3 :: try_from ( value) ?)
31- } else {
32- Self :: Path ( value. into ( ) )
33- } ,
34- )
31+ if value. starts_with ( "http://" ) || value. starts_with ( "https://" ) {
32+ return Ok ( Self :: Http ( Url :: parse ( value) ?) ) ;
33+ }
34+
35+ #[ cfg( feature = "s3" ) ]
36+ if value. starts_with ( "s3://" ) {
37+ return Ok ( Self :: S3 ( S3 :: try_from ( value) ?) ) ;
38+ }
39+
40+ Ok ( Self :: Path ( value. into ( ) ) )
3541 }
3642}
3743
@@ -42,6 +48,7 @@ impl Source {
4248 . into_iter ( )
4349 . map ( Self :: Path )
4450 . collect ( ) ) ,
51+ #[ cfg( feature = "s3" ) ]
4552 Self :: S3 ( s3) if s3. key . is_none ( ) => Ok ( Self :: discover_s3 ( s3)
4653 . await ?
4754 . into_iter ( )
@@ -77,6 +84,7 @@ impl Source {
7784 }
7885 }
7986
87+ #[ cfg( feature = "s3" ) ]
8088 async fn discover_s3 ( s3 : S3 ) -> anyhow:: Result < Vec < S3 > > {
8189 let client = s3. client ( ) . await ?;
8290
@@ -110,6 +118,7 @@ impl Source {
110118 match self {
111119 Self :: Path ( path) => path. to_string_lossy ( ) ,
112120 Self :: Http ( url) => url. as_str ( ) . into ( ) ,
121+ #[ cfg( feature = "s3" ) ]
113122 Self :: S3 ( s3) => format ! (
114123 "s3://{}/{}/{}" ,
115124 s3. region,
@@ -131,6 +140,7 @@ impl Source {
131140 . bytes ( )
132141 . await ?
133142 }
143+ #[ cfg( feature = "s3" ) ]
134144 Self :: S3 ( s3) => {
135145 let client = s3. client ( ) ;
136146 client
@@ -163,6 +173,7 @@ impl Source {
163173 . send ( )
164174 . await ?;
165175 }
176+ #[ cfg( feature = "s3" ) ]
166177 Self :: S3 ( s3) => {
167178 // delete the object from the bucket
168179 let client = s3. client ( ) ;
@@ -194,6 +205,7 @@ impl Source {
194205 // no-op, but warn
195206 log:: warn!( "Unable to move HTTP source ({url}), skipping!" ) ;
196207 }
208+ #[ cfg( feature = "s3" ) ]
197209 Self :: S3 ( s3) => {
198210 let client = s3. client ( ) ;
199211 client
@@ -211,6 +223,7 @@ impl Source {
211223 }
212224}
213225
226+ #[ cfg( feature = "s3" ) ]
214227#[ derive( Clone , Debug , PartialEq , Eq ) ]
215228pub struct S3 {
216229 region : String ,
@@ -219,6 +232,7 @@ pub struct S3 {
219232 key : Option < String > ,
220233}
221234
235+ #[ cfg( feature = "s3" ) ]
222236impl TryFrom < & str > for S3 {
223237 type Error = anyhow:: Error ;
224238
@@ -257,6 +271,7 @@ impl TryFrom<&str> for S3 {
257271 }
258272}
259273
274+ #[ cfg( feature = "s3" ) ]
260275impl S3 {
261276 pub async fn client ( & self ) -> anyhow:: Result < Client > {
262277 let region_provider = RegionProviderChain :: first_try ( Region :: new ( self . region . clone ( ) ) ) ;
@@ -280,6 +295,7 @@ impl S3 {
280295mod tests {
281296 use super :: * ;
282297
298+ #[ cfg( feature = "s3" ) ]
283299 #[ test]
284300 fn parse_s3 ( ) {
285301 assert_eq ! (
@@ -311,6 +327,7 @@ mod tests {
311327 ) ;
312328 }
313329
330+ #[ cfg( feature = "s3" ) ]
314331 #[ test]
315332 fn parse_s3_custom_region ( ) {
316333 assert_eq ! (
0 commit comments