refactor: use '? ' as much as possible

This commit is contained in:
Sebastian Pravda 2022-11-14 23:04:46 +01:00
parent b0e2129cf4
commit f1692df760
No known key found for this signature in database
GPG key ID: F3BC84F08EFA3F57

View file

@ -43,19 +43,13 @@ impl CandidateService {
return Err(ServiceError::UserAlreadyExists); return Err(ServiceError::UserAlreadyExists);
} }
let Ok(hashed_password) = hash_password(plain_text_password.to_string()).await else { let hashed_password = hash_password(plain_text_password.to_string()).await?;
return Err(ServiceError::CryptoHashFailed);
};
let (pubkey, priv_key_plain_text) = crypto::create_identity(); let (pubkey, priv_key_plain_text) = crypto::create_identity();
let Ok(encrypted_priv_key) = crypto::encrypt_password(priv_key_plain_text, plain_text_password.to_string()).await else { let encrypted_priv_key = crypto::encrypt_password(priv_key_plain_text, plain_text_password.to_string()).await?;
return Err(ServiceError::CryptoEncryptFailed);
};
let Ok(hashed_personal_id_number) = hash_password(personal_id_number).await else { let hashed_personal_id_number = hash_password(personal_id_number).await?;
return Err(ServiceError::CryptoHashFailed);
};
// TODO: Specify root path in config? // TODO: Specify root path in config?
tokio::fs::create_dir_all(Path::new(&application_id.to_string()).join("cache")).await?; tokio::fs::create_dir_all(Path::new(&application_id.to_string()).join("cache")).await?;
@ -181,12 +175,8 @@ impl CandidateService {
let admin_public_keys = Query::get_all_admin_public_keys(db) let admin_public_keys = Query::get_all_admin_public_keys(db)
.await?; .await?;
let candidate = Query::find_candidate_by_id(db, candidate_id) let candidate = Query::find_candidate_by_id(db, candidate_id).await?
.await?; .ok_or(ServiceError::CandidateNotFound)?;
let Some(candidate) = candidate else {
return Err(ServiceError::CandidateNotFound);
};
let candidate_public_key = candidate.public_key; let candidate_public_key = candidate.public_key;
@ -210,20 +200,14 @@ impl CandidateService {
} }
pub async fn get_portfolio(candidate_id: i32, db: &DbConn) -> Result<Vec<u8>, ServiceError> { pub async fn get_portfolio(candidate_id: i32, db: &DbConn) -> Result<Vec<u8>, ServiceError> {
let candidate = Query::find_candidate_by_id(db, candidate_id) let candidate = Query::find_candidate_by_id(db, candidate_id).await?
.await?; .ok_or(ServiceError::CandidateNotFound)?;
let Some(candidate) = candidate else {
return Err(ServiceError::CandidateNotFound);
};
let candidate_public_key = candidate.public_key; let candidate_public_key = candidate.public_key;
let path = Path::new(&candidate_id.to_string()).join("PORTFOLIO.zip"); let path = Path::new(&candidate_id.to_string()).join("PORTFOLIO.zip");
let Ok(buffer) = crypto::decrypt_file_with_private_key_as_buffer(path, &candidate_public_key).await else { let buffer = crypto::decrypt_file_with_private_key_as_buffer(path, &candidate_public_key).await?;
return Err(ServiceError::CryptoDecryptFailed);
};
Ok(buffer) Ok(buffer)
} }
@ -234,11 +218,7 @@ impl CandidateService {
) -> Result<String, ServiceError> { ) -> Result<String, ServiceError> {
let private_key_encrypted = candidate.private_key; let private_key_encrypted = candidate.private_key;
let private_key = crypto::decrypt_password(private_key_encrypted, password).await; let private_key = crypto::decrypt_password(private_key_encrypted, password).await?;
let Ok(private_key) = private_key else {
return Err(ServiceError::CryptoDecryptFailed);
};
Ok(private_key) Ok(private_key)
} }