From 460ebef1bfd27ec6afdb4ac693b295e846de88c6 Mon Sep 17 00:00:00 2001 From: zaaarf Date: Wed, 3 Jan 2024 12:14:23 +0100 Subject: [PATCH] chore: better sanitize() function --- src/model.rs | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/model.rs b/src/model.rs index ddeb728..7cd53af 100644 --- a/src/model.rs +++ b/src/model.rs @@ -104,18 +104,14 @@ impl PageInsertion { } pub fn sanitize(&mut self) { - if let Some(author) = self.author.as_mut() { - *author = Self::trim_and_escape(author, AUTHOR_MAX_CHARS); - } - if self.author.is_some() && self.author.as_deref().unwrap().is_empty() { - self.author = None; - } - if let Some(contact) = self.contact.as_mut() { - *contact = Self::trim_and_escape(contact, CONTACT_MAX_CHARS); - } - if self.contact.is_some() && self.contact.as_deref().unwrap().is_empty() { - self.contact = None; - } + self.author = self.author.as_mut() + .map(|a| Self::trim_and_escape(a, AUTHOR_MAX_CHARS).to_string()) + .and_then(|a| if a.is_empty() { None } else { Some(a) }); + + self.contact = self.contact.as_mut() + .map(|c| Self::trim_and_escape(c, CONTACT_MAX_CHARS).to_string()) + .and_then(|c| if c.is_empty() { None } else { Some(c) }); + self.body = Self::trim_and_escape(&self.body, BODY_MAX_CHARS); }