summaryrefslogtreecommitdiffstats
path: root/src/trait_ext.rs
blob: 90ce6d3784b6795ee3bac7fb878a078f9571463d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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 mut file = File::new_from_path(&self, None)
            .map_err(|e| MimeError::AccessFailed(Box::new(e)))?;
        file.meta_sync()
            .map_err(|e| MimeError::AccessFailed(Box::new(e)))?;


        file.get_mime()
            .map(|mime| {
                Ok(format!("{}", mime))
            })
            .ok_or(MimeError::NoMimeFound)?
    }
}