Skip to content

Commit

Permalink
refactor: replace let-else with ok_or_else
Browse files Browse the repository at this point in the history
  • Loading branch information
aphronyx committed May 17, 2024
1 parent 5472a3c commit bd63d6d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
15 changes: 6 additions & 9 deletions src/rust/zh.copymanga/src/decryptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,12 @@ pub trait EncryptedString {
impl EncryptedString for String {
fn decrypt(self) -> Result<Self> {
let encrypted_data = self.as_bytes();
// let iv = &encrypted_data[..16];
let Some(iv) = encrypted_data.get(..16) else {
return Err(to_aidoku_error("Failed to get `iv` from `encrypted_data`"));
};
let Some(hex_ciphertext) = encrypted_data.get(16..) else {
return Err(to_aidoku_error(
"Failed to get `hex_ciphertext` from `encrypted_data`",
));
};
let iv = encrypted_data
.get(..16)
.ok_or_else(|| to_aidoku_error("Failed to get `iv` from `encrypted_data`"))?;
let hex_ciphertext = encrypted_data.get(16..).ok_or_else(|| {
to_aidoku_error("Failed to get `hex_ciphertext` from `encrypted_data`")
})?;
let mut ciphertext = hex::decode(hex_ciphertext).map_err(to_aidoku_error)?;

let plaintext = Aes128CbcDec::new(KEY.into(), iv.into())
Expand Down
8 changes: 3 additions & 5 deletions src/rust/zh.copymanga/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,11 @@ pub trait UuidString {

impl UuidString for String {
fn get_timestamp(&self) -> Result<f64> {
let Some(timestamp) = Uuid::from_str(self)
let (integer_part, fractional_part) = Uuid::from_str(self)
.map_err(to_aidoku_error)?
.get_timestamp()
else {
return Err(to_aidoku_error("Failed to parse UUID to timestamp."));
};
let (integer_part, fractional_part) = timestamp.to_unix();
.ok_or_else(|| to_aidoku_error("Failed to parse UUID to timestamp."))?
.to_unix();

Ok((integer_part as f64) + (fractional_part as f64 * 10e-10))
}
Expand Down

0 comments on commit bd63d6d

Please sign in to comment.