~savoy/mailcap

7b2486d4096e378c2d7d7ab8aca56fcaca203626 — savoy 2 years ago ae71e31
more thorough error handling in parsing valid lines

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

M src/lib.rs
M src/lib.rs => src/lib.rs +11 -5
@@ 24,7 24,7 @@ use std::{env, fmt};
enum MailcapError {
    LineParseError,
    FileParseError,
    EmptyFiles,
    NoValidFiles,
}

struct Mailcap {


@@ 69,7 69,7 @@ fn split_string_into_vector(string: &str) -> Vec<String> {
impl fmt::Display for MailcapError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            MailcapError::EmptyFiles => write!(f, "No populated mailcap found"),
            MailcapError::NoValidFiles => write!(f, "No populated mailcap found"),
            MailcapError::LineParseError => write!(f, "Unable to parse mailcap lines"),
            MailcapError::FileParseError => write!(f, "Unable to parse mailcap file"),
        }


@@ 107,7 107,7 @@ impl Mailcap {
        mailcap_files.retain(|i| i.exists());

        match mailcap_files.is_empty() {
            true => Err(MailcapError::EmptyFiles),
            true => Err(MailcapError::NoValidFiles),
            false => Ok(()),
        }
    }


@@ 138,8 138,14 @@ impl Mailcap {
        }

        match lines.is_empty() {
            true => Err(MailcapError::LineParseError),
            false => Ok(lines),
            true => Err(MailcapError::FileParseError),
            false => {
                if lines.iter().all(|i| i.len() >= 2) {
                    Ok(lines)
                } else {
                    Err(MailcapError::FileParseError)
                }
            }
        }
    }
}