1
0
mirror of https://github.com/bobwen-dev/hunter synced 2025-04-12 00:55:41 +02:00
hunter/src/trait_ext.rs
2020-02-11 23:46:14 +01:00

46 lines
847 B
Rust

use std::path::PathBuf;
use crate::fail::{HResult, MimeError};
use crate::files::File;
// This makes using short-circuiting iterators more convenient
pub trait ExtractResult<T> {
fn extract(self) -> T;
}
impl<T> ExtractResult<T> for Result<T,T> {
fn extract(self) -> T {
match self {
Ok(val) => val,
Err(val) => val
}
}
}
// To get MIME from Path without hassle
pub trait PathBufMime {
fn get_mime(&self) -> HResult<String>;
}
impl PathBufMime for PathBuf {
fn get_mime(&self) -> HResult<String> {
let file = File::new_from_path(&self)
.map_err(|e| MimeError::AccessFailed(Box::new(e)))?;
file.get_mime()
.map(|mime| {
Ok(format!("{}", mime))
})
.map_err(|_| MimeError::NoMimeFound)?
}
}