From de0bed481b794e224ec76865aff6cc9cfee02ec8 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 14 Mar 2021 18:19:57 +0100 Subject: Extract HTML tag remover to a function Now that we're using it in two places, move it to a function. Also remove the `unwrap` call. --- src/main.rs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5c28dd1..ee2899e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -49,6 +49,9 @@ enum WrapError { #[error("unable to parse email body")] ParseMailUnknown, + #[error("regex error: {0}")] + Regex(#[from] regex::Error), + #[error(transparent)] Xdg(#[from] xdg::BaseDirectoriesError), @@ -82,6 +85,8 @@ fn main() { OttolangyError::Wrapped(WrapError::ParseMail(_)) | OttolangyError::Wrapped(WrapError::ParseMailUnknown) => process::exit(exitcode::DATAERR), + OttolangyError::Wrapped(WrapError::Regex(_)) => + process::exit(exitcode::SOFTWARE), OttolangyError::Wrapped(WrapError::Xdg(_)) | OttolangyError::Wrapped(WrapError::Io(_)) | OttolangyError::WriteConfig(_) => @@ -136,8 +141,7 @@ fn get_email_body(email: &[u8]) -> Result { let mut body = email.get_body()?; if email.ctype.mimetype == "text/html" { - let re = Regex::new("<[^>]*>").unwrap(); - body = re.replace_all(&body, "").into_owned(); + body = unhtml(&body)?; } println!("body: {:?}", body); @@ -171,10 +175,7 @@ fn extract_multipart_email_body( for part in &email.subparts { if email.ctype.mimetype == "text/html" { - let html_body = part.get_body()?; - let re = Regex::new("<[^>]*>").unwrap(); - - return Ok(re.replace_all(&html_body, "").into_owned()); + return unhtml(&part.get_body()?); } } @@ -182,6 +183,13 @@ fn extract_multipart_email_body( Err(WrapError::ParseMailUnknown) } +/// Remove all HTML tags in `html`. +fn unhtml(html: &str) -> Result { + let re = Regex::new("<[^>]*>")?; + + Ok(re.replace_all(&html, "").into_owned()) +} + /// Write the attribution config to a file. /// /// Store the file in the XDG data directory. -- cgit v1.2.3