anyhow used

This commit is contained in:
filipriec
2025-04-18 19:04:05 +02:00
parent 09ccad2bd4
commit 5a029283a1
12 changed files with 69 additions and 74 deletions

View File

@@ -9,6 +9,7 @@ use crate::state::pages::canvas_state::CanvasState;
use crate::ui::handlers::context::DialogPurpose;
use crate::modes::handlers::event::EventOutcome;
use common::proto::multieko2::auth::LoginResponse;
use anyhow::{Context, Result};
#[derive(Debug)]
pub enum LoginResult {
@@ -19,13 +20,12 @@ pub enum LoginResult {
/// Attempts to log the user in using the provided credentials via gRPC.
/// Updates AuthState and AppState on success or failure.
/// (This is your existing function - remains unchanged)
pub async fn save(
auth_state: &mut AuthState,
login_state: &mut LoginState,
auth_client: &mut AuthClient,
app_state: &mut AppState,
) -> Result<String, Box<dyn std::error::Error>> {
) -> Result<String> {
let identifier = login_state.username.clone();
let password = login_state.password.clone();
@@ -40,7 +40,7 @@ pub async fn save(
DialogPurpose::LoginFailed,
);
login_state.error_message = Some(error_message.clone());
return Ok(error_message);
return Err(anyhow::anyhow!(error_message));
}
// Clear previous error/dialog state before attempting
@@ -48,7 +48,9 @@ pub async fn save(
app_state.hide_dialog(); // Hide any previous dialog
// Call the gRPC login method
match auth_client.login(identifier, password).await {
match auth_client.login(identifier.clone(), password).await
.with_context(|| format!("gRPC login attempt failed for identifier: {}", identifier))
{
Ok(response) => {
// Store authentication details using correct field names
auth_state.auth_token = Some(response.access_token.clone());
@@ -92,28 +94,11 @@ pub async fn save(
login_state.set_has_unsaved_changes(true);
login_state.username.clear();
login_state.password.clear();
Ok(format!("Login failed: {}", error_message))
Err(e)
}
}
}
// --- Add this new function ---
/// Sets the stage for login: shows loading dialog and sets the pending flag.
/// Call this from the event handler when login is triggered.
pub async fn initiate_login(
app_state: &mut AppState,
login_state: &mut LoginState,
) -> Result<EventOutcome, Box<dyn std::error::Error>> {
// Show the loading dialog immediately
app_state.show_loading_dialog("Logging In", "Please wait...");
// Set the flag in LoginState to indicate the actual save should run next loop
login_state.login_request_pending = true;
// Return immediately to allow redraw
Ok(EventOutcome::Ok("Login initiated.".to_string()))
}
// --- End of new function ---
/// Reverts the login form fields to empty and returns to the previous screen (Intro).
pub async fn revert(
login_state: &mut LoginState,

View File

@@ -7,13 +7,14 @@ use crossterm::{
};
use ratatui::{backend::CrosstermBackend, Terminal};
use std::io::{self, stdout, Write};
use anyhow::{Context, Result};
pub struct TerminalCore {
terminal: Terminal<CrosstermBackend<io::Stdout>>,
}
impl TerminalCore {
pub fn new() -> Result<Self, Box<dyn std::error::Error>> {
pub fn new() -> Result<Self> {
enable_raw_mode()?;
let mut stdout = stdout();
execute!(
@@ -27,7 +28,7 @@ impl TerminalCore {
Ok(Self { terminal })
}
pub fn draw<F>(&mut self, f: F) -> Result<(), Box<dyn std::error::Error>>
pub fn draw<F>(&mut self, f: F) -> Result<()>
where
F: FnOnce(&mut ratatui::Frame),
{
@@ -35,7 +36,7 @@ impl TerminalCore {
Ok(())
}
pub fn cleanup(&mut self) -> Result<(), Box<dyn std::error::Error>> {
pub fn cleanup(&mut self) -> Result<()> {
let backend = self.terminal.backend_mut();
execute!(
backend,
@@ -56,7 +57,7 @@ impl TerminalCore {
pub fn set_cursor_style(
&mut self,
style: SetCursorStyle,
) -> Result<(), Box<dyn std::error::Error>> {
) -> Result<()> {
execute!(
self.terminal.backend_mut(),
style,
@@ -65,7 +66,7 @@ impl TerminalCore {
Ok(())
}
pub fn show_cursor(&mut self) -> Result<(), Box<dyn std::error::Error>> {
pub fn show_cursor(&mut self) -> Result<()> {
execute!(
self.terminal.backend_mut(),
Show
@@ -73,7 +74,7 @@ impl TerminalCore {
Ok(())
}
pub fn hide_cursor(&mut self) -> Result<(), Box<dyn std::error::Error>> {
pub fn hide_cursor(&mut self) -> Result<()> {
execute!(
self.terminal.backend_mut(),
Hide

View File

@@ -1,6 +1,7 @@
// src/tui/terminal/event_reader.rs
use crossterm::event::{self, Event};
use anyhow::{Context, Result};
pub struct EventReader;
@@ -9,7 +10,7 @@ impl EventReader {
Self
}
pub fn read_event(&self) -> Result<Event, Box<dyn std::error::Error>> {
pub fn read_event(&self) -> Result<Event> {
Ok(event::read()?)
}
}