~savoy/mailcap

b760b9b30a5f0e455a9b01d3ecd8257703499cd7 — savoy 2 years ago 35810c6
Mailcap::new returns vector of Entry

As multiple mailcap entries can exist for one mime type, the return type
of Mailcap has changed from HashMap<String, Entry> to HashMap<String,
Vec<Entry>>.

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

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

#[derive(Default, Debug, PartialEq)]


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

        let parsed_lines = Self::parse_valid_lines(virgin_lines)?;
        let data: HashMap<String, Entry> = parsed_lines
            .into_iter()
            .map(|i| (i[0].to_owned(), Entry::from(i)))
            .collect();
        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.to_owned())))
                .or_insert(vec![Entry::from(i.to_vec())]);
        });

        Ok(Mailcap { files, data })
    }

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



@@ 339,7 341,7 @@ mod tests {
        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.command, dummy_line.command)
            assert_eq!(i[0].command, dummy_line.command)
        }
    }



@@ 358,7 360,7 @@ mod tests {
        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.command, dummy_line.command)
            assert_eq!(i[0].command, dummy_line.command)
        }
    }
}