Skip to content

Commit 0204248

Browse files
author
Devdutt Shenoi
committed
refactor: Error replaces String
1 parent cde8edf commit 0204248

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

src/storage/s3.rs

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -156,31 +156,40 @@ pub enum SSECEncryptionKey {
156156
},
157157
}
158158

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+
159169
impl FromStr for SSECEncryptionKey {
160-
type Err = String;
170+
type Err = Error;
161171

162172
fn from_str(s: &str) -> Result<Self, Self::Err> {
163173
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+
}
169181

170-
let algorithm = parts[1];
171-
let encryption_key = parts[2];
182+
let algorithm = parts[1];
183+
let encryption_key = parts[2];
172184

173-
let alg = ObjectEncryptionAlgorithm::from_str(algorithm)?;
185+
let alg = ObjectEncryptionAlgorithm::from_str(algorithm)?;
174186

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+
})
184193
}
185194
}
186195

@@ -190,12 +199,12 @@ pub enum ObjectEncryptionAlgorithm {
190199
}
191200

192201
impl FromStr for ObjectEncryptionAlgorithm {
193-
type Err = String;
202+
type Err = Error;
194203

195204
fn from_str(s: &str) -> Result<Self, Self::Err> {
196205
match s {
197206
"AES256" => Ok(ObjectEncryptionAlgorithm::Aes256),
198-
_ => Err("Invalid SSE algorithm. Following are supported: AES256".into()),
207+
_ => Err(Error::InvalidAlgorithm),
199208
}
200209
}
201210
}

0 commit comments

Comments
 (0)