implement reasonable fallback for text/* types

This commit is contained in:
Gregory 2020-06-18 15:50:59 -04:00
parent 2143ddf067
commit 845aa5f213
No known key found for this signature in database
GPG key ID: 2E44FAEEDC94B1E2
3 changed files with 17 additions and 8 deletions

View file

@ -67,12 +67,19 @@ impl MimeApps {
Ok(handler) Ok(handler)
} }
Some(handlers) => Ok(handlers.get(0).unwrap().clone()), Some(handlers) => Ok(handlers.get(0).unwrap().clone()),
None => self None => match self
.added_associations .added_associations
.get(mime) .get(mime)
.map(|h| h.get(0).unwrap().clone()) .map(|h| h.get(0).unwrap().clone())
.or_else(|| self.system_apps.get_handler(mime)) .or_else(|| self.system_apps.get_handler(mime))
.ok_or(Error::NotFound(mime.to_string())), .ok_or(Error::NotFound(mime.to_string()))
{
Ok(h) => Ok(h),
Err(Error::NotFound(_)) if mime.type_() == "text" => self
.get_handler(&mime::TEXT_PLAIN)
.map_err(|_| Error::NotFound(mime.to_string())),
Err(e) => Err(e),
},
} }
} }
pub fn show_handler(&self, mime: &Mime, output_json: bool) -> Result<()> { pub fn show_handler(&self, mime: &Mime, output_json: bool) -> Result<()> {

View file

@ -1,6 +1,5 @@
use crate::{Error, Result}; use crate::{Error, Result};
use mime::Mime; use mime::Mime;
use mime_detective::MimeDetective;
use std::{ use std::{
convert::TryFrom, convert::TryFrom,
path::{Path, PathBuf}, path::{Path, PathBuf},
@ -30,11 +29,16 @@ impl TryFrom<&str> for MimeType {
impl TryFrom<&Path> for MimeType { impl TryFrom<&Path> for MimeType {
type Error = Error; type Error = Error;
fn try_from(path: &Path) -> Result<Self> { fn try_from(path: &Path) -> Result<Self> {
match MimeDetective::new()?.detect_filepath(path)? { match xdg_mime::SharedMimeInfo::new()
guess if guess == mime::APPLICATION_OCTET_STREAM => { .guess_mime_type()
.path(&path)
.guess()
.mime_type()
{
guess if guess == &mime::APPLICATION_OCTET_STREAM => {
Err(Error::Ambiguous(path.to_owned())) Err(Error::Ambiguous(path.to_owned()))
} }
guess => Ok(Self(guess)), guess => Ok(Self(guess.clone())),
} }
} }
} }

View file

@ -18,8 +18,6 @@ pub enum Error {
BadMimeType(#[from] mime::FromStrError), BadMimeType(#[from] mime::FromStrError),
#[error("malformed desktop entry at {0}")] #[error("malformed desktop entry at {0}")]
BadEntry(std::path::PathBuf), BadEntry(std::path::PathBuf),
#[error(transparent)]
MimeDetect(#[from] mime_detective::DetectiveError),
#[error("error spawning selector process '{0}'")] #[error("error spawning selector process '{0}'")]
Selector(String), Selector(String),
#[error("selection cancelled")] #[error("selection cancelled")]