From 9efbaefd9240053e7ddb23da2e1b333a2fd2eeb5 Mon Sep 17 00:00:00 2001 From: Delta1925 Date: Thu, 13 Apr 2023 22:26:41 +0200 Subject: [PATCH] Refactor even more --- src/confirmation.rs | 6 +++--- src/main.rs | 4 ++-- src/management.rs | 50 ++++++++++++++++++++++----------------------- src/utils.rs | 37 ++++++++++++++++++--------------- 4 files changed, 49 insertions(+), 48 deletions(-) diff --git a/src/confirmation.rs b/src/confirmation.rs index 646dfaf..59da817 100644 --- a/src/confirmation.rs +++ b/src/confirmation.rs @@ -9,15 +9,15 @@ use std::path::Path; pub fn confirm_action(token: &str) -> Result<(), Error> { let pending_path = pending_path!().join(token); - let data = if pending_path.exists() { + let content = if pending_path.exists() { match fs::read_to_string(&pending_path) { - Ok(data) => data, + Ok(content) => content, Err(_) => return Err(Error::Inaccessible), } } else { return Err(Error::MissingPath); }; - let key = match serde_json::from_str::(&data) { + let key = match serde_json::from_str::(&content) { Ok(key) => key, Err(_) => return Err(Error::ParseStored), }; diff --git a/src/main.rs b/src/main.rs index 105a5e2..c7fee8d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,7 +27,7 @@ struct Pem { #[derive(Deserialize, Debug)] struct Token { - data: String, + value: String, } #[derive(Deserialize, Debug)] @@ -63,7 +63,7 @@ async fn submit(pem: web::Form) -> Result { #[get("/api/confirm/{data}")] async fn confirm(token: web::Path) -> Result { - confirm_action(&token.data)?; + confirm_action(&token.value)?; Ok(String::from("Confirmation successfull!")) } diff --git a/src/management.rs b/src/management.rs index 13d65e2..5dfd5d3 100644 --- a/src/management.rs +++ b/src/management.rs @@ -48,24 +48,25 @@ impl Pending { } fn store_pending(pending: &Pending, token: &str) -> Result<(), Error> { - match serde_json::to_string(pending) { - Ok(serialized) => match fs::write(pending_path!().join(token), serialized) { - Ok(_) => Ok(()), - Err(_) => Err(Error::Inaccessible), - }, - Err(_) => Err(Error::SerializeData), + let serialized = match serde_json::to_string(pending) { + Ok(serialized) => serialized, + Err(_) => return Err(Error::SerializeData), + }; + match fs::write(pending_path!().join(token), serialized) { + Ok(_) => Ok(()), + Err(_) => Err(Error::Inaccessible), } } pub fn store_pending_addition(pem: String, token: &str) -> Result<(), Error> { - let data = Pending::build_add(pem); - store_pending(&data, token)?; + let pending = Pending::build_add(pem); + store_pending(&pending, token)?; Ok(()) } pub fn store_pending_deletion(email: String, token: &str) -> Result<(), Error> { - let data = Pending::build_delete(email); - store_pending(&data, token)?; + let pending = Pending::build_delete(email); + store_pending(&pending, token)?; Ok(()) } @@ -73,23 +74,20 @@ pub fn clean_stale(max_age: i64) -> Result<(), Error> { for path in fs::read_dir(pending_path!()).unwrap().flatten() { let file_path = path.path(); if file_path.exists() { - match fs::read_to_string(&file_path) { - Ok(data) => match serde_json::from_str::(&data) { - Ok(key) => { - let now = Utc::now().timestamp(); - if now - key.timestamp() > max_age { - if fs::remove_file(&file_path).is_err() { - return Err(Error::Inaccessible); - } - println!( - "Deleted {}, since it was stale", - &file_path.to_str().unwrap() - ); - } - } - Err(_) => return Err(Error::DeserializeData), - }, + let content = match fs::read_to_string(&file_path) { + Ok(content) => content, Err(_) => return Err(Error::Inaccessible), + }; + let key = match serde_json::from_str::(&content) { + Ok(key) => key, + Err(_) => return Err(Error::DeserializeData), + }; + let now = Utc::now().timestamp(); + if now - key.timestamp() > max_age { + if fs::remove_file(&file_path).is_err() { + return Err(Error::Inaccessible); + } + println!("Deleted {}, since it was stale", &file_path.display()); } } } diff --git a/src/utils.rs b/src/utils.rs index 0e3103f..f8c2e5f 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -13,9 +13,9 @@ macro_rules! pending_path { }; } -pub fn parse_pem(data: &str) -> Result { - match sequoia_openpgp::Cert::from_bytes(data.as_bytes()) { - Ok(data) => Ok(data), +pub fn parse_pem(pemfile: &str) -> Result { + match sequoia_openpgp::Cert::from_bytes(pemfile.as_bytes()) { + Ok(cert) => Ok(cert), Err(_) => Err(Error::ParseCert), } } @@ -26,24 +26,27 @@ pub fn gen_random_token() -> String { } pub fn get_email_from_cert(cert: &Cert) -> Result { - match cert.userids().next() { - Some(data) => match data.email() { - Ok(data) => match data { - Some(data) => Ok(data), - None => Err(Error::MissingMail), - }, - Err(_) => Err(Error::ParseMail), - }, - None => Err(Error::ParseCert), + let userid_opt = match cert.userids().next() { + Some(userid_opt) => userid_opt, + None => return Err(Error::ParseCert), + }; + let email_opt = match userid_opt.email() { + Ok(email_opt) => email_opt, + Err(_) => return Err(Error::ParseMail), + }; + match email_opt { + Some(email) => Ok(email), + None => Err(Error::MissingMail), } } pub fn get_user_file_path(email: &str) -> Result { - match Url::from(email) { - Ok(data) => match data.to_file_path(VARIANT) { - Ok(data) => Ok(data), - Err(_) => Err(Error::ParseMail), - }, + let wkd_url = match Url::from(email) { + Ok(wkd_url) => wkd_url, + Err(_) => return Err(Error::ParseMail), + }; + match wkd_url.to_file_path(VARIANT) { + Ok(path) => Ok(path), Err(_) => Err(Error::ParseMail), } }