Code cleanup
This commit is contained in:
@ -20,8 +20,8 @@ async fn check_master_pass(
|
||||
|
||||
let is_valid = {
|
||||
let hash = HashedBytes::from(model);
|
||||
let master_pass = master_pass.to_owned();
|
||||
spawn_blocking(move || hash.verify(master_pass.as_bytes())).await?
|
||||
let master_pass: Box<[u8]> = master_pass.as_bytes().into();
|
||||
spawn_blocking(move || hash.verify(&master_pass)).await?
|
||||
};
|
||||
|
||||
if !is_valid {
|
||||
|
@ -31,16 +31,14 @@ fn validate_document(document: Option<&Document>) -> Result<&Document, &'static
|
||||
}
|
||||
|
||||
#[inline]
|
||||
async fn download_file(bot: &Throttle<Bot>, file: &FileMeta) -> crate::Result<Vec<u8>> {
|
||||
let path = bot.get_file(file.id.as_str()).await?.path;
|
||||
async fn download_file(bot: &Throttle<Bot>, file: &FileMeta) -> crate::Result<Box<[u8]>> {
|
||||
let path = bot.get_file(&file.id).await?.path;
|
||||
let mut data = Vec::with_capacity(file.size as usize);
|
||||
bot.download_file_stream(&path)
|
||||
.try_for_each(|bytes| {
|
||||
data.extend_from_slice(&bytes);
|
||||
async { Ok(()) }
|
||||
})
|
||||
.await?;
|
||||
Ok(data)
|
||||
let mut stream = bot.download_file_stream(&path);
|
||||
while let Some(bytes) = stream.try_next().await? {
|
||||
data.extend_from_slice(&bytes);
|
||||
}
|
||||
Ok(data.into_boxed_slice())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
@ -116,12 +114,12 @@ fn process_accounts(
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn user_from_vec(
|
||||
vector: Vec<u8>,
|
||||
fn user_from_bytes(
|
||||
bytes: impl AsRef<[u8]>,
|
||||
existing_names: ahash::HashSet<String>,
|
||||
) -> crate::Result<Result<User, String>> {
|
||||
let mut user: User = serde_json::from_slice(&vector)?;
|
||||
drop(vector);
|
||||
let mut user: User = serde_json::from_slice(bytes.as_ref())?;
|
||||
drop(bytes);
|
||||
match process_accounts(&mut user.accounts, existing_names)? {
|
||||
Ok(()) => Ok(Ok(user)),
|
||||
Err(error_text) => Ok(Err(error_text)),
|
||||
@ -136,11 +134,7 @@ async fn user_from_document(
|
||||
user_id: u64,
|
||||
) -> Result<User, Cow<'static, str>> {
|
||||
let (data, existing_names) = {
|
||||
let file = match validate_document(document) {
|
||||
Ok(document) => &document.file,
|
||||
Err(text) => return Err(Cow::Borrowed(text)),
|
||||
};
|
||||
|
||||
let file = &validate_document(document)?.file;
|
||||
let data = download_file(bot, file).map_err(|_| "Error downloading the file. Try again");
|
||||
|
||||
let existing_names = Account::get_names(user_id, db)
|
||||
@ -150,9 +144,8 @@ async fn user_from_document(
|
||||
try_join!(data, existing_names)?
|
||||
};
|
||||
|
||||
match spawn_blocking(|| user_from_vec(data, existing_names)).await {
|
||||
Ok(Ok(Ok(user))) => Ok(user),
|
||||
Ok(Ok(Err(error_text))) => Err(Cow::Owned(error_text)),
|
||||
match spawn_blocking(|| user_from_bytes(data, existing_names)).await {
|
||||
Ok(Ok(user)) => user.map_err(Cow::Owned),
|
||||
_ => Err(Cow::Borrowed("Error parsing the json file. Try again")),
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user