Skip to content

Commit

Permalink
refactor: better separate user app and system app logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Anomalocaridid committed Oct 14, 2024
1 parent 7cc44c0 commit 8fbb702
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
18 changes: 13 additions & 5 deletions src/apps/system.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
apps::DesktopList,
common::{DesktopEntry, DesktopHandler, Handleable},
error::Result,
error::{Error, Result},
};
use mime::Mime;
use std::{collections::BTreeMap, convert::TryFrom, ffi::OsString};
Expand All @@ -16,13 +16,21 @@ pub struct SystemApps {

impl SystemApps {
/// Get the list of handlers associated with a given mime
pub fn get_handlers(&self, mime: &Mime) -> Option<DesktopList> {
Some(self.associations.get(mime)?.clone())
pub fn get_handlers(&self, mime: &Mime) -> Result<DesktopList> {
Ok(self
.associations
.get(mime)
.ok_or_else(|| Error::NotFound(mime.to_string()))?
.clone())
}

/// Get the primary of handler associated with a given mime
pub fn get_handler(&self, mime: &Mime) -> Option<DesktopHandler> {
Some(self.get_handlers(mime)?.front()?.clone())
pub fn get_handler(&self, mime: &Mime) -> Result<DesktopHandler> {
Ok(self
.get_handlers(mime)?
.front()
.ok_or_else(|| Error::NotFound(mime.to_string()))?
.clone())
}

/// Get all system-level desktop entries on the system
Expand Down
9 changes: 4 additions & 5 deletions src/config/main_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ impl Config {
pub fn get_handler(&self, mime: &Mime) -> Result<DesktopHandler> {
match self.mime_apps.get_handler_from_user(mime, &self.config) {
Err(e) if matches!(e, Error::Cancelled) => Err(e),
h => h.or_else(|_| self.get_handler_from_added_associations(mime)),
h => h
.or_else(|_| self.get_handler_from_added_associations(mime))
.or_else(|_| self.system_apps.get_handler(mime)),
}
}

Expand All @@ -70,10 +72,7 @@ impl Config {
self.mime_apps
.added_associations
.get(mime)
.map_or_else(
|| self.system_apps.get_handler(mime),
|h| h.front().cloned(),
)
.and_then(|h| h.front().cloned())
.ok_or_else(|| Error::NotFound(mime.to_string()))
}

Expand Down

0 comments on commit 8fbb702

Please sign in to comment.