mirror of
https://github.com/chmln/handlr.git
synced 2024-11-14 21:49:27 +01:00
implement reasonable fallback for text/* types
This commit is contained in:
parent
2143ddf067
commit
845aa5f213
3 changed files with 17 additions and 8 deletions
|
@ -67,12 +67,19 @@ impl MimeApps {
|
|||
Ok(handler)
|
||||
}
|
||||
Some(handlers) => Ok(handlers.get(0).unwrap().clone()),
|
||||
None => self
|
||||
None => match self
|
||||
.added_associations
|
||||
.get(mime)
|
||||
.map(|h| h.get(0).unwrap().clone())
|
||||
.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<()> {
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
use crate::{Error, Result};
|
||||
use mime::Mime;
|
||||
use mime_detective::MimeDetective;
|
||||
use std::{
|
||||
convert::TryFrom,
|
||||
path::{Path, PathBuf},
|
||||
|
@ -30,11 +29,16 @@ impl TryFrom<&str> for MimeType {
|
|||
impl TryFrom<&Path> for MimeType {
|
||||
type Error = Error;
|
||||
fn try_from(path: &Path) -> Result<Self> {
|
||||
match MimeDetective::new()?.detect_filepath(path)? {
|
||||
guess if guess == mime::APPLICATION_OCTET_STREAM => {
|
||||
match xdg_mime::SharedMimeInfo::new()
|
||||
.guess_mime_type()
|
||||
.path(&path)
|
||||
.guess()
|
||||
.mime_type()
|
||||
{
|
||||
guess if guess == &mime::APPLICATION_OCTET_STREAM => {
|
||||
Err(Error::Ambiguous(path.to_owned()))
|
||||
}
|
||||
guess => Ok(Self(guess)),
|
||||
guess => Ok(Self(guess.clone())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,8 +18,6 @@ pub enum Error {
|
|||
BadMimeType(#[from] mime::FromStrError),
|
||||
#[error("malformed desktop entry at {0}")]
|
||||
BadEntry(std::path::PathBuf),
|
||||
#[error(transparent)]
|
||||
MimeDetect(#[from] mime_detective::DetectiveError),
|
||||
#[error("error spawning selector process '{0}'")]
|
||||
Selector(String),
|
||||
#[error("selection cancelled")]
|
||||
|
|
Loading…
Reference in a new issue