From 25c3d390ff66c8583a62dc6d85c70c35c4aa1407 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Tue, 9 Mar 2021 00:49:34 +0100 Subject: Move email body parsing code to a new function Isolate this block. --- src/main.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 42e921c..734e302 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ use mailparse; +use std::error::Error; use std::io::{self, Read}; @@ -9,10 +10,17 @@ fn main() { let mut stdin = io::stdin(); stdin.read_to_end(&mut email_input).unwrap(); - let email = mailparse::parse_mail(&email_input).unwrap(); + let body = get_email_body(&email_input).unwrap(); + print!("{}", body); +} + +fn get_email_body(email: &[u8]) -> Result> { + let email = mailparse::parse_mail(&email).unwrap(); + if email.subparts.is_empty() { let body = email.get_body().unwrap(); - println!("{}", body); + + return Ok(body); } else { for part in email.subparts { for header in part.get_headers() { @@ -21,9 +29,11 @@ fn main() { if header.get_key() == "Content-Type" && header.get_value().starts_with("text/plain") { - print!("{}", part.get_body().unwrap()); + return Ok(part.get_body().unwrap()); } } } } + + Err("parse".into()) } -- cgit v1.2.3