From 76375382372eb631bc15b2c187efff5e59cb4014 Mon Sep 17 00:00:00 2001 From: Jack Nash Date: Sun, 28 Jul 2024 11:15:43 +1000 Subject: [PATCH] feat: impl game queue as enum instead of hard-coded --- src/components/forms/input/select_menu.rs | 4 +-- src/routes/api/dashboard/player_data.rs | 10 ++----- src/routes/api/dashboard/setup_data.rs | 33 +++++++++++++++++------ 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/components/forms/input/select_menu.rs b/src/components/forms/input/select_menu.rs index c818368..77e9349 100644 --- a/src/components/forms/input/select_menu.rs +++ b/src/components/forms/input/select_menu.rs @@ -77,8 +77,8 @@ impl SelectMenuBuilder { self } - pub fn options(mut self, options: Vec<&str>) -> Self { - self.options = options.iter().map(|s| s.to_string()).collect(); + pub fn options(mut self, options: Vec) -> Self { + self.options = options; self } diff --git a/src/routes/api/dashboard/player_data.rs b/src/routes/api/dashboard/player_data.rs index 46c21ce..f6661b1 100644 --- a/src/routes/api/dashboard/player_data.rs +++ b/src/routes/api/dashboard/player_data.rs @@ -4,6 +4,7 @@ use crate::{ forms::input::select_menu::SelectMenuBuilder, }, models::players::get_players, + routes::api::dashboard::setup_data::GameQueue, AppState, }; use actix_web::{ @@ -71,14 +72,7 @@ async fn player_data(state: Data, raw_thread_id: web::Path) -> let game_queue = SelectMenuBuilder::new() .name("game_queue") .placeholder("Select the game queue") - .options(vec![ - "Open", - "Newbie", - "Normal", - "Mini/Micro Theme", - "Large Theme", - "Other/Unknown", - ]) + .options(GameQueue::to_vec()) .is_required(true) .default_value("Other/Unknown") .build_html(); diff --git a/src/routes/api/dashboard/setup_data.rs b/src/routes/api/dashboard/setup_data.rs index 7a52db9..575869c 100644 --- a/src/routes/api/dashboard/setup_data.rs +++ b/src/routes/api/dashboard/setup_data.rs @@ -15,6 +15,30 @@ use actix_web::{ HttpResponse, Responder, }; use maud::html; +use strum::IntoEnumIterator; +use strum_macros::{Display, EnumIter}; + +#[derive(EnumIter, Display, Debug)] +pub enum GameQueue { + #[strum(serialize = "Open")] + Open, + #[strum(serialize = "Newbie")] + Newbie, + #[strum(serialize = "Normal")] + Normal, + #[strum(serialize = "Mini/Micro Theme")] + MiniOrMicroTheme, + #[strum(serialize = "Large Theme")] + LargeTheme, + #[strum(serialize = "Other/Unknown")] + OtherOrUnknown, +} + +impl GameQueue { + pub fn to_vec() -> Vec { + GameQueue::iter().map(|q| q.to_string()).collect() + } +} #[derive(serde::Deserialize, Debug)] struct FormData { @@ -53,14 +77,7 @@ async fn setup_data(state: Data, raw_thread_id: web::Path) -> let game_queue = SelectMenuBuilder::new() .name("game_queue") .placeholder("Select the game queue") - .options(vec![ - "Open", - "Newbie", - "Normal", - "Mini/Micro Theme", - "Large Theme", - "Other/Unknown", - ]) + .options(GameQueue::to_vec()) .is_required(true) .default_value_option(game_queue) .build_html();