BREAKING CHANGES updating gRPC based on the enum now

This commit is contained in:
filipriec
2025-04-07 21:27:01 +02:00
parent bb103fac6c
commit 70678432c6
7 changed files with 316 additions and 182 deletions

View File

@@ -1,10 +1,12 @@
// src/modes/canvas/common.rs
// src/modes/canvas/common_mode.rs
use crate::tui::terminal::core::TerminalCore;
use crate::state::pages::{form::FormState, auth::AuthState};
use crate::state::state::AppState;
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 crate::tui::functions::common::{
form::{save as form_save, revert as form_revert},
login::{save as login_save, revert as login_revert}
@@ -20,46 +22,54 @@ pub async fn handle_core_action(
app_state: &mut AppState,
current_position: &mut u64,
total_count: u64,
) -> Result<(bool, String), Box<dyn std::error::Error>> {
) -> Result<EventOutcome, Box<dyn std::error::Error>> {
match action {
"save" => {
if app_state.ui.show_login {
let message = login_save(auth_state, auth_client, app_state).await?;
Ok((false, message))
Ok(EventOutcome::Ok(message))
} else {
let message = form_save(
let save_outcome = form_save(
form_state,
grpc_client,
&mut app_state.ui.is_saved,
current_position,
total_count,
).await?;
Ok((false, message))
let message = match save_outcome {
SaveOutcome::NoChange => "No changes to save.".to_string(),
SaveOutcome::UpdatedExisting => "Entry updated.".to_string(),
SaveOutcome::CreatedNew(_) => "New entry created.".to_string(),
};
Ok(EventOutcome::DataSaved(save_outcome, message))
}
},
"force_quit" => {
terminal.cleanup()?;
Ok((true, "Force exiting without saving.".to_string()))
Ok(EventOutcome::Exit("Force exiting without saving.".to_string()))
},
"save_and_quit" => {
let message = if app_state.ui.show_login {
login_save(auth_state, auth_client, app_state).await?
} else {
form_save(
let save_outcome = form_save(
form_state,
grpc_client,
&mut app_state.ui.is_saved,
current_position,
total_count,
).await?
).await?;
match save_outcome {
SaveOutcome::NoChange => "No changes to save.".to_string(),
SaveOutcome::UpdatedExisting => "Entry updated.".to_string(),
SaveOutcome::CreatedNew(_) => "New entry created.".to_string(),
}
};
terminal.cleanup()?;
Ok((true, format!("{}. Exiting application.", message)))
Ok(EventOutcome::Exit(format!("{}. Exiting application.", message)))
},
"revert" => {
if app_state.ui.show_login {
let message = login_revert(auth_state, app_state).await;
Ok((false, message))
Ok(EventOutcome::Ok(message))
} else {
let message = form_revert(
form_state,
@@ -67,9 +77,9 @@ pub async fn handle_core_action(
current_position,
total_count,
).await?;
Ok((false, message))
Ok(EventOutcome::Ok(message))
}
},
_ => Ok((false, format!("Core action not handled: {}", action))),
_ => Ok(EventOutcome::Ok(format!("Core action not handled: {}", action))),
}
}

View File

@@ -1,4 +1,4 @@
// src/modes/handlers/command_mode.rs
// src/modes/common/command_mode.rs
use crossterm::event::{KeyEvent, KeyCode, KeyModifiers};
use crate::config::binds::config::Config;
@@ -7,6 +7,14 @@ use crate::state::pages::form::FormState;
use crate::modes::common::commands::CommandHandler;
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;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CommandOutcome {
ExitCommandMode(String),
// Other command-specific outcomes if needed
}
pub async fn handle_command_event(
key: KeyEvent,
@@ -19,15 +27,12 @@ pub async fn handle_command_event(
terminal: &mut TerminalCore,
current_position: &mut u64,
total_count: u64,
) -> Result<(bool, String, bool), Box<dyn std::error::Error>> {
// Return value: (should_exit, message, should_exit_command_mode)
) -> Result<EventOutcome, Box<dyn std::error::Error>> {
// Exit command mode (via configurable keybinding)
if config.is_exit_command_mode(key.code, key.modifiers) {
command_input.clear();
*command_message = "".to_string();
return Ok((false, "".to_string(), true));
return Ok(EventOutcome::Ok("Exited command mode".to_string()));
}
// Execute command (via configurable keybinding, defaults to Enter)
@@ -48,7 +53,7 @@ pub async fn handle_command_event(
// Backspace (via configurable keybinding, defaults to Backspace)
if config.is_command_backspace(key.code, key.modifiers) {
command_input.pop();
return Ok((false, "".to_string(), false));
return Ok(EventOutcome::Ok("".to_string()));
}
// Regular character input - accept any character in command mode
@@ -56,12 +61,12 @@ pub async fn handle_command_event(
// Accept regular or shifted characters (e.g., 'a' or 'A')
if key.modifiers.is_empty() || key.modifiers == KeyModifiers::SHIFT {
command_input.push(c);
return Ok((false, "".to_string(), false));
return Ok(EventOutcome::Ok("".to_string()));
}
}
// Ignore all other keys
Ok((false, "".to_string(), false))
Ok(EventOutcome::Ok("".to_string()))
}
async fn process_command(
@@ -74,12 +79,12 @@ async fn process_command(
terminal: &mut TerminalCore,
current_position: &mut u64,
total_count: u64,
) -> Result<(bool, String, bool), Box<dyn std::error::Error>> {
) -> Result<EventOutcome, Box<dyn std::error::Error>> {
// Clone the trimmed command to avoid borrow issues
let command = command_input.trim().to_string();
if command.is_empty() {
*command_message = "Empty command".to_string();
return Ok((false, command_message.clone(), false));
return Ok(EventOutcome::Ok(command_message.clone()));
}
// Get the action for the command (now checks global and common bindings too)
@@ -92,18 +97,26 @@ async fn process_command(
.handle_command(action, terminal)
.await?;
command_input.clear();
Ok((should_exit, message, true))
if should_exit {
Ok(EventOutcome::Exit(message))
} else {
Ok(EventOutcome::Ok(message))
}
},
"save" => {
let message = save(
let outcome = save(
form_state,
grpc_client,
&mut command_handler.is_saved,
current_position,
total_count,
).await?;
let message = match outcome {
SaveOutcome::CreatedNew(_) => "New entry created".to_string(),
SaveOutcome::UpdatedExisting => "Entry updated".to_string(),
SaveOutcome::NoChange => "No changes to save".to_string(),
};
command_input.clear();
return Ok((false, message, true));
Ok(EventOutcome::DataSaved(outcome, message))
},
"revert" => {
let message = revert(
@@ -113,17 +126,12 @@ async fn process_command(
total_count,
).await?;
command_input.clear();
return Ok((false, message, true));
},
"unknown" => {
let message = format!("Unknown command: {}", command);
command_input.clear();
return Ok((false, message, true));
Ok(EventOutcome::Ok(message))
},
_ => {
let message = format!("Unhandled action: {}", action);
command_input.clear();
return Ok((false, message, true));
Ok(EventOutcome::Ok(message))
}
}
}

View File

@@ -17,6 +17,15 @@ use crate::modes::{
};
use crate::config::binds::key_sequences::KeySequenceTracker;
use crate::modes::handlers::mode_manager::{ModeManager, AppMode};
use crate::tui::functions::common::form::SaveOutcome;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EventOutcome {
Ok(String), // Normal operation, display message
Exit(String), // Signal app exit, display message
DataSaved(SaveOutcome, String), // Data save attempted, include outcome and message
// Add other outcomes like QuitRequested, SaveAndQuitRequested later if needed
}
pub struct EventHandler {
pub command_mode: bool,
@@ -55,7 +64,7 @@ impl EventHandler {
app_state: &mut crate::state::state::AppState,
total_count: u64,
current_position: &mut u64,
) -> Result<(bool, String), Box<dyn std::error::Error>> {
) -> Result<EventOutcome, Box<dyn std::error::Error>> {
let current_mode = ModeManager::derive_mode(app_state, self);
app_state.update_mode(current_mode);
@@ -64,9 +73,10 @@ impl EventHandler {
let modifiers = key.modifiers;
if UiStateHandler::toggle_sidebar(&mut app_state.ui, config, key_code, modifiers) {
return Ok((false, format!("Sidebar {}",
let message = format!("Sidebar {}",
if app_state.ui.show_sidebar { "shown" } else { "hidden" }
)));
);
return Ok(EventOutcome::Ok(message));
}
match current_mode {
@@ -90,7 +100,7 @@ impl EventHandler {
self.edit_mode_cooldown = true;
self.command_message = "Edit mode".to_string();
terminal.set_cursor_style(SetCursorStyle::BlinkingBar)?;
return Ok((false, self.command_message.clone()));
return Ok(EventOutcome::Ok(self.command_message.clone()));
}
if config.is_enter_edit_mode_after(key_code, modifiers) &&
@@ -119,7 +129,7 @@ impl EventHandler {
self.edit_mode_cooldown = true;
self.command_message = "Edit mode (after cursor)".to_string();
terminal.set_cursor_style(SetCursorStyle::BlinkingBar)?;
return Ok((false, self.command_message.clone()));
return Ok(EventOutcome::Ok(self.command_message.clone()));
}
if let Some(action) = config.get_read_only_action_for_key(key_code, modifiers) {
@@ -127,7 +137,7 @@ impl EventHandler {
self.command_mode = true;
self.command_input.clear();
self.command_message.clear();
return Ok((false, String::new()));
return Ok(EventOutcome::Ok(String::new()));
}
}
@@ -154,7 +164,7 @@ impl EventHandler {
}
}
return read_only::handle_read_only_event(
let message = read_only::handle_read_only_event(
app_state,
key,
config,
@@ -167,26 +177,27 @@ impl EventHandler {
&mut self.command_message,
&mut self.edit_mode_cooldown,
&mut self.ideal_cursor_column,
).await;
).await?;
return Ok(EventOutcome::Ok(message));
},
AppMode::Edit => {
if config.is_exit_edit_mode(key_code, modifiers) {
self.is_edit_mode = false;
self.edit_mode_cooldown = true;
let has_changes = if app_state.ui.show_login {
auth_state.has_unsaved_changes()
} else {
form_state.has_unsaved_changes()
};
self.command_message = if has_changes {
"Exited edit mode (unsaved changes remain)".to_string()
} else {
"Read-only mode".to_string()
};
terminal.set_cursor_style(SetCursorStyle::SteadyBlock)?;
let current_input = if app_state.ui.show_login {
@@ -210,7 +221,7 @@ impl EventHandler {
self.ideal_cursor_column = form_state.current_cursor_pos();
}
}
return Ok((false, self.command_message.clone()));
return Ok(EventOutcome::Ok(self.command_message.clone()));
}
if let Some(action) = config.get_action_for_key_in_mode(
@@ -220,23 +231,23 @@ impl EventHandler {
) {
match action {
"save" | "force_quit" | "save_and_quit" | "revert" => {
return common_mode::handle_core_action(
action,
form_state,
auth_state,
grpc_client,
&mut self.auth_client,
terminal,
app_state,
current_position,
total_count,
).await;
},
return common_mode::handle_core_action(
action,
form_state,
auth_state,
grpc_client,
&mut self.auth_client,
terminal,
app_state,
current_position,
total_count,
).await;
},
_ => {}
}
}
let result = edit::handle_edit_event(
let message = edit::handle_edit_event(
app_state.ui.show_login,
key,
config,
@@ -244,18 +255,17 @@ impl EventHandler {
auth_state,
&mut self.ideal_cursor_column,
&mut self.command_message,
&mut app_state.ui.is_saved,
current_position,
total_count,
grpc_client,
).await?;
self.key_sequence_tracker.reset();
return Ok((false, result));
return Ok(EventOutcome::Ok(message));
},
AppMode::Command => {
let (should_exit, message, exit_command_mode) = command_mode::handle_command_event(
let outcome = command_mode::handle_command_event(
key,
config,
form_state,
@@ -267,17 +277,18 @@ impl EventHandler {
current_position,
total_count,
).await?;
if exit_command_mode {
self.command_mode = false;
if let EventOutcome::Ok(msg) = &outcome {
if msg == "Exited command mode" {
self.command_mode = false;
}
}
return Ok((should_exit, message));
return Ok(outcome);
}
}
}
self.edit_mode_cooldown = false;
Ok((false, self.command_message.clone()))
Ok(EventOutcome::Ok(self.command_message.clone()))
}
}