@@ -156,31 +156,40 @@ pub enum SSECEncryptionKey {
156
156
} ,
157
157
}
158
158
159
+ #[ derive( Debug , thiserror:: Error ) ]
160
+ pub enum Error {
161
+ #[ error( "Expected SSE-C:AES256:<base64_encryption_key>" ) ]
162
+ UnexpectedKey ,
163
+ #[ error( "Only SSE-C is supported for object encryption for now" ) ]
164
+ UnexpectedProtocol ,
165
+ #[ error( "Invalid SSE algorithm. Following are supported: AES256" ) ]
166
+ InvalidAlgorithm ,
167
+ }
168
+
159
169
impl FromStr for SSECEncryptionKey {
160
- type Err = String ;
170
+ type Err = Error ;
161
171
162
172
fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
163
173
let parts = s. split ( ':' ) . collect :: < Vec < _ > > ( ) ;
164
- if parts. len ( ) == 3 {
165
- let sse_type = parts[ 0 ] ;
166
- if sse_type != "SSE-C" {
167
- return Err ( "Only SSE-C is supported for object encryption for now" . into ( ) ) ;
168
- }
174
+ if parts. len ( ) != 3 {
175
+ return Err ( Error :: UnexpectedKey ) ;
176
+ }
177
+ let sse_type = parts[ 0 ] ;
178
+ if sse_type != "SSE-C" {
179
+ return Err ( Error :: UnexpectedProtocol ) ;
180
+ }
169
181
170
- let algorithm = parts[ 1 ] ;
171
- let encryption_key = parts[ 2 ] ;
182
+ let algorithm = parts[ 1 ] ;
183
+ let encryption_key = parts[ 2 ] ;
172
184
173
- let alg = ObjectEncryptionAlgorithm :: from_str ( algorithm) ?;
185
+ let alg = ObjectEncryptionAlgorithm :: from_str ( algorithm) ?;
174
186
175
- Ok ( match alg {
176
- ObjectEncryptionAlgorithm :: Aes256 => SSECEncryptionKey :: SseC {
177
- _algorithm : alg,
178
- base64_encryption_key : encryption_key. to_owned ( ) ,
179
- } ,
180
- } )
181
- } else {
182
- Err ( "Expected SSE-C:AES256:<base64_encryption_key>" . into ( ) )
183
- }
187
+ Ok ( match alg {
188
+ ObjectEncryptionAlgorithm :: Aes256 => SSECEncryptionKey :: SseC {
189
+ _algorithm : alg,
190
+ base64_encryption_key : encryption_key. to_owned ( ) ,
191
+ } ,
192
+ } )
184
193
}
185
194
}
186
195
@@ -190,12 +199,12 @@ pub enum ObjectEncryptionAlgorithm {
190
199
}
191
200
192
201
impl FromStr for ObjectEncryptionAlgorithm {
193
- type Err = String ;
202
+ type Err = Error ;
194
203
195
204
fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
196
205
match s {
197
206
"AES256" => Ok ( ObjectEncryptionAlgorithm :: Aes256 ) ,
198
- _ => Err ( "Invalid SSE algorithm. Following are supported: AES256" . into ( ) ) ,
207
+ _ => Err ( Error :: InvalidAlgorithm ) ,
199
208
}
200
209
}
201
210
}
0 commit comments