~savoy/mailcap

40287018a081143d6867a686f199e0006abd81fb — savoy 2 years ago f16bff3
reversion for Mailcap.data

The value of the mailcap data has reverted back to a single entry per
mime type instead of a vector.

To be implemented is a check for the test command of a mailcap entry.
Entry now returns an Option: entries that fail their test will return
None and (eventually) be logged. When Mailcap::new creates the HashMap
of data, only the passed tests will be included.

If multiple entries exist for a mime type, the previous behavior of the
most recent is included.

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

M src/lib.rs
M src/lib.rs => src/lib.rs +13 -15
@@ 30,7 30,7 @@ pub enum MailcapError {
#[derive(Debug, PartialEq)]
pub struct Mailcap {
    files: Vec<PathBuf>,
    data: HashMap<String, Vec<Entry>>,
    data: HashMap<String, Entry>,
}

#[derive(Default, Debug, PartialEq)]


@@ 74,17 74,15 @@ impl Mailcap {
        }

        let parsed_lines = Self::parse_valid_lines(virgin_lines)?;
        let mut data: HashMap<String, Vec<Entry>> = HashMap::new();
        parsed_lines.iter().for_each(|i| {
            data.entry(i[0].to_string())
                .and_modify(|e| e.push(Entry::from(i)))
                .or_insert(vec![Entry::from(i)]);
        });
        let data: HashMap<String, Entry> = parsed_lines
            .iter()
            .filter_map(|i| Some((i[0].to_owned(), Entry::from(i).unwrap())))
            .collect();

        Ok(Mailcap { files, data })
    }

    pub fn get(&self, key: &str) -> Option<&Vec<Entry>> {
    pub fn get(&self, key: &str) -> Option<&Entry> {
        self.data.get(key)
    }



@@ 169,7 167,7 @@ impl Mailcap {
}

impl Entry {
    fn from(line: &Vec<String>) -> Entry {
    fn from(line: &Vec<String>) -> Option<Entry> {
        let mut entry = Entry::default();
        // TODO: validate mime_type against database
        entry.mime_type = line[0].to_owned();


@@ 196,7 194,7 @@ impl Entry {
            }
        }

        entry
        Some(entry)
    }

    pub fn mime(&self) -> &String {


@@ 358,7 356,7 @@ mod tests {
            "needsterminal".to_string(),
            "textualnewlines=1917".to_string(),
        ];
        let entry = Entry::from(&line);
        let entry = Entry::from(&line).unwrap();
        assert_eq!(
            Entry {
                mime_type: "text/html".to_string(),


@@ 390,12 388,12 @@ mod tests {
            vec!["text/html; qutebrowser '%s'; test=test -n \"$DISPLAY\"; nametemplate=%s.html; needsterminal".to_string()]
        );
        let dummy_line_vectorized = Mailcap::parse_valid_lines(dummy_line).unwrap();
        let dummy_line = Entry::from(&dummy_line_vectorized[0]);
        let dummy_line = Entry::from(&dummy_line_vectorized[0]).unwrap();

        env::set_var("MAILCAPS", "/tmp/mailcap-rs.test");
        let mailcap = Mailcap::new().unwrap();
        if let Some(i) = mailcap.data.get("text/html") {
            assert_eq!(i[0].command, dummy_line.command)
            assert_eq!(i.command, dummy_line.command)
        }
    }



@@ 409,12 407,12 @@ mod tests {
        );

        let dummy_line_vectorized = Mailcap::parse_valid_lines(dummy_line).unwrap();
        let dummy_line = Entry::from(&dummy_line_vectorized[1]);
        let dummy_line = Entry::from(&dummy_line_vectorized[1]).unwrap();

        env::set_var("MAILCAPS", "/tmp/mailcap-rs.test");
        let mailcap = Mailcap::new().unwrap();
        if let Some(i) = mailcap.data.get("text/html") {
            assert_eq!(i[0].command, dummy_line.command)
            assert_eq!(i.command, dummy_line.command)
        }
    }
}