mirror of
https://github.com/chmln/handlr.git
synced 2024-11-27 03:13:49 +01:00
Fix mime_types logic and add some tests
This commit is contained in:
parent
4f2940135e
commit
84a5df3e98
4 changed files with 33 additions and 20 deletions
|
@ -32,7 +32,7 @@ impl TryFrom<&Path> for MimeType {
|
||||||
fn try_from(path: &Path) -> Result<Self> {
|
fn try_from(path: &Path) -> Result<Self> {
|
||||||
match MimeDetective::new()?.detect_filepath(path)? {
|
match MimeDetective::new()?.detect_filepath(path)? {
|
||||||
guess if guess == mime::APPLICATION_OCTET_STREAM => {
|
guess if guess == mime::APPLICATION_OCTET_STREAM => {
|
||||||
Err(Error::Ambiguous(path.to_string_lossy().into()))
|
Err(Error::Ambiguous(path.to_owned()))
|
||||||
}
|
}
|
||||||
guess => Ok(Self(guess)),
|
guess => Ok(Self(guess)),
|
||||||
}
|
}
|
||||||
|
@ -42,14 +42,20 @@ impl TryFrom<&Path> for MimeType {
|
||||||
// Mime derived from user input: extension(.pdf) or type like image/jpg
|
// Mime derived from user input: extension(.pdf) or type like image/jpg
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct MimeOrExtension(pub Mime);
|
pub struct MimeOrExtension(pub Mime);
|
||||||
|
|
||||||
impl FromStr for MimeOrExtension {
|
impl FromStr for MimeOrExtension {
|
||||||
type Err = Error;
|
type Err = Error;
|
||||||
fn from_str(s: &str) -> Result<Self> {
|
fn from_str(s: &str) -> Result<Self> {
|
||||||
if s.starts_with(".") {
|
let mime = if s.starts_with(".") {
|
||||||
Ok(Self(MimeType::try_from(s)?.0))
|
mime_db::lookup(&s[1..])
|
||||||
|
.ok_or(Error::Ambiguous(s.into()))?
|
||||||
|
.parse::<Mime>()
|
||||||
|
.unwrap()
|
||||||
} else {
|
} else {
|
||||||
Ok(Self(Mime::from_str(s)?))
|
Mime::from_str(s)?
|
||||||
}
|
};
|
||||||
|
|
||||||
|
Ok(Self(mime))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,22 +64,25 @@ mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn user_input() {
|
fn user_input() -> Result<()> {
|
||||||
"image/jpg".parse::<MimeOrExtension>().unwrap();
|
assert_eq!(MimeOrExtension::from_str(".pdf")?.0, mime::APPLICATION_PDF);
|
||||||
".jpg".parse::<MimeOrExtension>().unwrap();
|
assert_eq!(
|
||||||
|
MimeOrExtension::from_str("image/jpeg")?.0,
|
||||||
|
mime::IMAGE_JPEG
|
||||||
|
);
|
||||||
|
|
||||||
"image//jpg".parse::<MimeOrExtension>().unwrap_err();
|
"image//jpg".parse::<MimeOrExtension>().unwrap_err();
|
||||||
"image".parse::<MimeOrExtension>().unwrap_err();
|
"image".parse::<MimeOrExtension>().unwrap_err();
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn from_path_with_extension() {
|
fn from_path() -> Result<()> {
|
||||||
assert_eq!(
|
assert_eq!(MimeType::try_from(".")?.0.essence_str(), "inode/directory");
|
||||||
MimeType::try_from(".pdf").unwrap().0,
|
assert_eq!(MimeType::try_from("./tests/cat")?.0.type_(), "text");
|
||||||
mime::APPLICATION_PDF
|
assert_eq!(MimeType::try_from("./tests/rust.vim")?.0.type_(), "text");
|
||||||
);
|
|
||||||
assert_eq!(
|
Ok(())
|
||||||
MimeType::try_from(".").unwrap().0.essence_str(),
|
|
||||||
"inode/directory"
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,10 +10,10 @@ pub enum Error {
|
||||||
Xdg(#[from] xdg::BaseDirectoriesError),
|
Xdg(#[from] xdg::BaseDirectoriesError),
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
Config(#[from] confy::ConfyError),
|
Config(#[from] confy::ConfyError),
|
||||||
#[error("no handler defined for {0}")]
|
#[error("no handlers found for '{0}'")]
|
||||||
NotFound(String),
|
NotFound(String),
|
||||||
#[error("could not figure out the mime type {0}")]
|
#[error("could not figure out the mime type of '{0}'")]
|
||||||
Ambiguous(String),
|
Ambiguous(std::path::PathBuf),
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
BadMimeType(#[from] mime::FromStrError),
|
BadMimeType(#[from] mime::FromStrError),
|
||||||
#[error("malformed desktop entry at {0}")]
|
#[error("malformed desktop entry at {0}")]
|
||||||
|
|
2
tests/cat
Executable file
2
tests/cat
Executable file
|
@ -0,0 +1,2 @@
|
||||||
|
#!/bin/sh
|
||||||
|
bat "$@"
|
2
tests/rust.vim
Normal file
2
tests/rust.vim
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
#!/bin/sh
|
||||||
|
bat "$@"
|
Loading…
Reference in a new issue