aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
authorTeddy Wing2021-03-14 18:19:57 +0100
committerTeddy Wing2021-03-14 18:19:57 +0100
commitde0bed481b794e224ec76865aff6cc9cfee02ec8 (patch)
tree3ad23365a38903e92e7de6b817cc289b4637c3a5 /src/main.rs
parentc60b9819991cd7a78bbc1f260484bf426d31827c (diff)
downloadmutt-ottolangy-de0bed481b794e224ec76865aff6cc9cfee02ec8.tar.bz2
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.
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs20
1 files 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<String, WrapError> {
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<String, WrapError> {
+ 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.