From 2aede33db64a91630716f8ee8b7f7974a9d59e48 Mon Sep 17 00:00:00 2001 From: 2Shirt <2xShirt@gmail.com> Date: Mon, 5 May 2025 21:15:50 -0700 Subject: [PATCH] Improve handling of pe tool toml files --- pe_menu/src/app.rs | 56 +++++++++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/pe_menu/src/app.rs b/pe_menu/src/app.rs index 6f10996..ed9afee 100644 --- a/pe_menu/src/app.rs +++ b/pe_menu/src/app.rs @@ -76,7 +76,7 @@ impl App { let disk_list_arc = Arc::new(Mutex::new(Vec::new())); let tasks = Tasks::new(action_tx.clone(), disk_list_arc.clone()); let mut list = StatefulList::default(); - list.set_items(load_tools()); + list.set_items(load_tools(action_tx.clone())); Ok(Self { // TUI action_rx, @@ -441,21 +441,45 @@ fn build_right_items(app: &App) -> Action { Action::UpdateRight(labels, start_index, items) } -pub fn load_tools() -> Vec { +pub fn load_tools(action_tx: mpsc::UnboundedSender) -> Vec { + let mut entries: Vec; + let mut tools: Vec = Vec::new(); let exe_path = env::current_exe().expect("Failed to find main executable"); let tool_config_path = exe_path.parent().unwrap().join("menu_entries"); - let mut entries: Vec = std::fs::read_dir(tool_config_path) - .expect("Failed to find any tool configs") - .map(|res| res.map(|e| e.path())) - .filter_map(Result::ok) - .collect(); - entries.sort(); - entries - .iter() - .map(|entry| { - let contents = fs::read_to_string(entry).expect("Failed to read tool config file"); - let tool: Tool = toml::from_str(&contents).expect("Failed to parse tool config file"); - tool - }) - .collect() + if let Ok(read_dir) = std::fs::read_dir(tool_config_path) { + entries = read_dir + .map(|res| res.map(|e| e.path())) + .filter_map(Result::ok) + .collect(); + entries.sort(); + } else { + action_tx + .send(Action::Error(String::from( + "Failed to find any tool configs", + ))) + .unwrap(); + entries = Vec::new(); + } + entries.iter().for_each(|entry| { + if let Ok(toml_str) = fs::read_to_string(entry) { + if let Ok(tool) = toml::from_str(&toml_str) { + tools.push(tool); + } else { + action_tx + .send(Action::Error(format!( + "Failed to parse tool config file: {:?}", + &entry, + ))) + .unwrap(); + } + } else { + action_tx + .send(Action::Error(format!( + "Failed to read tool config file: {:?}", + &entry, + ))) + .unwrap(); + } + }); + tools }