~savoy/mailcap

23e6548d35d3d783e2c972fd2d5fe0a3aeafdd87 — savoy 2 years ago 9269e27
support for wildcard fallbacks e.g. text/*

Signed-off-by: savoy <git@liberation.red>
1 files changed, 38 insertions(+), 1 deletions(-)

M src/lib.rs
M src/lib.rs => src/lib.rs +38 -1
@@ 85,7 85,18 @@ impl Mailcap {
    }

    pub fn get(&self, key: &str) -> Option<&Entry> {
        self.data.get(key)
        match self.data.get(key) {
            Some(v) => Some(v),
            None => {
                let mime_split: Vec<&str> = key.split("/").collect();
                let mut wildcard = String::new();
                if let Some(x) = mime_split.get(0) {
                    wildcard.push_str(x);
                    wildcard.push_str("/*");
                };
                self.data.get(&wildcard)
            }
        }
    }

    fn get_user_home() -> PathBuf {


@@ 304,6 315,24 @@ mod tests {
                textual_new_lines: true,
            },
        );
        cap_data.insert(
            "text/*".to_string(),
            Entry {
                mime_type: "text/*".to_string(),
                command: "qutebrowser '%s'".to_string(),
                compose: None,
                compose_typed: None,
                edit: None,
                print: None,
                test: Some("test -n \"$DISPLAY\"".to_string()),
                note: None,
                description: None,
                name_template: Some("%s.html".to_string()),
                needs_terminal: true,
                copious_output: false,
                textual_new_lines: true,
            },
        );
        Mailcap {
            files: vec![PathBuf::from("/etc/mailcap")],
            data: cap_data,


@@ 455,4 484,12 @@ mod tests {
        let mailcap = dummy_mailcap();
        assert!(mailcap.get("image/jpeg").is_none())
    }

    #[test]
    fn get_mailcap_wildcard_fallback() {
        let mailcap = dummy_mailcap();
        // deliberate mispelling
        let fallback = mailcap.get("text/hmtl").unwrap();
        assert_eq!(fallback.mime_type, "text/*")
    }
}