From f7d109f1b226611a187b5cc66340c3287efa7c06 Mon Sep 17 00:00:00 2001 From: Philipp Hofer Date: Thu, 9 Jan 2025 15:31:05 +0100 Subject: [PATCH] cargo clippy --- src/model/boathouse.rs | 154 ++++++++++++++++------------ src/model/logbook.rs | 4 - src/model/personal/equatorprice.rs | 72 ++++++------- src/model/user/mod.rs | 68 ++++++------ src/tera/admin/user.rs | 4 +- src/tera/board/achievement.rs | 6 +- src/tera/boatdamage.rs | 4 +- src/tera/ergo.rs | 4 +- src/tera/log.rs | 2 +- src/tera/misc.rs | 2 +- templates/board/boathouse.html.tera | 7 +- 11 files changed, 174 insertions(+), 153 deletions(-) diff --git a/src/model/boathouse.rs b/src/model/boathouse.rs index 0e96caf..03d0f78 100644 --- a/src/model/boathouse.rs +++ b/src/model/boathouse.rs @@ -1,5 +1,3 @@ -use std::collections::HashMap; - use rocket::serde::{Deserialize, Serialize}; use sqlx::{FromRow, SqlitePool}; @@ -7,6 +5,93 @@ use crate::tera::board::boathouse::FormBoathouseToAdd; use super::boat::Boat; +#[derive(Debug, Serialize, Deserialize)] +pub struct BoathousePlace { + boat: Boat, + boathouse_id: i64, +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct BoathouseRack { + boats: [Option; 12], +} + +impl BoathouseRack { + fn new() -> Self { + let boats = [ + None, None, None, None, None, None, None, None, None, None, None, None, + ]; + Self { boats } + } + + async fn add(&mut self, db: &SqlitePool, boathouse: Boathouse) { + self.boats[boathouse.level as usize] = Some(BoathousePlace { + boat: Boat::find_by_id(db, boathouse.boat_id as i32) + .await + .unwrap(), + boathouse_id: boathouse.id, + }); + } +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct BoathouseSide { + mountain: BoathouseRack, + water: BoathouseRack, +} + +impl BoathouseSide { + fn new() -> Self { + Self { + mountain: BoathouseRack::new(), + water: BoathouseRack::new(), + } + } + + async fn add(&mut self, db: &SqlitePool, boathouse: Boathouse) { + match boathouse.side.as_str() { + "mountain" => self.mountain.add(db, boathouse).await, + "water" => self.water.add(db, boathouse).await, + _ => panic!("db constraint failed"), + } + } +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct BoathouseAisles { + mountain: BoathouseSide, + middle: BoathouseSide, + water: BoathouseSide, +} + +impl BoathouseAisles { + fn new() -> Self { + Self { + mountain: BoathouseSide::new(), + middle: BoathouseSide::new(), + water: BoathouseSide::new(), + } + } + + async fn add(&mut self, db: &SqlitePool, boathouse: Boathouse) { + match boathouse.aisle.as_str() { + "water" => self.water.add(db, boathouse).await, + "middle" => self.middle.add(db, boathouse).await, + "mountain" => self.mountain.add(db, boathouse).await, + _ => panic!("db constraint failed"), + }; + } + + pub async fn from(db: &SqlitePool, boathouses: Vec) -> Self { + let mut ret = BoathouseAisles::new(); + + for boathouse in boathouses { + ret.add(db, boathouse).await; + } + ret + } +} + #[derive(FromRow, Debug, Serialize, Deserialize)] pub struct Boathouse { pub id: i64, @@ -17,54 +102,7 @@ pub struct Boathouse { } impl Boathouse { - pub async fn get(db: &SqlitePool) -> HashMap<&str, HashMap<&str, [Option<(i64, Boat)>; 12]>> { - let mut ret: HashMap<&str, HashMap<&str, [Option<(i64, Boat)>; 12]>> = HashMap::new(); - - let mut mountain = HashMap::new(); - mountain.insert( - "mountain", - [ - None, None, None, None, None, None, None, None, None, None, None, None, - ], - ); - mountain.insert( - "water", - [ - None, None, None, None, None, None, None, None, None, None, None, None, - ], - ); - ret.insert("mountain-aisle", mountain); - - let mut middle = HashMap::new(); - middle.insert( - "mountain", - [ - None, None, None, None, None, None, None, None, None, None, None, None, - ], - ); - middle.insert( - "water", - [ - None, None, None, None, None, None, None, None, None, None, None, None, - ], - ); - ret.insert("middle-aisle", middle); - - let mut water = HashMap::new(); - water.insert( - "mountain", - [ - None, None, None, None, None, None, None, None, None, None, None, None, - ], - ); - water.insert( - "water", - [ - None, None, None, None, None, None, None, None, None, None, None, None, - ], - ); - ret.insert("water-aisle", water); - + pub async fn get(db: &SqlitePool) -> BoathouseAisles { let boathouses = sqlx::query_as!( Boathouse, "SELECT id, boat_id, aisle, side, level FROM boathouse" @@ -73,21 +111,7 @@ impl Boathouse { .await .unwrap(); //TODO: fixme - for boathouse in boathouses { - let aisle = ret - .get_mut(format!("{}-aisle", boathouse.aisle).as_str()) - .unwrap(); - let side = aisle.get_mut(boathouse.side.as_str()).unwrap(); - - side[boathouse.level as usize] = Some(( - boathouse.id, - Boat::find_by_id(db, boathouse.boat_id as i32) - .await - .unwrap(), - )); - } - - ret + BoathouseAisles::from(db, boathouses).await } pub async fn create(db: &SqlitePool, data: FormBoathouseToAdd) -> Result<(), String> { diff --git a/src/model/logbook.rs b/src/model/logbook.rs index 076f2a6..4b104bf 100644 --- a/src/model/logbook.rs +++ b/src/model/logbook.rs @@ -34,7 +34,6 @@ impl PartialEq for Logbook { pub(crate) enum Filter { SingleDayOnly, MultiDayOnly, - None, } #[derive(FromForm, Debug, Clone)] @@ -398,9 +397,6 @@ ORDER BY departure DESC ret.push(LogbookWithBoatAndRowers::from_tx(db, log).await); } } - Filter::None => { - ret.push(LogbookWithBoatAndRowers::from_tx(db, log).await); - } } } if exclude_last_log { diff --git a/src/model/personal/equatorprice.rs b/src/model/personal/equatorprice.rs index a03d9eb..6190163 100644 --- a/src/model/personal/equatorprice.rs +++ b/src/model/personal/equatorprice.rs @@ -3,62 +3,62 @@ use serde::Serialize; #[derive(Serialize, PartialEq, Debug)] pub(crate) enum Level { - NONE, - BRONZE, - SILVER, - GOLD, - DIAMOND, - DONE, + None, + Bronze, + Silver, + Gold, + Diamond, + Done, } impl Level { fn required_km(&self) -> i32 { match self { - Level::BRONZE => 40000, - Level::SILVER => 80000, - Level::GOLD => 100000, - Level::DIAMOND => 200000, - Level::DONE => 0, - Level::NONE => 0, + Level::Bronze => 40000, + Level::Silver => 80000, + Level::Gold => 100000, + Level::Diamond => 200000, + Level::Done => 0, + Level::None => 0, } } fn next_level(km: i32) -> Self { - if km < Level::BRONZE.required_km() { - Level::BRONZE - } else if km < Level::SILVER.required_km() { - Level::SILVER - } else if km < Level::GOLD.required_km() { - Level::GOLD - } else if km < Level::DIAMOND.required_km() { - Level::DIAMOND + if km < Level::Bronze.required_km() { + Level::Bronze + } else if km < Level::Silver.required_km() { + Level::Silver + } else if km < Level::Gold.required_km() { + Level::Gold + } else if km < Level::Diamond.required_km() { + Level::Diamond } else { - Level::DONE + Level::Done } } pub(crate) fn curr_level(km: i32) -> Self { - if km < Level::BRONZE.required_km() { - Level::NONE - } else if km < Level::SILVER.required_km() { - Level::BRONZE - } else if km < Level::GOLD.required_km() { - Level::SILVER - } else if km < Level::DIAMOND.required_km() { - Level::GOLD + if km < Level::Bronze.required_km() { + Level::None + } else if km < Level::Silver.required_km() { + Level::Bronze + } else if km < Level::Gold.required_km() { + Level::Silver + } else if km < Level::Diamond.required_km() { + Level::Gold } else { - Level::DIAMOND + Level::Diamond } } pub(crate) fn desc(&self) -> &str { match self { - Level::BRONZE => "Bronze", - Level::SILVER => "Silber", - Level::GOLD => "Gold", - Level::DIAMOND => "Diamant", - Level::DONE => "", - Level::NONE => "-", + Level::Bronze => "Bronze", + Level::Silver => "Silber", + Level::Gold => "Gold", + Level::Diamond => "Diamant", + Level::Done => "", + Level::None => "-", } } } diff --git a/src/model/user/mod.rs b/src/model/user/mod.rs index 6f27ea8..a72ed3b 100644 --- a/src/model/user/mod.rs +++ b/src/model/user/mod.rs @@ -395,12 +395,10 @@ ASKÖ Ruderverein Donau Linz", self.name), } } else if self.has_role(db, "Ehrenmitglied").await { fee.add("Ehrenmitglied".into(), 0); + } else if halfprice { + fee.add("Mitgliedsbeitrag (Halbpreis)".into(), REGULAR / 2); } else { - if halfprice { - fee.add("Mitgliedsbeitrag (Halbpreis)".into(), REGULAR / 2); - } else { - fee.add("Mitgliedsbeitrag".into(), REGULAR); - } + fee.add("Mitgliedsbeitrag".into(), REGULAR); } } @@ -994,39 +992,43 @@ ORDER BY last_access DESC smtp_pw: &str, ) { if self.has_role_tx(db, "scheckbuch").await { - let amount_trips = Logbook::completed_with_user_tx(db, &self).await.len(); - if amount_trips == 5 { - if let Some(mail) = &self.mail { - let _ = self.send_end_mail_scheckbuch(db, mail, smtp_pw).await; + let amount_trips = Logbook::completed_with_user_tx(db, self).await.len(); + match amount_trips { + 5 => { + if let Some(mail) = &self.mail { + let _ = self.send_end_mail_scheckbuch(db, mail, smtp_pw).await; + } + Notification::create_for_steering_people_tx( + db, + &format!( + "Liebe Steuerberechtigte, {} hat alle Ausfahrten des Scheckbuchs absolviert. Hoffentlich können wir uns bald über ein neues Mitglied freuen :-)", + self.name + ), + "Scheckbuch fertig", + None,None + ) + .await; } - Notification::create_for_steering_people_tx( - db, - &format!( - "Liebe Steuerberechtigte, {} hat alle Ausfahrten des Scheckbuchs absolviert. Hoffentlich können wir uns bald über ein neues Mitglied freuen :-)", - self.name - ), - "Scheckbuch fertig", - None,None - ) - .await; - } else if amount_trips > 5 { - let board = Role::find_by_name_tx(db, "Vorstand").await.unwrap(); - Notification::create_for_role_tx( - db, - &board, - &format!( - "Lieber Vorstand, {} hat nun bereits die {}. seiner 5 Scheckbuchausfahrten absolviert.", - self.name, amount_trips - ), - "Scheckbuch überfertig", - None,None - ) - .await; + a if a > 5 => { + let board = Role::find_by_name_tx(db, "Vorstand").await.unwrap(); + Notification::create_for_role_tx( + db, + &board, + &format!( + "Lieber Vorstand, {} hat nun bereits die {}. seiner 5 Scheckbuchausfahrten absolviert.", + self.name, amount_trips + ), + "Scheckbuch überfertig", + None,None + ) + .await; + } + _ => {} } } // check fahrtenabzeichen fertig - if rowingbadge::Status::completed_with_last_log(db, &self).await { + if rowingbadge::Status::completed_with_last_log(db, self).await { let board = Role::find_by_name_tx(db, "Vorstand").await.unwrap(); Notification::create_for_role_tx( db, diff --git a/src/tera/admin/user.rs b/src/tera/admin/user.rs index 3c7564d..8cea3d8 100644 --- a/src/tera/admin/user.rs +++ b/src/tera/admin/user.rs @@ -408,7 +408,7 @@ async fn create_scheckbuch( format!("{} created new scheckbuch: {data:?}", admin.name), ) .await; - Flash::success(Redirect::to("/admin/user/scheckbuch"), &format!("Scheckbuch erfolgreich erstellt. Eine E-Mail in der alles erklärt wird, wurde an {mail} verschickt.")) + Flash::success(Redirect::to("/admin/user/scheckbuch"), format!("Scheckbuch erfolgreich erstellt. Eine E-Mail in der alles erklärt wird, wurde an {mail} verschickt.")) } #[get("/user/move/schnupperant//to/scheckbuch")] @@ -458,7 +458,7 @@ async fn schnupper_to_scheckbuch( ), ) .await; - Flash::success(Redirect::to("/admin/schnupper"), &format!("Scheckbuch erfolgreich erstellt. Eine E-Mail in der alles erklärt wird, wurde an {} verschickt.", user.mail.unwrap())) + Flash::success(Redirect::to("/admin/schnupper"), format!("Scheckbuch erfolgreich erstellt. Eine E-Mail in der alles erklärt wird, wurde an {} verschickt.", user.mail.unwrap())) } pub fn routes() -> Vec { diff --git a/src/tera/board/achievement.rs b/src/tera/board/achievement.rs index 0ac17c3..750ab25 100644 --- a/src/tera/board/achievement.rs +++ b/src/tera/board/achievement.rs @@ -18,12 +18,12 @@ async fn index( context.insert("flash", &msg.into_inner()); } - let role = Role::find_by_name(&db, "Donau Linz").await.unwrap(); - let users = User::all_with_role(&db, &role).await; + let role = Role::find_by_name(db, "Donau Linz").await.unwrap(); + let users = User::all_with_role(db, &role).await; let mut people = Vec::new(); let mut rowingbadge_year = None; for user in users { - let achievement = Achievements::for_user(&db, &user).await; + let achievement = Achievements::for_user(db, &user).await; if let Some(badge) = &achievement.rowingbadge { rowingbadge_year = Some(badge.year); } diff --git a/src/tera/boatdamage.rs b/src/tera/boatdamage.rs index ec80954..2e345ac 100644 --- a/src/tera/boatdamage.rs +++ b/src/tera/boatdamage.rs @@ -148,13 +148,13 @@ async fn fixed<'r>( #[derive(FromForm)] pub struct FormBoatDamageVerified<'r> { - pub desc: &'r str, + desc: &'r str, } #[post("//verified", data = "")] async fn verified<'r>( db: &State, - data: Form>, + data: Form>, boatdamage_id: i32, techuser: TechUser, ) -> Flash { diff --git a/src/tera/ergo.rs b/src/tera/ergo.rs index e3c4f1f..31b3d0d 100644 --- a/src/tera/ergo.rs +++ b/src/tera/ergo.rs @@ -217,7 +217,7 @@ async fn new_thirty( eprintln!("Failed to persist file: {:?}", e); } - let result = data.result.trim_start_matches(|c| c == '0' || c == ' '); + let result = data.result.trim_start_matches(['0', ' ']); sqlx::query!( "UPDATE user SET dirty_thirty = ? where id = ?", @@ -318,7 +318,7 @@ async fn new_dozen( 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 == ' '); + let result = data.result.trim_start_matches(['0', ' ']); let result = if result.contains(":") || result.contains(".") { format_time(result) } else { diff --git a/src/tera/log.rs b/src/tera/log.rs index e9f3746..ae82c3b 100644 --- a/src/tera/log.rs +++ b/src/tera/log.rs @@ -312,7 +312,7 @@ async fn update( let data = data.into_inner(); let Some(logbook) = Logbook::find_by_id(db, data.id).await else { - return Flash::error(Redirect::to("/log"), &format!("Logbucheintrag kann nicht bearbeitet werden, da es einen Logbuch-Eintrag mit ID={} nicht gibt", data.id)); + return Flash::error(Redirect::to("/log"), format!("Logbucheintrag kann nicht bearbeitet werden, da es einen Logbuch-Eintrag mit ID={} nicht gibt", data.id)); }; match logbook.update(db, data.clone(), &user.user).await { diff --git a/src/tera/misc.rs b/src/tera/misc.rs index 4d0e76e..929694e 100644 --- a/src/tera/misc.rs +++ b/src/tera/misc.rs @@ -19,7 +19,7 @@ async fn cal_registered( return Err("Invalid".into()); }; - if &user.user_token != uuid { + if user.user_token != uuid { return Err("Invalid".into()); } diff --git a/templates/board/boathouse.html.tera b/templates/board/boathouse.html.tera index 29e5665..9223dd3 100644 --- a/templates/board/boathouse.html.tera +++ b/templates/board/boathouse.html.tera @@ -4,13 +4,12 @@ {% extends "base" %} {% macro show_place(aisle_name, side_name, level) %}
  • - {% set aisle = aisle_name ~ "-aisle" %} - {% set place = boathouse[aisle][side_name] %} + {% set place = boathouse[aisle_name][side_name].boats %} {% if place[level] %} - {{ place[level].1.name }} + {{ place[level].boat.name }} {% if "admin" in loggedin_user.roles %} X + href="/board/boathouse/{{ place[level].boathouse_id }}/delete">X {% endif %} {% elif boats | length > 0 %} {% if "admin" in loggedin_user.roles %}