chore: better sanitize() function

This commit is contained in:
zaaarf 2024-01-03 12:14:23 +01:00
parent 6c6c0fd62b
commit 460ebef1bf
No known key found for this signature in database
GPG key ID: 102E445F4C3F829B

View file

@ -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);
}