Skip to content

Commit

Permalink
Minor fixes (#87)
Browse files Browse the repository at this point in the history
* Move multipart classification into own closure

* Add classification enum

* Express classification indirectly with an enum

* Express logic clause in equivalent way, add assert inline test to make sure of it

* Remove old classifier

* Remove enum indirection

* Remove closure

* Hoist common if condition into outer scope

* Simplify expression

* Simplify lifetime bounds

A missing lifetime in the GetHeader trait was inferred by lifetime elision rules
to be shorter than necessary, leading to awkward lifetime constraints being
propagated throughout many functions that didnt need them.

* Prohibit Rust 2018 idioms, return longest feasible lifetimes

* Dont duplicate crlf in extension when saving failing results
  • Loading branch information
sftse authored Oct 21, 2024
1 parent 54ca7df commit 0baffdd
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 99 deletions.
2 changes: 1 addition & 1 deletion src/core/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl<'x> Address<'x> {
}

/// Returns an iterator over the addresses in the list, or the addresses in the groups.
pub fn iter<'y: 'x>(&'y self) -> Box<dyn DoubleEndedIterator<Item = &Addr<'x>> + 'x> {
pub fn iter(&self) -> Box<dyn DoubleEndedIterator<Item = &Addr<'x>> + '_> {
match self {
Address::List(list) => Box::new(list.iter()),
Address::Group(group) => {
Expand Down
42 changes: 21 additions & 21 deletions src/core/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl<'x> Header<'x> {
}

/// Returns the parsed header value
pub fn value(&self) -> &HeaderValue {
pub fn value(&self) -> &HeaderValue<'x> {
&self.value
}

Expand Down Expand Up @@ -171,14 +171,14 @@ impl<'x> HeaderValue<'x> {
}
}

pub fn as_received(&self) -> Option<&Received> {
pub fn as_received(&self) -> Option<&Received<'x>> {
match *self {
HeaderValue::Received(ref r) => Some(r),
_ => None,
}
}

pub fn as_content_type(&self) -> Option<&ContentType> {
pub fn as_content_type(&self) -> Option<&ContentType<'x>> {
match *self {
HeaderValue::ContentType(ref c) => Some(c),
_ => None,
Expand Down Expand Up @@ -374,7 +374,7 @@ impl<'x> HeaderName<'x> {
}
}

pub fn as_str<'y: 'x>(&'y self) -> &'x str {
pub fn as_str(&self) -> &str {
match self {
HeaderName::Other(other) => other.as_ref(),
_ => self.as_static_str(),
Expand Down Expand Up @@ -542,7 +542,7 @@ impl<'x> MimeHeaders<'x> for Message<'x> {
.and_then(|header| header.as_text())
}

fn content_disposition(&self) -> Option<&ContentType> {
fn content_disposition(&self) -> Option<&ContentType<'x>> {
self.parts[0]
.headers
.header_value(&HeaderName::ContentDisposition)
Expand All @@ -563,14 +563,14 @@ impl<'x> MimeHeaders<'x> for Message<'x> {
.and_then(|header| header.as_text())
}

fn content_type(&self) -> Option<&ContentType> {
fn content_type(&self) -> Option<&ContentType<'x>> {
self.parts[0]
.headers
.header_value(&HeaderName::ContentType)
.and_then(|header| header.as_content_type())
}

fn content_language(&self) -> &HeaderValue {
fn content_language(&self) -> &HeaderValue<'x> {
self.parts[0]
.headers
.header_value(&HeaderName::ContentLanguage)
Expand Down Expand Up @@ -667,7 +667,7 @@ impl<'x> MessagePart<'x> {
}

/// Get the message headers
pub fn headers(&self) -> &[Header] {
pub fn headers(&self) -> &[Header<'x>] {
&self.headers
}

Expand Down Expand Up @@ -713,7 +713,7 @@ impl<'x> MessagePart<'x> {
}

impl<'x> fmt::Display for MessagePart<'x> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.write_str(self.text_contents().unwrap_or("[no contents]"))
}
}
Expand All @@ -725,7 +725,7 @@ impl<'x> MimeHeaders<'x> for MessagePart<'x> {
.and_then(|header| header.as_text())
}

fn content_disposition(&self) -> Option<&ContentType> {
fn content_disposition(&self) -> Option<&ContentType<'x>> {
self.headers
.header_value(&HeaderName::ContentDisposition)
.and_then(|header| header.as_content_type())
Expand All @@ -743,13 +743,13 @@ impl<'x> MimeHeaders<'x> for MessagePart<'x> {
.and_then(|header| header.as_text())
}

fn content_type(&self) -> Option<&ContentType> {
fn content_type(&self) -> Option<&ContentType<'x>> {
self.headers
.header_value(&HeaderName::ContentType)
.and_then(|header| header.as_content_type())
}

fn content_language(&self) -> &HeaderValue {
fn content_language(&self) -> &HeaderValue<'x> {
self.headers
.header_value(&HeaderName::ContentLanguage)
.unwrap_or(&HeaderValue::Empty)
Expand Down Expand Up @@ -786,7 +786,7 @@ impl<'x> ContentType<'x> {
}

/// Removes an attribute by name
pub fn remove_attribute(&mut self, name: &str) -> Option<Cow<str>> {
pub fn remove_attribute(&mut self, name: &str) -> Option<Cow<'x, str>> {
let attributes = self.attributes.as_mut()?;

attributes
Expand All @@ -796,15 +796,15 @@ impl<'x> ContentType<'x> {
}

/// Returns all attributes
pub fn attributes(&self) -> Option<&[(Cow<str>, Cow<str>)]> {
pub fn attributes(&self) -> Option<&[(Cow<'x, str>, Cow<'x, str>)]> {
self.attributes.as_deref()
}

/// Returns `true` when the provided attribute name is present
pub fn has_attribute(&self, name: &str) -> bool {
self.attributes
.as_ref()
.map_or_else(|| false, |attr| attr.iter().any(|(key, _)| key == name))
.map_or(false, |attr| attr.iter().any(|(key, _)| key == name))
}

/// Returns ```true``` if the Content-Disposition type is "attachment"
Expand Down Expand Up @@ -840,7 +840,7 @@ impl<'x> Received<'x> {
}

/// Returns the hostname or IP address of the machine that originated the message
pub fn from(&self) -> Option<&Host> {
pub fn from(&self) -> Option<&Host<'x>> {
self.from.as_ref()
}

Expand All @@ -855,7 +855,7 @@ impl<'x> Received<'x> {
}

/// Returns the hostname or IP address of the machine that received the message
pub fn by(&self) -> Option<&Host> {
pub fn by(&self) -> Option<&Host<'x>> {
self.by.as_ref()
}

Expand Down Expand Up @@ -890,7 +890,7 @@ impl<'x> Received<'x> {
}

/// Returns the EHLO/LHLO/HELO hostname or IP address of the machine that sent the message
pub fn helo(&self) -> Option<&Host> {
pub fn helo(&self) -> Option<&Host<'x>> {
self.helo.as_ref()
}

Expand Down Expand Up @@ -921,14 +921,14 @@ impl<'x> Host<'x> {
}

impl<'x> GetHeader<'x> for Vec<Header<'x>> {
fn header_value(&self, name: &HeaderName) -> Option<&HeaderValue<'x>> {
fn header_value(&self, name: &HeaderName<'_>) -> Option<&HeaderValue<'x>> {
self.iter()
.rev()
.find(|header| &header.name == name)
.map(|header| &header.value)
}

fn header(&self, name: impl Into<HeaderName<'x>>) -> Option<&Header> {
fn header(&self, name: impl Into<HeaderName<'x>>) -> Option<&Header<'x>> {
let name = name.into();
self.iter().rev().find(|header| header.name == name)
}
Expand All @@ -953,7 +953,7 @@ impl<'x> From<String> for HeaderName<'x> {
}

impl From<HeaderName<'_>> for String {
fn from(header: HeaderName) -> Self {
fn from(header: HeaderName<'_>) -> Self {
header.to_string()
}
}
Expand Down
Loading

0 comments on commit 0baffdd

Please sign in to comment.