feat: remove unessesary error assignment

This commit is contained in:
imgurbot12 2023-07-29 20:30:20 -07:00
parent f8d0ce85ea
commit fd147364c8
2 changed files with 31 additions and 37 deletions

View File

@ -11,5 +11,4 @@ rmenu-plugin = { version = "0.0.0", path = "../rmenu-plugin" }
rust-ini = { version = "0.19.0", features = ["unicase"] } rust-ini = { version = "0.19.0", features = ["unicase"] }
serde_json = "1.0.103" serde_json = "1.0.103"
shellexpand = "3.1.0" shellexpand = "3.1.0"
thiserror = "1.0.44"
walkdir = "2.3.3" walkdir = "2.3.3"

View File

@ -5,7 +5,6 @@ use std::{fs::read_to_string, path::Path};
use freedesktop_desktop_entry::DesktopEntry; use freedesktop_desktop_entry::DesktopEntry;
use ini::Ini; use ini::Ini;
use rmenu_plugin::{Action, Entry}; use rmenu_plugin::{Action, Entry};
use thiserror::Error;
use walkdir::WalkDir; use walkdir::WalkDir;
static XDG_DATA_ENV: &'static str = "XDG_DATA_DIRS"; 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 XDG_CONFIG_DEFAULT: &'static str = "~/.config";
static DEFAULT_THEME: &'static str = "hicolor"; 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 /// Retrieve XDG-CONFIG-HOME Directory
#[inline] #[inline]
fn config_dir() -> PathBuf { fn config_dir() -> PathBuf {
@ -137,16 +126,22 @@ fn data_dirs(dir: &str) -> Vec<PathBuf> {
} }
/// Parse XDG Desktop Entry into RMenu Entry /// Parse XDG Desktop Entry into RMenu Entry
fn parse_desktop(path: &Path, locale: Option<&str>) -> Result<Entry, ProcessError> { fn parse_desktop(path: &Path, locale: Option<&str>) -> Option<Entry> {
let bytes = read_to_string(path)?; let bytes = read_to_string(path).ok()?;
let entry = DesktopEntry::decode(&path, &bytes)?; let entry = DesktopEntry::decode(&path, &bytes).ok()?;
let name = entry let name = entry.name(locale)?.to_string();
.name(locale)
.ok_or(ProcessError::InvalidAttr("Name"))?
.to_string();
let icon = entry.icon().map(|s| s.to_string()); let icon = entry.icon().map(|s| s.to_string());
let comment = entry.comment(locale).map(|s| s.to_string()); let comment = entry.comment(locale).map(|s| s.to_string());
let actions: Vec<Action> = 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() .actions()
.unwrap_or("") .unwrap_or("")
.split(";") .split(";")
@ -160,9 +155,9 @@ fn parse_desktop(path: &Path, locale: Option<&str>) -> Result<Entry, ProcessErro
exec: exec.to_string(), exec: exec.to_string(),
comment: None, comment: None,
}) })
}) }),
.collect(); );
Ok(Entry { Some(Entry {
name, name,
actions, actions,
comment, comment,
@ -178,7 +173,7 @@ fn find_desktops(path: PathBuf, locale: Option<&str>) -> Vec<Entry> {
.filter_map(|e| e.ok()) .filter_map(|e| e.ok())
.filter(|e| e.file_name().to_string_lossy().ends_with(".desktop")) .filter(|e| e.file_name().to_string_lossy().ends_with(".desktop"))
.filter(|e| e.file_type().is_file()) .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() .collect()
} }