~savoy/mailcap

e11d92601f5d38ed12834e327e3fc47874f4463e — savoy 2 years ago b760b9b
better use of borrowing vs owning

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

M src/lib.rs
M src/lib.rs => src/lib.rs +9 -9
@@ 94,8 94,8 @@ impl Mailcap {
        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())]);
                .and_modify(|e| e.push(Entry::from(i)))
                .or_insert(vec![Entry::from(i)]);
        });

        Ok(Mailcap { files, data })


@@ 177,14 177,14 @@ impl Mailcap {
}

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

        for field in line[2..].iter() {
            match Self::parse_arg(&field) {
            match Self::parse_arg(field) {
                Some(("compose", v)) => entry.compose = Some(split_string_into_vector(&v[1..])),
                Some(("composetyped", v)) => {
                    entry.compose_typed = Some(split_string_into_vector(&v[1..]))


@@ 205,10 205,10 @@ impl Entry {
        entry
    }

    fn parse_arg(field: &String) -> Option<(&str, &str)> {
    fn parse_arg(field: &str) -> Option<(&str, &str)> {
        match field.find("=") {
            Some(i) => Some(field.split_at(i)),
            None => Some((field.as_str(), "")),
            None => Some((field, "")),
        }
    }
}


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


@@ 336,7 336,7 @@ 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].to_owned());
        let dummy_line = Entry::from(&dummy_line_vectorized[0]);

        env::set_var("MAILCAPS", "/tmp/mailcap-rs.test");
        let mailcap = Mailcap::new().unwrap();


@@ 355,7 355,7 @@ mod tests {
        );

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

        env::set_var("MAILCAPS", "/tmp/mailcap-rs.test");
        let mailcap = Mailcap::new().unwrap();