From fd147364c8b2ea54dc59d98eedd845163b346925 Mon Sep 17 00:00:00 2001 From: imgurbot12 Date: Sat, 29 Jul 2023 20:30:20 -0700 Subject: [PATCH] feat: remove unessesary error assignment --- plugin-desktop/Cargo.toml | 1 - plugin-desktop/src/main.rs | 67 ++++++++++++++++++-------------------- 2 files changed, 31 insertions(+), 37 deletions(-) diff --git a/plugin-desktop/Cargo.toml b/plugin-desktop/Cargo.toml index 1936159..baa4064 100644 --- a/plugin-desktop/Cargo.toml +++ b/plugin-desktop/Cargo.toml @@ -11,5 +11,4 @@ rmenu-plugin = { version = "0.0.0", path = "../rmenu-plugin" } rust-ini = { version = "0.19.0", features = ["unicase"] } serde_json = "1.0.103" shellexpand = "3.1.0" -thiserror = "1.0.44" walkdir = "2.3.3" diff --git a/plugin-desktop/src/main.rs b/plugin-desktop/src/main.rs index 4f4209f..50f6848 100644 --- a/plugin-desktop/src/main.rs +++ b/plugin-desktop/src/main.rs @@ -5,7 +5,6 @@ use std::{fs::read_to_string, path::Path}; use freedesktop_desktop_entry::DesktopEntry; use ini::Ini; use rmenu_plugin::{Action, Entry}; -use thiserror::Error; use walkdir::WalkDir; static XDG_DATA_ENV: &'static str = "XDG_DATA_DIRS"; @@ -14,16 +13,6 @@ static XDG_DATA_DEFAULT: &'static str = "/usr/share:/usr/local/share"; static XDG_CONFIG_DEFAULT: &'static str = "~/.config"; static DEFAULT_THEME: &'static str = "hicolor"; -#[derive(Error, Debug)] -enum ProcessError { - #[error("Failed to Read Desktop File")] - FileError(#[from] std::io::Error), - #[error("Invalid Desktop File")] - InvalidFile(#[from] freedesktop_desktop_entry::DecodeError), - #[error("No Such Attribute")] - InvalidAttr(&'static str), -} - /// Retrieve XDG-CONFIG-HOME Directory #[inline] fn config_dir() -> PathBuf { @@ -137,32 +126,38 @@ fn data_dirs(dir: &str) -> Vec { } /// Parse XDG Desktop Entry into RMenu Entry -fn parse_desktop(path: &Path, locale: Option<&str>) -> Result { - let bytes = read_to_string(path)?; - let entry = DesktopEntry::decode(&path, &bytes)?; - let name = entry - .name(locale) - .ok_or(ProcessError::InvalidAttr("Name"))? - .to_string(); +fn parse_desktop(path: &Path, locale: Option<&str>) -> Option { + let bytes = read_to_string(path).ok()?; + let entry = DesktopEntry::decode(&path, &bytes).ok()?; + let name = entry.name(locale)?.to_string(); let icon = entry.icon().map(|s| s.to_string()); let comment = entry.comment(locale).map(|s| s.to_string()); - let actions: Vec = entry - .actions() - .unwrap_or("") - .split(";") - .into_iter() - .filter(|a| a.len() > 0) - .filter_map(|a| { - let name = entry.action_name(a, locale)?; - let exec = entry.action_exec(a)?; - Some(Action { - name: name.to_string(), - exec: exec.to_string(), - comment: None, - }) - }) - .collect(); - Ok(Entry { + let mut actions = match entry.exec() { + Some(exec) => vec![Action { + name: "main".to_string(), + exec: exec.to_string(), + comment: None, + }], + None => vec![], + }; + actions.extend( + entry + .actions() + .unwrap_or("") + .split(";") + .into_iter() + .filter(|a| a.len() > 0) + .filter_map(|a| { + let name = entry.action_name(a, locale)?; + let exec = entry.action_exec(a)?; + Some(Action { + name: name.to_string(), + exec: exec.to_string(), + comment: None, + }) + }), + ); + Some(Entry { name, actions, comment, @@ -178,7 +173,7 @@ fn find_desktops(path: PathBuf, locale: Option<&str>) -> Vec { .filter_map(|e| e.ok()) .filter(|e| e.file_name().to_string_lossy().ends_with(".desktop")) .filter(|e| e.file_type().is_file()) - .filter_map(|e| parse_desktop(e.path(), locale).ok()) + .filter_map(|e| parse_desktop(e.path(), locale)) .collect() }