Complete validation function usage - fix unused validation functions

🔧 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
This commit is contained in:
Dylan Knutson
2025-08-28 07:38:52 +00:00
parent 79ed3a7e4c
commit 869526005f

View File

@@ -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<i32, String> {
fn validate_duration(text: &str) -> Result<i32, String> {
match text.parse::<i32>() {
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::<i32>() {
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
}
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(());
}
};
match validate_start_time(text) {
Ok(hours) => {
process_start_time_and_respond(&bot, dialogue, draft, chat_id, hours).await?;
}
Err(error_msg) => {
send_html_message(&bot, chat_id, &error_msg, Some(create_start_time_keyboard())).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::<i32>() {
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
}
Err(_) => {
bot.send_message(
chat_id,
"❌ Invalid number. Please enter duration in hours (1-720):",
)
.await?;
return Ok(());
}
};
match validate_duration(text) {
Ok(duration) => {
process_duration_and_respond(bot, dialogue, draft, chat_id, duration).await?;
}
Err(error_msg) => {
send_html_message(&bot, chat_id, &error_msg, None).await?;
}
}
Ok(())
}