diff --git a/src/app.rs b/src/app.rs index a5faef2..554c507 100644 --- a/src/app.rs +++ b/src/app.rs @@ -9,7 +9,7 @@ use cover_renderer::render_cover; use logging::Logger; use patch_hub::lore::{ lore_api_client::BlockingLoreAPIClient, - lore_session, + lore_session::{self, B4Result}, patch::{Author, Patch}, }; use patch_renderer::{render_patch_preview, PatchRenderer}; @@ -132,7 +132,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(); @@ -160,12 +160,14 @@ impl App { screen => bail!(format!("Invalid screen passed as argument {screen:?}")), }; - let patchset_path: String = match log_on_error!(lore_session::download_patchset( + let patchset_path: String = match lore_session::download_patchset( self.config.patchsets_cache_dir(), &representative_patch, - )) { - Ok(result) => result, - Err(io_error) => bail!("{io_error}"), + ) { + lore_session::B4Result::PatchFound(path) => path, + lore_session::B4Result::PatchNotFound(error) => { + return Ok(lore_session::B4Result::PatchNotFound(error)) + } }; match log_on_error!(lore_session::split_patchset(&patchset_path)) { @@ -250,7 +252,7 @@ impl App { lore_api_client: self.lore_api_client.clone(), patchset_path, }); - Ok(()) + Ok(B4Result::PatchFound("Patch found".to_string())) } Err(message) => bail!(message), } diff --git a/src/handler/latest.rs b/src/handler/latest.rs index 6a3b1e6..c0c2c12 100644 --- a/src/handler/latest.rs +++ b/src/handler/latest.rs @@ -3,14 +3,17 @@ use std::ops::ControlFlow; use crate::{ app::{screens::CurrentScreen, App}, 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, Terminal, }; +use patch_hub::lore::lore_session::B4Result; + pub fn handle_latest_patchsets( app: &mut App, key: KeyEvent, @@ -55,9 +58,18 @@ where "Loading patchset" => { let result = app.init_details_actions(); if result.is_ok() { - app.set_current_screen(CurrentScreen::PatchsetDetails); + match result.unwrap() { + B4Result::PatchFound(_) => { + app.set_current_screen(CurrentScreen::PatchsetDetails); + } + + B4Result::PatchNotFound(err_cause) => { + app.popup = Some(InfoPopUp::generate_info_popup("Error",&format!("The selected patchset couldn't be retrieved.\nReason: {err_cause}\nPlease choose another patchset."))); + app.set_current_screen(CurrentScreen::LatestPatchsets); + } + } } - result + Ok(()) } }; } diff --git a/src/lore/lore_session.rs b/src/lore/lore_session.rs index 57d68a6..c184809 100644 --- a/src/lore/lore_session.rs +++ b/src/lore/lore_session.rs @@ -7,6 +7,7 @@ use derive_getters::Getters; use regex::Regex; use serde_xml_rs::from_str; use std::collections::{HashMap, HashSet}; +use std::ffi::OsStr; use std::io::{BufRead, BufReader}; use std::mem::swap; use std::path::Path; @@ -18,6 +19,11 @@ use std::{ }; use thiserror::Error; +pub enum B4Result { + PatchFound(String), + PatchNotFound(String), +} + #[cfg(test)] mod tests; @@ -152,17 +158,20 @@ impl LoreSession { } } -pub fn download_patchset(output_dir: &str, patch: &Patch) -> io::Result { +pub fn download_patchset(output_dir: &str, patch: &Patch) -> B4Result { let message_id: &str = &patch.message_id().href; let mbox_name: String = extract_mbox_name_from_message_id(message_id); if !Path::new(output_dir).exists() { - fs::create_dir_all(output_dir)?; + match fs::create_dir_all(output_dir) { + Ok(_) => {} + Err(_) => return B4Result::PatchNotFound("Couldn't create patches dir.".to_string()), + }; } let filepath: String = format!("{output_dir}/{mbox_name}"); if !Path::new(&filepath).exists() { - Command::new("b4") + match Command::new("b4") .arg("--quiet") .arg("am") .arg("--use-version") @@ -174,10 +183,20 @@ pub fn download_patchset(output_dir: &str, patch: &Patch) -> io::Result .arg(&mbox_name) .stdout(Stdio::null()) .stderr(Stdio::null()) - .status()?; + .status() + { + Ok(_) => {} + Err(_) => return B4Result::PatchNotFound("Couldn't create patch file.".to_string()), + }; + } + + let path = Path::new(OsStr::new(&filepath)); + + if !path.exists() { + return B4Result::PatchNotFound("Couldn't create patch file.".to_string()); } - Ok(filepath) + B4Result::PatchFound(filepath) } fn extract_mbox_name_from_message_id(message_id: &str) -> String {