~asimpson/mailpreview-cli

356ba76f2e7e80b887d4416a651d357818e2614f — Adam Simpson 2 years ago a95228a
refactor: make error handling and feedback better
1 files changed, 19 insertions(+), 12 deletions(-)

M src/main.rs
M src/main.rs => src/main.rs +19 -12
@@ 1,10 1,11 @@
use mailparse::*;
use std::error::Error;
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();
    let mut body = String::new();

    if mail.subparts.len() > 0 {
        for m in mail.subparts.iter() {


@@ 16,27 17,33 @@ fn return_body(mail: ParsedMail) -> Result<String, MailParseError> {
        body = mail.get_body()?;
    }

    return Ok(body);
    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");
fn return_path_from_cli() -> Result<String, String> {
    let file = std::env::args().nth(1);

    match file {
        Some(f) => Ok(f),
        None => Err("No message file was provided.".to_string()),
    }
}

fn main() -> Result<(), Box<dyn Error>> {
    let file = return_path_from_cli()?;
    let mut contents = String::new();
    File::open(file.trim())?.read_to_string(&mut contents)?;

    file_handle
        .read_to_string(&mut contents)
        .expect("actual message content");
    let mail = parse_mail(contents.as_bytes()).expect("parsed mail struct");
    let mail = parse_mail(contents.as_bytes())?;
    let message_id = mail
        .headers
        .get_first_value("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 body = return_body(mail).expect("parsed body");


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

    Ok(())
}