diff --git a/src/app.rs b/src/app.rs index a5faef2..0f1b98d 100644 --- a/src/app.rs +++ b/src/app.rs @@ -3,7 +3,7 @@ use crate::{ ui::popup::{info_popup::InfoPopUp, PopUp}, }; use ansi_to_tui::IntoText; -use color_eyre::eyre::bail; +use color_eyre::eyre::{bail}; use config::Config; use cover_renderer::render_cover; use logging::Logger; @@ -23,6 +23,8 @@ use screens::{ CurrentScreen, }; use std::collections::{HashMap, HashSet}; +use std::ffi::OsStr; +use std::path::Path; use crate::utils; @@ -32,6 +34,11 @@ pub mod logging; pub mod patch_renderer; pub mod screens; +pub enum PatchFound { + Found, + NotFound +} + /// Type that represents the overall state of the application. It can be viewed /// as the **Model** component of `patch-hub`. pub struct App { @@ -132,7 +139,7 @@ impl App { /// Initializes field [App::details_actions], from currently selected /// patchset in [App::bookmarked_patchsets] or [App::latest_patchsets], /// depending on the value of [App::current_screen]. - pub fn init_details_actions(&mut self) -> color_eyre::Result<()> { + pub fn init_details_actions(&mut self) -> color_eyre::Result { let representative_patch: Patch; let mut is_patchset_bookmarked = true; let mut reviewed_by = Vec::new(); @@ -164,7 +171,15 @@ impl App { self.config.patchsets_cache_dir(), &representative_patch, )) { - Ok(result) => result, + Ok(result) => { + let path = Path::new(OsStr::new(&result)); + + if ! path.exists() { + return Ok(PatchFound::NotFound); + } + + result + } Err(io_error) => bail!("{io_error}"), }; @@ -250,7 +265,7 @@ impl App { lore_api_client: self.lore_api_client.clone(), patchset_path, }); - Ok(()) + Ok(PatchFound::Found) } Err(message) => bail!(message), } diff --git a/src/handler/latest.rs b/src/handler/latest.rs index 6a3b1e6..b4c5a4e 100644 --- a/src/handler/latest.rs +++ b/src/handler/latest.rs @@ -1,10 +1,11 @@ use std::ops::ControlFlow; use crate::{ - app::{screens::CurrentScreen, App}, + app::{screens::CurrentScreen, App, PatchFound}, loading_screen, - ui::popup::{help::HelpPopUpBuilder, PopUp}, + ui::popup::{help::HelpPopUpBuilder, info_popup::InfoPopUp, PopUp}, }; +use color_eyre::eyre::Ok; use ratatui::{ crossterm::event::{KeyCode, KeyEvent}, prelude::Backend, @@ -55,10 +56,19 @@ where "Loading patchset" => { let result = app.init_details_actions(); if result.is_ok() { - app.set_current_screen(CurrentScreen::PatchsetDetails); + match result.unwrap() { + PatchFound::Found => { + app.set_current_screen(CurrentScreen::PatchsetDetails); + } + + PatchFound::NotFound => { + app.popup = Some(InfoPopUp::generate_info_popup("Error","The selected patchset couldn't be retrieved.\nPlease choose another patchset.")); + app.set_current_screen(CurrentScreen::LatestPatchsets); + } + } } - result - } + Ok(()) + } }; } _ => {}