~savoy/mailcap

f16bff3e084c0574e16a5e10b0f4c16bb7b73661 — savoy 2 years ago 8c156a4
refactored helper functions as associated for structs

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

M src/lib.rs
M src/lib.rs => src/lib.rs +28 -26
@@ 50,23 50,6 @@ pub struct Entry {
    textual_new_lines: bool, // either 1 or 0 in file
}

fn get_user_home() -> PathBuf {
    let home = match env::var("HOME") {
        Ok(i) => PathBuf::from(i),
        Err(_) => PathBuf::from("."),
    };

    home
}

fn split_string_into_vector(string: &str) -> Vec<String> {
    string
        .split(" ")
        .map(|i| i.trim())
        .map(str::to_string)
        .collect()
}

impl fmt::Display for MailcapError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {


@@ 105,6 88,15 @@ impl Mailcap {
        self.data.get(key)
    }

    fn get_user_home() -> PathBuf {
        let home = match env::var("HOME") {
            Ok(i) => PathBuf::from(i),
            Err(_) => PathBuf::from("."),
        };

        home
    }

    fn list_potential_files() -> Vec<PathBuf> {
        let mut mailcap_files: Vec<PathBuf> = vec![];
        if let Ok(paths) = env::var("MAILCAPS") {


@@ 114,7 106,7 @@ impl Mailcap {
        };

        if mailcap_files.is_empty() {
            let home = get_user_home();
            let home = Self::get_user_home();

            let mut default_locations: Vec<PathBuf> = vec![
                PathBuf::from("/usr/local/etc/mailcap"),


@@ 181,17 173,19 @@ impl 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]);
        entry.command = Self::split_string_into_vector(&line[1]);

        for field in line[2..].iter() {
            match Self::parse_arg(field) {
                Some(("compose", v)) => entry.compose = Some(split_string_into_vector(&v[1..])),
                Some(("compose", v)) => {
                    entry.compose = Some(Self::split_string_into_vector(&v[1..]))
                }
                Some(("composetyped", v)) => {
                    entry.compose_typed = Some(split_string_into_vector(&v[1..]))
                    entry.compose_typed = Some(Self::split_string_into_vector(&v[1..]))
                }
                Some(("edit", v)) => entry.edit = Some(split_string_into_vector(&v[1..])),
                Some(("print", v)) => entry.print = Some(split_string_into_vector(&v[1..])),
                Some(("test", v)) => entry.test = Some(split_string_into_vector(&v[1..])),
                Some(("edit", v)) => entry.edit = Some(Self::split_string_into_vector(&v[1..])),
                Some(("print", v)) => entry.print = Some(Self::split_string_into_vector(&v[1..])),
                Some(("test", v)) => entry.test = Some(Self::split_string_into_vector(&v[1..])),
                Some(("note", v)) => entry.note = Some(v[1..].to_string()),
                Some(("description", v)) => entry.description = Some(v[1..].to_string()),
                Some(("nametemplate", v)) => entry.name_template = Some(v[1..].to_string()),


@@ 263,6 257,14 @@ impl Entry {
            None => Some((field, "")),
        }
    }

    fn split_string_into_vector(string: &str) -> Vec<String> {
        string
            .split(" ")
            .map(|i| i.trim())
            .map(str::to_string)
            .collect()
    }
}

#[cfg(test)]


@@ 295,7 297,7 @@ mod tests {
            env::remove_var("MAILCAPS")
        }

        let home = get_user_home();
        let home = Mailcap::get_user_home();
        let default_locations: Vec<PathBuf> = vec![
            PathBuf::from("/usr/local/etc/mailcap"),
            PathBuf::from("/usr/etc/mailcap"),


@@ 310,7 312,7 @@ mod tests {
    fn mailcap_lines() {
        env::remove_var("MAILCAPS");

        let home = get_user_home();
        let home = Mailcap::get_user_home();
        let local_location = home.join(".mailcap");

        let correct_lines = Mailcap::get_mailcap_lines(&local_location);