summaryrefslogtreecommitdiffstats
path: root/src/config/mimetype.rs
blob: 8c730c0c2d6ee88cae674316749c3de46d882f93 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use serde_derive::Deserialize;
use std::collections::HashMap;
use std::fmt;

use crate::config::{parse_config_file, Flattenable};
use crate::MIMETYPE_FILE;

#[derive(Debug, Deserialize)]
pub struct JoshutoMimetypeEntry {
    pub program: String,
    pub args: Option<Vec<String>>,
    pub fork: Option<bool>,
    pub silent: Option<bool>,
}

impl std::fmt::Display for JoshutoMimetypeEntry {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(self.program.as_str()).unwrap();
        if let Some(s) = self.args.as_ref() {
            for arg in s {
                write!(f, " {}", arg).unwrap();
            }
        }
        f.write_str("\t[").unwrap();
        if let Some(s) = self.fork {
            if s {
                f.write_str("fork,").unwrap();
            }
        }
        if let Some(s) = self.silent {
            if s {
                f.write_str("silent").unwrap();
            }
        }
        f.write_str("]")
    }
}

#[derive(Debug, Deserialize)]
pub struct JoshutoRawMimetype {
    mimetype: Option<HashMap<String, Vec<JoshutoMimetypeEntry>>>,
    extension: Option<HashMap<String, Vec<JoshutoMimetypeEntry>>>,
}

impl JoshutoRawMimetype {
    #[allow(dead_code)]
    pub fn new() -> Self {
        JoshutoRawMimetype {
            mimetype: None,
            extension: None,
        }
    }
}

impl Flattenable<JoshutoMimetype> for JoshutoRawMimetype {
    fn flatten(self) -> JoshutoMimetype {
        let mimetype = self.mimetype.unwrap_or_default();
        let extension = self.extension.unwrap_or_default();

        JoshutoMimetype {
            mimetype,
            extension,
        }
    }
}

#[derive(Debug)]
pub struct JoshutoMimetype {
    pub mimetype: HashMap<String, Vec<JoshutoMimetypeEntry>>,
    pub extension: HashMap<String, Vec<JoshutoMimetypeEntry>>,
}

impl JoshutoMimetype {
    pub fn new() -> Self {
        JoshutoMimetype {
            mimetype: HashMap::new(),
            extension: HashMap::new(),
        }
    }

    pub fn get_config() -> JoshutoMimetype {
        parse_config_file::<JoshutoRawMimetype, JoshutoMimetype>(MIMETYPE_FILE)
            .unwrap_or_else(JoshutoMimetype::new)
    }
}