~savoy/mailcap

4bbfb1b118d3ce4565ff8545fd64225e8bb7f80f — savoy 2 years ago 7b2486d
creation of Mailcap struct

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

M src/lib.rs
M src/lib.rs => src/lib.rs +30 -9
@@ 21,15 21,16 @@ use std::path::PathBuf;
use std::{env, fmt};

#[derive(Debug)]
enum MailcapError {
pub enum MailcapError {
    LineParseError,
    FileParseError,
    NoValidFiles,
}

struct Mailcap {
    file: PathBuf,
    data: HashMap<String, Mailcap>,
#[derive(Debug, PartialEq)]
pub struct Mailcap {
    files: Vec<PathBuf>,
    data: HashMap<String, Entry>,
}

#[derive(Default, Debug, PartialEq)]


@@ 77,7 78,27 @@ impl fmt::Display for MailcapError {
}

impl Mailcap {
    //pub fn new(&self) -> Result<Mailcap, MailcapError> {
    pub fn new() -> Result<Mailcap, MailcapError> {
        let mut files = Self::list_potential_files();
        Self::check_files_exist(&mut files)?;

        let mut virgin_lines: Vec<String> = vec![];
        for file in &files {
            match Self::get_mailcap_lines(&file) {
                Ok(mut i) => virgin_lines.append(&mut i),
                Err(_) => continue,
            };
        }

        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();

        Ok(Mailcap { files, data })
    }

    //pub fn lookup(&self) -> Entry {}
    fn list_potential_files() -> Vec<PathBuf> {
        let mut mailcap_files: Vec<PathBuf> = vec![];


@@ 126,7 147,7 @@ impl Mailcap {
        Ok(correct_lines)
    }

    fn parse_valid_lines(all_lines: &Vec<String>) -> Result<Vec<Vec<String>>, MailcapError> {
    fn parse_valid_lines(all_lines: Vec<String>) -> Result<Vec<Vec<String>>, MailcapError> {
        let mut lines: Vec<Vec<String>> = vec![];
        for line in all_lines {
            lines.push(


@@ 151,7 172,7 @@ 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();


@@ 239,7 260,7 @@ mod tests {
            String::from("text/html; qutebrowser '%s'; test=test -n \"$DISPLAY\"; nametemplate=%s.html; needsterminal"),
            String::from("image/*; feh -g 1280x720 --scale-down '%s'; test=test -n \"$DISPLAY\"")
        ];
        let lines = Mailcap::parse_valid_lines(&all_lines).unwrap();
        let lines = Mailcap::parse_valid_lines(all_lines).unwrap();
        assert_eq!(
            vec![
                vec![


@@ 269,7 290,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(),