diff options
author | Teddy Wing | 2021-03-09 00:49:34 +0100 |
---|---|---|
committer | Teddy Wing | 2021-03-09 00:49:34 +0100 |
commit | 25c3d390ff66c8583a62dc6d85c70c35c4aa1407 (patch) | |
tree | a715f570d207772fb0d0645c3a513c1d97ad84fb /src/main.rs | |
parent | 79d34c91c9dd192342dd4df6edd210729510c84f (diff) | |
download | mutt-ottolangy-25c3d390ff66c8583a62dc6d85c70c35c4aa1407.tar.bz2 |
Move email body parsing code to a new function
Isolate this block.
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 16 |
1 files changed, 13 insertions, 3 deletions
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<String, Box<dyn Error>> { + 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()) } |