~asimpson/mailpreview-cli

a95228a114de2740a4ea005dfe1048b31c67d016 — Adam Simpson 2 years ago a44d951
refactor: move body_parse into its own func
1 files changed, 18 insertions(+), 11 deletions(-)

M src/main.rs
M src/main.rs => src/main.rs +18 -11
@@ 3,6 3,22 @@ use std::fs::File;
use std::io::prelude::*;
use std::io::Read;

fn return_body(mail: ParsedMail) -> Result<String, MailParseError> {
    let mut body = "".to_string();

    if mail.subparts.len() > 0 {
        for m in mail.subparts.iter() {
            if m.ctype.mimetype == "text/plain" {
                body = m.get_body()?;
            }
        }
    } else {
        body = mail.get_body()?;
    }

    return Ok(body);
}

fn main() {
    let file = std::env::args().nth(1).expect("mail message to parse");
    let mut file_handle = File::open(file.trim()).expect("file handle to contents of message");


@@ 15,20 31,11 @@ fn main() {
    let message_id = mail
        .headers
        .get_first_value("Message-ID")
        .expect("Message ID");
        .expect("Message-ID");
    let file_name = format!("/tmp/{}", message_id);
    let mut tmp_file = File::create(&file_name).expect("tmp file created");
    let mut body = "".to_string();
    let body = return_body(mail).expect("parsed body");

    if mail.subparts.len() > 0 {
        for m in mail.subparts.iter() {
            if m.ctype.mimetype == "text/plain" {
                body = m.get_body().expect("message content");
            }
        }
    } else {
        body = mail.get_body().expect("message content");
    }

    println!("{}", &file_name);
    tmp_file.write_all(body.as_bytes()).expect("file created");