diff --git a/src/fragments/address.rs b/src/fragments/address.rs index 16515b8..75d2fca 100644 --- a/src/fragments/address.rs +++ b/src/fragments/address.rs @@ -14,40 +14,40 @@ use crate::fragments::misc_token::phrase; use crate::fragments::whitespace::{cfws}; use crate::error::IMFError; -impl<'a> TryFrom<&'a lazy::Mailbox<'a>> for MailboxRef { +impl<'a> TryFrom> for MailboxRef { type Error = IMFError<'a>; - fn try_from(mx: &'a lazy::Mailbox<'a>) -> Result { + fn try_from(mx: lazy::Mailbox<'a>) -> Result { mailbox(mx.0) .map(|(_, m)| m) .map_err(|e| IMFError::Mailbox(e)) } } -impl<'a> TryFrom<&'a lazy::MailboxList<'a>> for MailboxList { +impl<'a> TryFrom> for MailboxList { type Error = IMFError<'a>; - fn try_from(ml: &'a lazy::MailboxList<'a>) -> Result { + fn try_from(ml: lazy::MailboxList<'a>) -> Result { mailbox_list(ml.0) .map(|(_, m)| m) .map_err(|e| IMFError::MailboxList(e)) } } -impl<'a> TryFrom<&'a lazy::AddressList<'a>> for AddressList { +impl<'a> TryFrom> for AddressList { type Error = IMFError<'a>; - fn try_from(al: &'a lazy::AddressList<'a>) -> Result { + fn try_from(al: lazy::AddressList<'a>) -> Result { address_list(al.0) .map(|(_, a)| a) .map_err(|e| IMFError::AddressList(e)) } } -impl<'a> TryFrom<&'a lazy::NullableAddressList<'a>> for AddressList { +impl<'a> TryFrom> for AddressList { type Error = IMFError<'a>; - fn try_from(nal: &'a lazy::NullableAddressList<'a>) -> Result { + fn try_from(nal: lazy::NullableAddressList<'a>) -> Result { opt(alt((address_list, address_list_cfws)))(nal.0) .map(|(_, a)| a.unwrap_or(vec![])) .map_err(|e| IMFError::NullableAddressList(e)) diff --git a/src/fragments/datetime.rs b/src/fragments/datetime.rs index 34d68e4..7af6589 100644 --- a/src/fragments/datetime.rs +++ b/src/fragments/datetime.rs @@ -18,10 +18,10 @@ use crate::error::IMFError; const MIN: i32 = 60; const HOUR: i32 = 60 * MIN; -impl<'a> TryFrom<&'a lazy::DateTime<'a>> for DateTime { +impl<'a> TryFrom> for DateTime { type Error = IMFError<'a>; - fn try_from(value: &'a lazy::DateTime<'a>) -> Result { + fn try_from(value: lazy::DateTime<'a>) -> Result { match section(value.0) { Ok((_, Some(dt))) => Ok(dt), Err(e) => Err(IMFError::DateTimeParse(e)), diff --git a/src/fragments/eager.rs b/src/fragments/eager.rs index def4ab9..513990e 100644 --- a/src/fragments/eager.rs +++ b/src/fragments/eager.rs @@ -45,10 +45,10 @@ pub enum Field<'a> { } use Field::*; -impl<'a> TryFrom<&'a Lazy<'a>> for Field<'a> { +impl<'a> TryFrom> for Field<'a> { type Error = IMFError<'a>; - fn try_from(l: &'a Lazy<'a>) -> Result { + fn try_from(l: Lazy<'a>) -> Result { match l { Lazy::Date(v) => v.try_into().map(|v| Date(v)), Lazy::From(v) => v.try_into().map(|v| From(v)), @@ -66,7 +66,7 @@ impl<'a> TryFrom<&'a Lazy<'a>> for Field<'a> { Lazy::Received(v) => v.try_into().map(|v| Received(v)), Lazy::ReturnPath(v) => v.try_into().map(|v| ReturnPath(v)), Lazy::Optional(k, v) => v.try_into().map(|v| Optional(k, v)), - Lazy::Rescue(v) => Ok(Rescue(*v)), + Lazy::Rescue(v) => Ok(Rescue(v)), } } } diff --git a/src/fragments/identification.rs b/src/fragments/identification.rs index 42fa843..f8d51c6 100644 --- a/src/fragments/identification.rs +++ b/src/fragments/identification.rs @@ -15,20 +15,20 @@ use crate::fragments::mailbox::is_dtext; use crate::fragments::model::{MessageId, MessageIdList}; use crate::error::IMFError; -impl<'a> TryFrom<&'a lazy::Identifier<'a>> for MessageId<'a> { +impl<'a> TryFrom> for MessageId<'a> { type Error = IMFError<'a>; - fn try_from(id: &'a lazy::Identifier<'a>) -> Result { + fn try_from(id: lazy::Identifier<'a>) -> Result { msg_id(id.0) .map(|(_, i)| i) .map_err(|e| IMFError::MessageID(e)) } } -impl<'a> TryFrom<&'a lazy::IdentifierList<'a>> for MessageIdList<'a> { +impl<'a> TryFrom> for MessageIdList<'a> { type Error = IMFError<'a>; - fn try_from(id: &'a lazy::IdentifierList<'a>) -> Result { + fn try_from(id: lazy::IdentifierList<'a>) -> Result { many1(msg_id)(id.0) .map(|(_, i)| i) .map_err(|e| IMFError::MessageIDList(e)) diff --git a/src/fragments/misc_token.rs b/src/fragments/misc_token.rs index 44ddd89..a366c40 100644 --- a/src/fragments/misc_token.rs +++ b/src/fragments/misc_token.rs @@ -21,20 +21,20 @@ pub struct Unstructured(pub String); #[derive(Debug, PartialEq, Default)] pub struct PhraseList(pub Vec); -impl<'a> TryFrom<&'a lazy::Unstructured<'a>> for Unstructured { +impl<'a> TryFrom> for Unstructured { type Error = IMFError<'a>; - fn try_from(input: &'a lazy::Unstructured<'a>) -> Result { + fn try_from(input: lazy::Unstructured<'a>) -> Result { unstructured(input.0) .map(|(_, v)| Unstructured(v)) .map_err(|e| IMFError::Unstructured(e)) } } -impl<'a> TryFrom<&'a lazy::PhraseList<'a>> for PhraseList { +impl<'a> TryFrom> for PhraseList { type Error = IMFError<'a>; - fn try_from(p: &'a lazy::PhraseList<'a>) -> Result { + fn try_from(p: lazy::PhraseList<'a>) -> Result { separated_list1(tag(","), phrase)(p.0) .map(|(_, q)| PhraseList(q)) .map_err(|e| IMFError::PhraseList(e)) diff --git a/src/fragments/section.rs b/src/fragments/section.rs index b80893f..7f241a8 100644 --- a/src/fragments/section.rs +++ b/src/fragments/section.rs @@ -49,33 +49,31 @@ pub struct Section<'a> { //@FIXME min and max limits are not enforced, // it may result in missing data or silently overriden data. -impl<'a> From>> for Section<'a> { - fn from(field_list: Vec>) -> Self { - field_list.into_iter().fold( - Section::default(), - |mut section, field| { - match field { - Field::Date(v) => section.date = Some(v), - Field::From(v) => section.from.extend(v), - Field::Sender(v) => section.sender = Some(v), - Field::ReplyTo(v) => section.reply_to.extend(v), - Field::To(v) => section.to.extend(v), - Field::Cc(v) => section.cc.extend(v), - Field::Bcc(v) => section.bcc.extend(v), - Field::MessageID(v) => section.msg_id = Some(v), - Field::InReplyTo(v) => section.in_reply_to.extend(v), - Field::References(v) => section.references.extend(v), - Field::Subject(v) => section.subject = Some(v), - Field::Comments(v) => section.comments.push(v), - Field::Keywords(v) => section.keywords.push(v), - Field::ReturnPath(v) => section.return_path.push(v), - Field::Received(v) => section.received.push(v), - Field::Optional(k, v) => { section.optional.insert(k, v); }, - Field::Rescue(v) => section.unparsed.push(v), - }; - section +impl<'a> FromIterator> for Section<'a> { + fn from_iter>>(iter: I) -> Self { + let mut section = Section::default(); + for field in iter { + match field { + Field::Date(v) => section.date = Some(v), + Field::From(v) => section.from.extend(v), + Field::Sender(v) => section.sender = Some(v), + Field::ReplyTo(v) => section.reply_to.extend(v), + Field::To(v) => section.to.extend(v), + Field::Cc(v) => section.cc.extend(v), + Field::Bcc(v) => section.bcc.extend(v), + Field::MessageID(v) => section.msg_id = Some(v), + Field::InReplyTo(v) => section.in_reply_to.extend(v), + Field::References(v) => section.references.extend(v), + Field::Subject(v) => section.subject = Some(v), + Field::Comments(v) => section.comments.push(v), + Field::Keywords(v) => section.keywords.push(v), + Field::ReturnPath(v) => section.return_path.push(v), + Field::Received(v) => section.received.push(v), + Field::Optional(k, v) => { section.optional.insert(k, v); }, + Field::Rescue(v) => section.unparsed.push(v), } - ) + } + section } } diff --git a/src/fragments/trace.rs b/src/fragments/trace.rs index d15dfd9..239da67 100644 --- a/src/fragments/trace.rs +++ b/src/fragments/trace.rs @@ -14,10 +14,10 @@ use crate::error::IMFError; #[derive(Debug, PartialEq)] pub struct ReceivedLog<'a>(pub &'a str); -impl<'a> TryFrom<&'a lazy::ReceivedLog<'a>> for ReceivedLog<'a> { +impl<'a> TryFrom> for ReceivedLog<'a> { type Error = IMFError<'a>; - fn try_from(input: &'a lazy::ReceivedLog<'a>) -> Result { + fn try_from(input: lazy::ReceivedLog<'a>) -> Result { received_body(input.0) .map_err(|e| IMFError::ReceivedLog(e)) .map(|(_, v)| ReceivedLog(v)) diff --git a/src/multipass/field_eager.rs b/src/multipass/field_eager.rs index 77d1220..6cc8a3f 100644 --- a/src/multipass/field_eager.rs +++ b/src/multipass/field_eager.rs @@ -7,10 +7,10 @@ pub struct Parsed<'a> { pub body: &'a [u8], } -impl<'a> From <&'a field_lazy::Parsed<'a>> for Parsed<'a> { - fn from(p: &'a field_lazy::Parsed<'a>) -> Self { +impl<'a> From > for Parsed<'a> { + fn from(p: field_lazy::Parsed<'a>) -> Self { Parsed { - fields: p.fields.iter().filter_map(|entry| entry.try_into().ok()).collect(), + fields: p.fields.into_iter().filter_map(|entry| entry.try_into().ok()).collect(), body: p.body, } } @@ -25,7 +25,7 @@ mod tests { #[test] fn test_field_body() { - assert_eq!(Parsed::from(&field_lazy::Parsed { + assert_eq!(Parsed::from(field_lazy::Parsed { fields: vec![ lazy::Field::From(lazy::MailboxList("hello@world.com,\r\n\talice@wonderlands.com\r\n")), lazy::Field::Date(lazy::DateTime("12 Mar 1997 07:33:25 Z\r\n")), diff --git a/src/multipass/field_lazy.rs b/src/multipass/field_lazy.rs index d0ac6ab..ba58ddf 100644 --- a/src/multipass/field_lazy.rs +++ b/src/multipass/field_lazy.rs @@ -7,8 +7,8 @@ pub struct Parsed<'a> { pub body: &'a [u8], } -impl<'a> From <&'a ExtractFields<'a>> for Parsed<'a> { - fn from(ef: &'a ExtractFields<'a>) -> Self { +impl<'a> From > for Parsed<'a> { + fn from(ef: ExtractFields<'a>) -> Self { Parsed { fields: ef.fields.iter().map(|e| (*e).into()).collect(), body: ef.body, @@ -22,7 +22,7 @@ mod tests { #[test] fn test_field_name() { - assert_eq!(Parsed::from(&ExtractFields { + assert_eq!(Parsed::from(ExtractFields { fields: vec![ "From: hello@world.com,\r\n\talice@wonderlands.com\r\n", "Date: 12 Mar 1997 07:33:25 Z\r\n", diff --git a/src/multipass/guess_charset.rs b/src/multipass/guess_charset.rs index aa63324..4d1c237 100644 --- a/src/multipass/guess_charset.rs +++ b/src/multipass/guess_charset.rs @@ -16,8 +16,8 @@ const IS_LAST_BUFFER: bool = true; const ALLOW_UTF8: bool = true; const NO_TLD: Option<&[u8]> = None; -impl<'a> From<&'a Segment<'a>> for GuessCharset<'a> { - fn from(seg: &'a Segment<'a>) -> Self { +impl<'a> From> for GuessCharset<'a> { + fn from(seg: Segment<'a>) -> Self { // Create detector let mut detector = EncodingDetector::new(); detector.feed(&seg.header, IS_LAST_BUFFER); @@ -37,7 +37,7 @@ mod tests { #[test] fn test_charset() { assert_eq!( - GuessCharset::from(&Segment { + GuessCharset::from(Segment { body: b"Hello world!", header: b"From: hello@world.com\r\nDate: 12 Mar 1997 07:33:25 Z\r\n", }), diff --git a/src/multipass/header_section.rs b/src/multipass/header_section.rs index 93bdafe..e76224d 100644 --- a/src/multipass/header_section.rs +++ b/src/multipass/header_section.rs @@ -10,7 +10,7 @@ pub struct Parsed<'a> { impl<'a> From> for Parsed<'a> { fn from(p: field_eager::Parsed<'a>) -> Self { Parsed { - fields: p.fields.into(), + fields: Section::from_iter(p.fields.into_iter()), body: p.body, } }