diff --git a/client/src/functions/modes/edit/add_table_e.rs b/client/src/functions/modes/edit/add_table_e.rs index 50838dd..66e26a7 100644 --- a/client/src/functions/modes/edit/add_table_e.rs +++ b/client/src/functions/modes/edit/add_table_e.rs @@ -2,7 +2,7 @@ use crate::state::pages::add_table::AddTableState; use crate::state::pages::canvas_state::CanvasState; // Use trait use crossterm::event::{KeyCode, KeyEvent}; -use std::error::Error; +use anyhow::{Context, Result}; #[derive(PartialEq)] enum CharType { @@ -134,7 +134,7 @@ pub async fn execute_edit_action( state: &mut AddTableState, ideal_cursor_column: &mut usize, // Add other params like grpc_client if needed for future actions (e.g., validation) -) -> Result> { +) -> Result { // Use the CanvasState trait methods implemented for AddTableState match action { "insert_char" => { diff --git a/client/src/functions/modes/edit/auth_e.rs b/client/src/functions/modes/edit/auth_e.rs index 3a5fd49..8bb0fbd 100644 --- a/client/src/functions/modes/edit/auth_e.rs +++ b/client/src/functions/modes/edit/auth_e.rs @@ -7,6 +7,7 @@ use crate::state::pages::auth::RegisterState; use crate::tui::functions::common::form::{revert, save}; use crossterm::event::{KeyCode, KeyEvent}; use std::any::Any; +use anyhow::{Context, Result}; pub async fn execute_common_action( action: &str, @@ -14,7 +15,7 @@ pub async fn execute_common_action( grpc_client: &mut GrpcClient, current_position: &mut u64, total_count: u64, -) -> Result> { +) -> Result { match action { "save" | "revert" => { if !state.has_unsaved_changes() { @@ -62,7 +63,7 @@ pub async fn execute_edit_action( key: KeyEvent, state: &mut S, ideal_cursor_column: &mut usize, -) -> Result> { +) -> Result { match action { "insert_char" => { if let KeyCode::Char(c) = key.code { diff --git a/client/src/functions/modes/edit/form_e.rs b/client/src/functions/modes/edit/form_e.rs index 8c5bb2c..fd62430 100644 --- a/client/src/functions/modes/edit/form_e.rs +++ b/client/src/functions/modes/edit/form_e.rs @@ -8,6 +8,7 @@ use crate::tui::functions::common::form::SaveOutcome; use crate::modes::handlers::event::EventOutcome; use crossterm::event::{KeyCode, KeyEvent}; use std::any::Any; +use anyhow::{Context, Result}; pub async fn execute_common_action( action: &str, @@ -15,7 +16,7 @@ pub async fn execute_common_action( grpc_client: &mut GrpcClient, current_position: &mut u64, total_count: u64, -) -> Result> { +) -> Result { match action { "save" | "revert" => { if !state.has_unsaved_changes() { @@ -76,7 +77,7 @@ pub async fn execute_edit_action( key: KeyEvent, state: &mut S, ideal_cursor_column: &mut usize, -) -> Result> { +) -> Result { match action { "insert_char" => { if let KeyCode::Char(c) = key.code { diff --git a/client/src/functions/modes/read_only/add_table_ro.rs b/client/src/functions/modes/read_only/add_table_ro.rs index 2b80e6f..98f29bb 100644 --- a/client/src/functions/modes/read_only/add_table_ro.rs +++ b/client/src/functions/modes/read_only/add_table_ro.rs @@ -3,7 +3,7 @@ use crate::config::binds::key_sequences::KeySequenceTracker; use crate::state::pages::add_table::AddTableState; use crate::state::pages::canvas_state::CanvasState; // Use trait for common actions use crate::state::app::state::AppState; -use std::error::Error; +use anyhow::Result; // Re-use word navigation helpers if they are public or move them to a common module // For now, duplicating them here for simplicity. Consider refactoring later. @@ -74,7 +74,7 @@ pub async fn execute_action( ideal_cursor_column: &mut usize, key_sequence_tracker: &mut KeySequenceTracker, command_message: &mut String, // Keep for potential messages -) -> Result> { +) -> Result { // Use the CanvasState trait methods implemented for AddTableState match action { "move_up" => { diff --git a/client/src/functions/modes/read_only/auth_ro.rs b/client/src/functions/modes/read_only/auth_ro.rs index 25c55e1..a146be8 100644 --- a/client/src/functions/modes/read_only/auth_ro.rs +++ b/client/src/functions/modes/read_only/auth_ro.rs @@ -3,7 +3,7 @@ use crate::config::binds::key_sequences::KeySequenceTracker; use crate::state::pages::canvas_state::CanvasState; use crate::state::app::state::AppState; -use std::error::Error; +use anyhow::{Context, Result}; #[derive(PartialEq)] enum CharType { @@ -19,7 +19,7 @@ pub async fn execute_action( ideal_cursor_column: &mut usize, key_sequence_tracker: &mut KeySequenceTracker, command_message: &mut String, -) -> Result> { +) -> Result { match action { "previous_entry" | "next_entry" => { key_sequence_tracker.reset(); diff --git a/client/src/functions/modes/read_only/form_ro.rs b/client/src/functions/modes/read_only/form_ro.rs index 470fec2..da3ecda 100644 --- a/client/src/functions/modes/read_only/form_ro.rs +++ b/client/src/functions/modes/read_only/form_ro.rs @@ -2,7 +2,7 @@ use crate::config::binds::key_sequences::KeySequenceTracker; use crate::state::pages::canvas_state::CanvasState; -use std::error::Error; +use anyhow::Result; #[derive(PartialEq)] enum CharType { @@ -17,7 +17,7 @@ pub async fn execute_action( ideal_cursor_column: &mut usize, key_sequence_tracker: &mut KeySequenceTracker, command_message: &mut String, -) -> Result> { +) -> Result { match action { "previous_entry" | "next_entry" => { key_sequence_tracker.reset(); diff --git a/client/src/modes/canvas/common_mode.rs b/client/src/modes/canvas/common_mode.rs index 1cf57f7..834815c 100644 --- a/client/src/modes/canvas/common_mode.rs +++ b/client/src/modes/canvas/common_mode.rs @@ -7,6 +7,7 @@ use crate::services::grpc_client::GrpcClient; use crate::services::auth::AuthClient; use crate::modes::handlers::event::EventOutcome; use crate::tui::functions::common::form::SaveOutcome; +use anyhow::{Context, Result}; use crate::tui::functions::common::{ form::{save as form_save, revert as form_revert}, login::{save as login_save, revert as login_revert}, @@ -25,14 +26,14 @@ pub async fn handle_core_action( app_state: &mut AppState, current_position: &mut u64, total_count: u64, -) -> Result> { +) -> Result { match action { "save" => { if app_state.ui.show_login { - let message = login_save(auth_state, login_state, auth_client, app_state).await?; + let message = login_save(auth_state, login_state, auth_client, app_state).await.context("Login save action failed")?; Ok(EventOutcome::Ok(message)) } else if app_state.ui.show_register { - let message = register_save(register_state, auth_client, app_state).await?; + let message = register_save(register_state, auth_client, app_state).await.context("Register save_and_quit action failed")?; Ok(EventOutcome::Ok(message)) } else { let save_outcome = form_save( @@ -40,7 +41,7 @@ pub async fn handle_core_action( grpc_client, current_position, total_count, - ).await?; + ).await.context("Register save action failed")?; let message = match save_outcome { SaveOutcome::NoChange => "No changes to save.".to_string(), SaveOutcome::UpdatedExisting => "Entry updated.".to_string(), @@ -55,9 +56,9 @@ pub async fn handle_core_action( }, "save_and_quit" => { let message = if app_state.ui.show_login { - login_save(auth_state, login_state, auth_client, app_state).await? + login_save(auth_state, login_state, auth_client, app_state).await.context("Login save n quit action failed")? } else if app_state.ui.show_register { - register_save(register_state, auth_client, app_state).await? + register_save(register_state, auth_client, app_state).await.context("Register save n quit action failed")? } else { let save_outcome = form_save( form_state, @@ -87,7 +88,7 @@ pub async fn handle_core_action( grpc_client, current_position, total_count, - ).await?; + ).await.context("Form revert x action failed")?; Ok(EventOutcome::Ok(message)) } }, diff --git a/client/src/modes/canvas/edit.rs b/client/src/modes/canvas/edit.rs index 1cb1476..ef1eae3 100644 --- a/client/src/modes/canvas/edit.rs +++ b/client/src/modes/canvas/edit.rs @@ -11,6 +11,7 @@ use crate::modes::handlers::event::EventOutcome; use crate::functions::modes::edit::{auth_e, form_e}; use crate::functions::modes::edit::add_table_e; use crate::state::app::state::AppState; +use anyhow::Result; use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; #[derive(Debug, Clone, PartialEq, Eq)] @@ -31,7 +32,7 @@ pub async fn handle_edit_event( total_count: u64, grpc_client: &mut GrpcClient, app_state: &AppState, -) -> Result> { +) -> Result { // Global command mode check (should ideally be handled before calling this function) if let Some("enter_command_mode") = config.get_action_for_key_in_mode( &config.keybindings.global, diff --git a/client/src/modes/canvas/read_only.rs b/client/src/modes/canvas/read_only.rs index 41f1883..8dd2b73 100644 --- a/client/src/modes/canvas/read_only.rs +++ b/client/src/modes/canvas/read_only.rs @@ -10,6 +10,7 @@ use crate::state::pages::add_table::AddTableState; use crate::state::app::state::AppState; use crate::functions::modes::read_only::{auth_ro, form_ro, add_table_ro}; use crossterm::event::KeyEvent; +use anyhow::{Context, Result}; pub async fn handle_read_only_event( app_state: &mut AppState, @@ -26,7 +27,7 @@ pub async fn handle_read_only_event( command_message: &mut String, edit_mode_cooldown: &mut bool, ideal_cursor_column: &mut usize, -) -> Result<(bool, String), Box> { +) -> Result<(bool, String)> { if config.is_enter_edit_mode_before(key.code, key.modifiers) { *edit_mode_cooldown = true; *command_message = "Entering Edit mode".to_string(); diff --git a/client/src/modes/common/command_mode.rs b/client/src/modes/common/command_mode.rs index 926fcac..6645554 100644 --- a/client/src/modes/common/command_mode.rs +++ b/client/src/modes/common/command_mode.rs @@ -10,7 +10,7 @@ use crate::tui::terminal::core::TerminalCore; use crate::tui::functions::common::form::{save, revert}; use crate::modes::handlers::event::EventOutcome; use crate::tui::functions::common::form::SaveOutcome; -use std::error::Error; +use anyhow::{Context, Result}; pub async fn handle_command_event( key: KeyEvent, @@ -26,7 +26,7 @@ pub async fn handle_command_event( terminal: &mut TerminalCore, current_position: &mut u64, total_count: u64, -) -> Result> { +) -> Result { // Exit command mode (via configurable keybinding) if config.is_exit_command_mode(key.code, key.modifiers) { command_input.clear(); @@ -84,7 +84,7 @@ async fn process_command( terminal: &mut TerminalCore, current_position: &mut u64, total_count: u64, -) -> Result> { +) -> Result { // Clone the trimmed command to avoid borrow issues let command = command_input.trim().to_string(); if command.is_empty() { diff --git a/client/src/modes/common/commands.rs b/client/src/modes/common/commands.rs index 17ffe23..2504c18 100644 --- a/client/src/modes/common/commands.rs +++ b/client/src/modes/common/commands.rs @@ -3,6 +3,7 @@ use crate::tui::terminal::core::TerminalCore; use crate::state::app::state::AppState; use crate::state::pages::{form::FormState, auth::LoginState, auth::RegisterState}; use crate::state::pages::canvas_state::CanvasState; +use anyhow::{Context, Result}; pub struct CommandHandler; @@ -19,7 +20,7 @@ impl CommandHandler { form_state: &FormState, login_state: &LoginState, register_state: &RegisterState, - ) -> Result<(bool, String), Box> { + ) -> Result<(bool, String)> { match action { "quit" => self.handle_quit(terminal, app_state, form_state, login_state, register_state).await, "force_quit" => self.handle_force_quit(terminal).await, @@ -35,7 +36,7 @@ impl CommandHandler { form_state: &FormState, login_state: &LoginState, register_state: &RegisterState, - ) -> Result<(bool, String), Box> { + ) -> Result<(bool, String)> { // Use actual unsaved changes state instead of is_saved flag let has_unsaved = if app_state.ui.show_login { login_state.has_unsaved_changes() @@ -56,7 +57,7 @@ impl CommandHandler { async fn handle_force_quit( &self, terminal: &mut TerminalCore, - ) -> Result<(bool, String), Box> { + ) -> Result<(bool, String)> { terminal.cleanup()?; Ok((true, "Force exiting without saving.".into())) } @@ -64,7 +65,7 @@ impl CommandHandler { async fn handle_save_quit( &mut self, terminal: &mut TerminalCore, - ) -> Result<(bool, String), Box> { + ) -> Result<(bool, String)> { terminal.cleanup()?; Ok((true, "State saved. Exiting.".into())) } diff --git a/client/src/modes/general/dialog.rs b/client/src/modes/general/dialog.rs index 5e20485..31a0063 100644 --- a/client/src/modes/general/dialog.rs +++ b/client/src/modes/general/dialog.rs @@ -10,6 +10,7 @@ use crate::state::pages::admin::AdminState; use crate::modes::handlers::event::EventOutcome; use crate::tui::functions::common::{login, register}; use crate::tui::functions::common::add_table::handle_delete_selected_columns; +use anyhow::{Context, Result}; /// Handles key events specifically when a dialog is active. /// Returns Some(Result) if the event was handled (consumed), @@ -22,7 +23,7 @@ pub async fn handle_dialog_event( register_state: &mut RegisterState, buffer_state: &mut BufferState, admin_state: &mut AdminState, -) -> Option>> { +) -> Option> { if let Event::Key(key) = event { // Always allow Esc to dismiss if key.code == KeyCode::Esc { diff --git a/client/src/modes/general/navigation.rs b/client/src/modes/general/navigation.rs index a04979a..cf9a67b 100644 --- a/client/src/modes/general/navigation.rs +++ b/client/src/modes/general/navigation.rs @@ -11,6 +11,7 @@ use crate::state::pages::admin::AdminState; use crate::state::pages::canvas_state::CanvasState; use crate::ui::handlers::context::UiContext; use crate::modes::handlers::event::EventOutcome; +use anyhow::{Context, Result}; pub async fn handle_navigation_event( key: KeyEvent, @@ -24,7 +25,7 @@ pub async fn handle_navigation_event( command_mode: &mut bool, command_input: &mut String, command_message: &mut String, -) -> Result> { +) -> Result { if let Some(action) = config.get_general_action(key.code, key.modifiers) { match action { "move_up" => { diff --git a/client/src/modes/handlers/event.rs b/client/src/modes/handlers/event.rs index 505f6fd..66241f3 100644 --- a/client/src/modes/handlers/event.rs +++ b/client/src/modes/handlers/event.rs @@ -280,7 +280,7 @@ impl EventHandler { } UiContext::Register => { match index { - 0 => register::save(register_state, &mut self.auth_client, app_state).await?, + 0 => register::save(register_state, &mut self.auth_client, app_state).await.context("Register save action failed")?, 1 => register::back_to_login(register_state, app_state, buffer_state).await, _ => "Invalid Login Option".to_string(), } @@ -534,7 +534,7 @@ impl EventHandler { } Err(e) => { // Handle error from the edit handler - return Err(e); + return Err(e.into()); } } }, // End AppMode::Edit diff --git a/client/src/services/ui_service.rs b/client/src/services/ui_service.rs index b6680a5..8e7c95b 100644 --- a/client/src/services/ui_service.rs +++ b/client/src/services/ui_service.rs @@ -12,7 +12,7 @@ impl UiService { pub async fn initialize_app_state( grpc_client: &mut GrpcClient, app_state: &mut AppState, - ) -> Result, Box> { + ) -> Result> { // Fetch profile tree let profile_tree = grpc_client.get_profile_tree().await.context("Failed to get profile tree")?; app_state.profile_tree = profile_tree; @@ -34,7 +34,7 @@ impl UiService { grpc_client: &mut GrpcClient, app_state: &mut AppState, ) -> Result<()> { - let total_count = grpc_client.get_adresar_count().await.await.context("Failed to get adresar count")?; + let total_count = grpc_client.get_adresar_count().await.context("Failed to get adresar count")?; app_state.update_total_count(total_count); app_state.update_current_position(total_count.saturating_add(1)); // Start in new entry mode Ok(()) diff --git a/client/src/tui/functions/common/form.rs b/client/src/tui/functions/common/form.rs index 9260d66..44440f2 100644 --- a/client/src/tui/functions/common/form.rs +++ b/client/src/tui/functions/common/form.rs @@ -3,6 +3,7 @@ use crate::services::grpc_client::GrpcClient; use crate::state::pages::form::FormState; use common::proto::multieko2::adresar::{PostAdresarRequest, PutAdresarRequest}; +use anyhow::{Context, Result}; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum SaveOutcome { @@ -17,7 +18,7 @@ pub async fn save( grpc_client: &mut GrpcClient, current_position: &mut u64, total_count: u64, -) -> Result> { // <-- Return SaveOutcome +) -> Result { // <-- Return SaveOutcome if !form_state.has_unsaved_changes { return Ok(SaveOutcome::NoChange); // Early exit if no changes } @@ -78,7 +79,7 @@ pub async fn revert( grpc_client: &mut GrpcClient, current_position: &mut u64, total_count: u64, -) -> Result> { +) -> Result { let is_new = *current_position == total_count + 1; if is_new { diff --git a/client/src/tui/functions/common/register.rs b/client/src/tui/functions/common/register.rs index aa15936..2c1c3e0 100644 --- a/client/src/tui/functions/common/register.rs +++ b/client/src/tui/functions/common/register.rs @@ -8,6 +8,7 @@ use crate::state::{ }; use crate::ui::handlers::context::DialogPurpose; use crate::state::app::buffer::{AppView, BufferState}; +use anyhow::{Context, Result}; /// Attempts to register the user using the provided details via gRPC. /// Updates RegisterState and AppState on success or failure. @@ -15,7 +16,7 @@ pub async fn save( register_state: &mut RegisterState, auth_client: &mut AuthClient, app_state: &mut AppState, -) -> Result> { +) -> Result { let username = register_state.username.clone(); let email = register_state.email.clone(); // Handle optional passwords: send None if empty, Some(value) otherwise diff --git a/client/src/ui/handlers/ui.rs b/client/src/ui/handlers/ui.rs index b50b21b..0fb0faf 100644 --- a/client/src/ui/handlers/ui.rs +++ b/client/src/ui/handlers/ui.rs @@ -57,8 +57,7 @@ pub async fn run_ui() -> Result<()> { let mut form_state = FormState::new(column_names); // Fetch the total count of Adresar entries - UiService::initialize_adresar_count(&mut grpc_client, &mut app_state).await - .await?; + UiService::initialize_adresar_count(&mut grpc_client, &mut app_state).await?; form_state.reset_to_empty(); // --- FPS Calculation State ---