@@ -67,6 +67,28 @@ pub fn get_file_list<P: AsRef<Path>>(path: P) -> Result<Vec<PathBuf>, Error> {
67
67
Ok ( files)
68
68
}
69
69
70
+ #[ derive( Debug , failure:: Fail ) ]
71
+ #[ fail( display = "invalid storage backend" ) ]
72
+ pub ( crate ) struct InvalidStorageBackendError ;
73
+
74
+ #[ derive( Debug ) ]
75
+ pub ( crate ) enum StorageBackendKind {
76
+ Database ,
77
+ S3 ,
78
+ }
79
+
80
+ impl std:: str:: FromStr for StorageBackendKind {
81
+ type Err = InvalidStorageBackendError ;
82
+
83
+ fn from_str ( input : & str ) -> Result < Self , Self :: Err > {
84
+ match input {
85
+ "database" => Ok ( StorageBackendKind :: Database ) ,
86
+ "s3" => Ok ( StorageBackendKind :: S3 ) ,
87
+ _ => Err ( InvalidStorageBackendError ) ,
88
+ }
89
+ }
90
+ }
91
+
70
92
enum StorageBackend {
71
93
Database ( DatabaseBackend ) ,
72
94
S3 ( Box < S3Backend > ) ,
@@ -78,29 +100,17 @@ pub struct Storage {
78
100
79
101
impl Storage {
80
102
pub fn new ( pool : Pool , metrics : Arc < Metrics > , config : & Config ) -> Result < Self , Error > {
81
- let backend = if let Some ( c) = s3:: s3_client ( ) {
82
- StorageBackend :: S3 ( Box :: new ( S3Backend :: new ( c, metrics, config) ?) )
83
- } else {
84
- StorageBackend :: Database ( DatabaseBackend :: new ( pool, metrics) )
85
- } ;
86
- Ok ( Storage { backend } )
87
- }
88
-
89
- #[ cfg( test) ]
90
- pub ( crate ) fn temp_new_s3 ( metrics : Arc < Metrics > , config : & Config ) -> Result < Self , Error > {
91
103
Ok ( Storage {
92
- backend : StorageBackend :: S3 ( Box :: new ( S3Backend :: new (
93
- s3:: s3_client ( ) . unwrap ( ) ,
94
- metrics,
95
- config,
96
- ) ?) ) ,
97
- } )
98
- }
99
-
100
- #[ cfg( test) ]
101
- pub ( crate ) fn temp_new_db ( pool : Pool , metrics : Arc < Metrics > ) -> Result < Self , Error > {
102
- Ok ( Storage {
103
- backend : StorageBackend :: Database ( DatabaseBackend :: new ( pool, metrics) ) ,
104
+ backend : match config. storage_backend {
105
+ StorageBackendKind :: Database => {
106
+ StorageBackend :: Database ( DatabaseBackend :: new ( pool, metrics) )
107
+ }
108
+ StorageBackendKind :: S3 => StorageBackend :: S3 ( Box :: new ( S3Backend :: new (
109
+ s3:: s3_client ( ) . unwrap ( ) ,
110
+ metrics,
111
+ config,
112
+ ) ?) ) ,
113
+ } ,
104
114
} )
105
115
}
106
116
@@ -564,18 +574,21 @@ mod backend_tests {
564
574
565
575
macro_rules! backend_tests {
566
576
(
567
- backends( $env : ident ) { $( $backend: ident => $create : expr, ) * }
577
+ backends { $( $backend: ident => $config : expr, ) * }
568
578
tests $tests: tt
569
579
tests_with_metrics $tests_with_metrics: tt
570
580
) => {
571
581
$(
572
582
mod $backend {
573
583
use crate :: test:: TestEnvironment ;
574
- use crate :: storage:: Storage ;
584
+ use crate :: storage:: { Storage , StorageBackendKind } ;
575
585
use std:: sync:: Arc ;
576
586
577
- fn get_storage( $env: & TestEnvironment ) -> Arc <Storage > {
578
- $create
587
+ fn get_storage( env: & TestEnvironment ) -> Arc <Storage > {
588
+ env. override_config( |config| {
589
+ config. storage_backend = $config;
590
+ } ) ;
591
+ env. storage( )
579
592
}
580
593
581
594
backend_tests!( @tests $tests) ;
@@ -606,9 +619,9 @@ mod backend_tests {
606
619
}
607
620
608
621
backend_tests ! {
609
- backends( env ) {
610
- s3 => env . s3 ( ) ,
611
- database => env . temp_storage_db ( ) ,
622
+ backends {
623
+ s3 => StorageBackendKind :: S3 ,
624
+ database => StorageBackendKind :: Database ,
612
625
}
613
626
614
627
tests {
0 commit comments