From 869526005f5bd34c01a79a2d2291f538f1029f42 Mon Sep 17 00:00:00 2001 From: Dylan Knutson Date: Thu, 28 Aug 2025 07:38:52 +0000 Subject: [PATCH] Complete validation function usage - fix unused validation functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ๐Ÿ”ง Fixed unused validation functions: - Updated validate_duration() to handle hours (1-720) instead of days to match handler expectations - Refactored handle_duration_input() to use validate_duration() utility - Refactored handle_start_time_input() to use validate_start_time() utility โœ… All validation functions now utilized: - validate_title() โœ“ (used in handle_title_input) - validate_price() โœ“ (used in handle_price_input) - validate_slots() โœ“ (used in handle_slots_input) - validate_duration() โœ“ (used in handle_duration_input) - NOW USED - validate_start_time() โœ“ (used in handle_start_time_input) - NOW USED ๐ŸŽฏ Benefits: - Eliminated all unused validation function warnings - Consistent validation patterns across ALL input handlers - Centralized error messages for easier maintenance - Improved code consistency and maintainability ๐Ÿ“Š Final refactoring status: 100% complete - All input handlers now use validation utilities - All validation functions are properly utilized - No more code duplication in validation logic --- src/commands/new_listing.rs | 97 +++++++++++++------------------------ 1 file changed, 33 insertions(+), 64 deletions(-) diff --git a/src/commands/new_listing.rs b/src/commands/new_listing.rs index aa42322..014b99e 100644 --- a/src/commands/new_listing.rs +++ b/src/commands/new_listing.rs @@ -12,7 +12,6 @@ use chrono::{Duration, Utc}; use log::info; use serde::{Deserialize, Serialize}; use sqlx::SqlitePool; -use std::str::FromStr; use teloxide::{ dispatching::{dialogue::serializer::Json, DpHandlerDescription}, prelude::*, @@ -141,11 +140,11 @@ fn validate_slots(text: &str) -> Result { fn validate_duration(text: &str) -> Result { match text.parse::() { - Ok(days) if days >= 1 && days <= 14 => Ok(days), + Ok(hours) if hours >= 1 && hours <= 720 => Ok(hours), // 1 hour to 30 days Ok(_) => Err( - "โŒ Duration must be between 1 and 14 days. Please enter a valid number:".to_string(), + "โŒ Duration must be between 1 and 720 hours. Please enter a valid number:".to_string(), ), - Err(_) => Err("โŒ Invalid number. Please enter number of days (1-14):".to_string()), + Err(_) => Err("โŒ Invalid number. Please enter number of hours (1-720):".to_string()), } } @@ -258,7 +257,10 @@ fn create_slots_keyboard() -> InlineKeyboardMarkup { // Create confirmation keyboard (Create/Discard/Edit) fn create_confirmation_keyboard() -> InlineKeyboardMarkup { create_multi_row_keyboard(&[ - &[("โœ… Create", "confirm_create"), ("๐Ÿ—‘๏ธ Discard", "confirm_discard")], + &[ + ("โœ… Create", "confirm_create"), + ("๐Ÿ—‘๏ธ Discard", "confirm_discard"), + ], &[("โœ๏ธ Edit", "confirm_edit")], ]) } @@ -266,9 +268,15 @@ fn create_confirmation_keyboard() -> InlineKeyboardMarkup { // Create field selection keyboard for editing fn create_field_selection_keyboard() -> InlineKeyboardMarkup { create_multi_row_keyboard(&[ - &[("๐Ÿ“ Title", "edit_title"), ("๐Ÿ“„ Description", "edit_description")], + &[ + ("๐Ÿ“ Title", "edit_title"), + ("๐Ÿ“„ Description", "edit_description"), + ], &[("๐Ÿ’ฐ Price", "edit_price"), ("๐Ÿ”ข Slots", "edit_slots")], - &[("โฐ Start Time", "edit_start_time"), ("โฑ๏ธ Duration", "edit_duration")], + &[ + ("โฐ Start Time", "edit_start_time"), + ("โฑ๏ธ Duration", "edit_duration"), + ], &[("โœ… Done", "edit_done")], ]) } @@ -287,7 +295,10 @@ fn create_back_button_keyboard() -> InlineKeyboardMarkup { fn create_back_button_keyboard_with_clear(field: &str) -> InlineKeyboardMarkup { create_single_row_keyboard(&[ ("๐Ÿ”™ Back", "edit_back"), - (&format!("๐Ÿงน Clear {}", field), &format!("edit_clear_{}", field)), + ( + &format!("๐Ÿงน Clear {}", field), + &format!("edit_clear_{}", field), + ), ]) } @@ -749,8 +760,6 @@ pub async fn handle_viewing_draft_callback( Ok(()) } - - // Helper function to process start time input, update dialogue state, and send response async fn process_start_time_and_respond( bot: &Bot, @@ -788,8 +797,6 @@ async fn process_start_time_and_respond( Ok(()) } - - pub async fn handle_price_input( bot: Bot, dialogue: NewListingDialogue, @@ -863,40 +870,20 @@ pub async fn handle_start_time_input( let chat_id = msg.chat.id; let text = msg.text().unwrap_or("").trim(); - info!( - "User {} entered start time input", - UserHandleAndId::from_chat(&msg.chat) - ); + log_user_action(&msg, "entered start time input"); if is_cancel(text) { return cancel_wizard(bot, dialogue, msg).await; } - let hours = match text.parse::() { - Ok(num) => { - if num < 0 || num > 168 { - bot.send_message( - chat_id, - "โŒ Start time must be between 0 and 168 hours. Please enter a valid number:", - ) - .reply_markup(create_start_time_keyboard()) - .await?; - return Ok(()); - } - num + match validate_start_time(text) { + Ok(hours) => { + process_start_time_and_respond(&bot, dialogue, draft, chat_id, hours).await?; } - Err(_) => { - bot.send_message( - chat_id, - "โŒ Invalid number. Please enter hours (0 for immediate, up to 168 for 7 days):", - ) - .reply_markup(create_start_time_keyboard()) - .await?; - return Ok(()); + Err(error_msg) => { + send_html_message(&bot, chat_id, &error_msg, Some(create_start_time_keyboard())).await?; } - }; - - process_start_time_and_respond(&bot, dialogue, draft, chat_id, hours).await?; + } Ok(()) } @@ -910,38 +897,20 @@ pub async fn handle_duration_input( let chat_id = msg.chat.id; let text = msg.text().unwrap_or("").trim(); - info!( - "User {} entered duration input", - UserHandleAndId::from_chat(&msg.chat) - ); + log_user_action(&msg, "entered duration input"); if is_cancel(text) { return cancel_wizard(bot, dialogue, msg).await; } - let duration = match text.parse::() { - Ok(num) => { - if num < 1 || num > 720 { - bot.send_message( - chat_id, - "โŒ Duration must be between 1 and 720 hours. Please enter a valid number:", - ) - .await?; - return Ok(()); - } - num + match validate_duration(text) { + Ok(duration) => { + process_duration_and_respond(bot, dialogue, draft, chat_id, duration).await?; } - Err(_) => { - bot.send_message( - chat_id, - "โŒ Invalid number. Please enter duration in hours (1-720):", - ) - .await?; - return Ok(()); + Err(error_msg) => { + send_html_message(&bot, chat_id, &error_msg, None).await?; } - }; - - process_duration_and_respond(bot, dialogue, draft, chat_id, duration).await?; + } Ok(()) }