From 267135bf73a960f7c439040ec1454bb86eaf7d1d Mon Sep 17 00:00:00 2001 From: Philipp Hofer Date: Thu, 5 Dec 2024 23:38:36 +0100 Subject: [PATCH] linting + proper time formatting --- src/tera/ergo.rs | 120 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 84 insertions(+), 36 deletions(-) diff --git a/src/tera/ergo.rs b/src/tera/ergo.rs index 3a7330e..e3c4f1f 100644 --- a/src/tera/ergo.rs +++ b/src/tera/ergo.rs @@ -17,7 +17,10 @@ use sqlx::SqlitePool; use tera::Context; use crate::model::{ - log::Log, notification::Notification, role::Role, user::{AdminUser, User, UserWithDetails} + log::Log, + notification::Notification, + role::Role, + user::{AdminUser, User, UserWithDetails}, }; #[derive(Serialize)] @@ -101,13 +104,13 @@ async fn index(db: &State, user: User, flash: Option, - data: Form, - user: User, -) -> Flash { +async fn new_user(db: &State, data: Form, user: User) -> Flash { if user.has_role(db, "ergo").await { - return - Flash::error(Redirect::to("/ergo"), "Du hast deine Daten schon eingegeben. Wenn du sie updaten willst, melde dich bitte bei it@rudernlinz.at"); + return Flash::error(Redirect::to("/ergo"), "Du hast deine Daten schon eingegeben. Wenn du sie updaten willst, melde dich bitte bei it@rudernlinz.at"); } // check data - if data.birthyear <1900 || data.birthyear > chrono::Utc::now().year() - 5 { - return - Flash::error(Redirect::to("/ergo"), "Bitte überprüfe dein Geburtsjahr..."); + if data.birthyear < 1900 || data.birthyear > chrono::Utc::now().year() - 5 { + return Flash::error(Redirect::to("/ergo"), "Bitte überprüfe dein Geburtsjahr..."); } if data.weight < 20 || data.weight > 200 { - return - Flash::error(Redirect::to("/ergo"), "Bitte überprüfe dein Gewicht..."); + return Flash::error(Redirect::to("/ergo"), "Bitte überprüfe dein Gewicht..."); } - if &data.sex != "f" && &data.sex != "m"{ - return - Flash::error(Redirect::to("/ergo"), "Bitte überprüfe dein Geschlecht..."); + if &data.sex != "f" && &data.sex != "m" { + return Flash::error(Redirect::to("/ergo"), "Bitte überprüfe dein Geschlecht..."); } // set data - user.update_ergo(db, data.birthyear, data.weight, &data.sex).await; + user.update_ergo(db, data.birthyear, data.weight, &data.sex) + .await; // inform all other `ergo` users let ergo = Role::find_by_name(db, "ergo").await.unwrap(); - Notification::create_for_role(db, &ergo, &format!("{} nimmt heuer an der Ergochallenge teil 💪", user.name), "Ergo Challenge", None, None).await; - + Notification::create_for_role( + db, + &ergo, + &format!("{} nimmt heuer an der Ergochallenge teil 💪", user.name), + "Ergo Challenge", + None, + None, + ) + .await; + // add to `ergo` group - user.add_role(db,&ergo).await.unwrap(); - - Flash::success(Redirect::to("/ergo"), "Du hast deine Daten erfolgreich eingegeben. Viel Spaß beim Schwitzen :-)") + user.add_role(db, &ergo).await.unwrap(); + + Flash::success( + Redirect::to("/ergo"), + "Du hast deine Daten erfolgreich eingegeben. Viel Spaß beim Schwitzen :-)", + ) } #[derive(FromForm, Debug)] @@ -209,7 +216,7 @@ async fn new_thirty( if let Err(e) = data.proof.move_copy_to(file_path).await { eprintln!("Failed to persist file: {:?}", e); } - + let result = data.result.trim_start_matches(|c| c == '0' || c == ' '); sqlx::query!( @@ -228,12 +235,28 @@ async fn new_thirty( .await; let ergo = Role::find_by_name(db, "ergo").await.unwrap(); - Notification::create_for_role(db, &ergo, &format!("{} ist gerade die Dirty Thirty Challenge gefahren 🥵", user.name), "Ergo Challenge", Some("/ergo"), None).await; + Notification::create_for_role( + db, + &ergo, + &format!( + "{} ist gerade die Dirty Thirty Challenge gefahren 🥵", + user.name + ), + "Ergo Challenge", + Some("/ergo"), + None, + ) + .await; Flash::success(Redirect::to("/ergo"), "Erfolgreich eingetragen") } fn format_time(input: &str) -> String { + let input = if input.starts_with(":") { + &format!("00{input}") + } else { + input + }; let mut parts: Vec<&str> = input.split(':').collect(); // If there's only seconds (e.g., "24.2"), treat it as "00:00:24.2" @@ -248,15 +271,29 @@ fn format_time(input: &str) -> String { } // Now parts should have [hours, minutes, seconds] - let hours = if parts[0].len() == 1 { format!("0{}", parts[0]) } else { parts[0].to_string() }; - let minutes = if parts[1].len() == 1 { format!("0{}", parts[1]) } else { parts[1].to_string() }; + let hours = if parts[0].len() == 1 { + format!("0{}", parts[0]) + } else { + parts[0].to_string() + }; + let minutes = if parts[1].len() == 1 { + format!("0{}", parts[1]) + } else { + parts[1].to_string() + }; let seconds = parts[2]; // Split seconds into whole and fractional parts let (sec_int, sec_frac) = seconds.split_once('.').unwrap_or((seconds, "0")); // Format the time as "hh:mm:ss.s" - format!("{}:{}:{}.{:1}", hours, minutes, sec_int, sec_frac.chars().next().unwrap_or('0')) + format!( + "{}:{}:{}.{:1}", + hours, + minutes, + sec_int, + sec_frac.chars().next().unwrap_or('0') + ) } #[post("/dozen", data = "", format = "multipart/form-data")] @@ -283,9 +320,9 @@ async fn new_dozen( } let result = data.result.trim_start_matches(|c| c == '0' || c == ' '); let result = if result.contains(":") || result.contains(".") { - format_time(result) - }else{ -result.to_string() + format_time(result) + } else { + result.to_string() }; sqlx::query!( @@ -304,7 +341,18 @@ result.to_string() .await; let ergo = Role::find_by_name(db, "ergo").await.unwrap(); - Notification::create_for_role(db, &ergo, &format!("{} ist gerade die Dirty Dozen Challenge gefahren 🥵", user.name), "Ergo Challenge", Some("/ergo"), None).await; + Notification::create_for_role( + db, + &ergo, + &format!( + "{} ist gerade die Dirty Dozen Challenge gefahren 🥵", + user.name + ), + "Ergo Challenge", + Some("/ergo"), + None, + ) + .await; Flash::success(Redirect::to("/ergo"), "Erfolgreich eingetragen") }