mirror of
https://github.com/chmln/handlr.git
synced 2024-11-27 11:23:51 +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)
|
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<()> {
|
||||||
|
|
|
@ -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())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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")]
|
||||||
|
|
Loading…
Reference in a new issue