Skip to content

Commit

Permalink
refactor: extract duplicate code to function
Browse files Browse the repository at this point in the history
  • Loading branch information
aphronyx committed May 17, 2024
1 parent 1feb711 commit 5472a3c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 50 deletions.
37 changes: 8 additions & 29 deletions src/rust/zh.copymanga/src/decryptor.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use crate::helper::to_aidoku_error;
use aes::{
cipher::{block_padding::Pkcs7, BlockDecryptMut, KeyIvInit},
Aes128,
};
use aidoku::{
error::{AidokuError, AidokuErrorKind, Result},
prelude::println,
std::String,
};
use aidoku::{error::Result, std::String};
use core::marker::Sized;

type Aes128CbcDec = cbc::Decryptor<Aes128>;
Expand All @@ -24,36 +21,18 @@ impl EncryptedString for String {
let encrypted_data = self.as_bytes();
// let iv = &encrypted_data[..16];
let Some(iv) = encrypted_data.get(..16) else {
println!("Failed to get `iv` from `encrypted_data`");

return Err(AidokuError {
reason: AidokuErrorKind::Unimplemented,
});
return Err(to_aidoku_error("Failed to get `iv` from `encrypted_data`"));
};
let Some(hex_ciphertext) = encrypted_data.get(16..) else {
println!("Failed to get `hex_ciphertext` from `encrypted_data`");

return Err(AidokuError {
reason: AidokuErrorKind::Unimplemented,
});
return Err(to_aidoku_error(
"Failed to get `hex_ciphertext` from `encrypted_data`",
));
};
let mut ciphertext = hex::decode(hex_ciphertext).map_err(|e| {
println!("{e}");

AidokuError {
reason: AidokuErrorKind::Unimplemented,
}
})?;
let mut ciphertext = hex::decode(hex_ciphertext).map_err(to_aidoku_error)?;

let plaintext = Aes128CbcDec::new(KEY.into(), iv.into())
.decrypt_padded_mut::<Pkcs7>(&mut ciphertext)
.map_err(|e| {
println!("{e}");

AidokuError {
reason: AidokuErrorKind::Unimplemented,
}
})?;
.map_err(to_aidoku_error)?;
Ok(Self::from_utf8_lossy(plaintext).into())
}
}
15 changes: 9 additions & 6 deletions src/rust/zh.copymanga/src/helper/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ use aidoku::{
error::{AidokuError, AidokuErrorKind, Result},
prelude::println,
};
use core::fmt::Display;
use regex::Regex as OriginalRegex;

pub struct Regex;

impl Regex {
#[allow(clippy::new_ret_no_self)]
pub fn new<S: AsRef<str>>(pat: S) -> Result<OriginalRegex> {
OriginalRegex::new(pat.as_ref()).map_err(|e| {
println!("{e}");
OriginalRegex::new(pat.as_ref()).map_err(to_aidoku_error)
}
}

pub fn to_aidoku_error<E: Display>(err: E) -> AidokuError {
println!("{err}");

AidokuError {
reason: AidokuErrorKind::Unimplemented,
}
})
AidokuError {
reason: AidokuErrorKind::Unimplemented,
}
}
23 changes: 8 additions & 15 deletions src/rust/zh.copymanga/src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::{helper::Regex, url::Url};
use crate::{
helper::{to_aidoku_error, Regex},
url::Url,
};
use aidoku::{
error::{AidokuError, AidokuErrorKind, Result},
prelude::{format, println},
error::{AidokuError, Result},
prelude::format,
std::{html::Node, json, ArrayRef, ObjectRef, String, ValueRef, Vec},
Manga, MangaPageResult, MangaStatus,
};
Expand Down Expand Up @@ -159,20 +162,10 @@ pub trait UuidString {
impl UuidString for String {
fn get_timestamp(&self) -> Result<f64> {
let Some(timestamp) = Uuid::from_str(self)
.map_err(|e| {
println!("{e}");

AidokuError {
reason: AidokuErrorKind::Unimplemented,
}
})?
.map_err(to_aidoku_error)?
.get_timestamp()
else {
println!("Failed to parse UUID to timestamp.");

return Err(AidokuError {
reason: AidokuErrorKind::Unimplemented,
});
return Err(to_aidoku_error("Failed to parse UUID to timestamp."));
};
let (integer_part, fractional_part) = timestamp.to_unix();

Expand Down

0 comments on commit 5472a3c

Please sign in to comment.