From ac8fb89d56351fbc0017b8a7a8a4ddf53217ab60 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Fri, 5 Jan 2024 10:05:30 +0100 Subject: [PATCH] reformat cargo --- src/imap/attributes.rs | 4 +- src/imap/imf_view.rs | 1 - src/imap/mail_view.rs | 25 +++---- src/imap/mailbox_view.rs | 23 ++---- src/imap/mime_view.rs | 153 +++++++++++++++++++++----------------- src/imap/mod.rs | 8 +- src/imap/selectors.rs | 5 +- tests/behavior.rs | 24 ++++-- tests/common/fragments.rs | 6 +- 9 files changed, 129 insertions(+), 120 deletions(-) diff --git a/src/imap/attributes.rs b/src/imap/attributes.rs index 66b078e..7a55632 100644 --- a/src/imap/attributes.rs +++ b/src/imap/attributes.rs @@ -1,6 +1,4 @@ -use imap_codec::imap_types::fetch::{ - MacroOrMessageDataItemNames, MessageDataItemName, -}; +use imap_codec::imap_types::fetch::{MacroOrMessageDataItemNames, MessageDataItemName}; /// Internal decisions based on fetched attributes /// passed by the client diff --git a/src/imap/imf_view.rs b/src/imap/imf_view.rs index c18b05b..4297769 100644 --- a/src/imap/imf_view.rs +++ b/src/imap/imf_view.rs @@ -95,4 +95,3 @@ pub fn convert_mbx(addr: &imf::mailbox::MailboxRef) -> Address<'static> { )), } } - diff --git a/src/imap/mail_view.rs b/src/imap/mail_view.rs index d1f8a58..c95c733 100644 --- a/src/imap/mail_view.rs +++ b/src/imap/mail_view.rs @@ -3,7 +3,6 @@ use std::num::NonZeroU32; use anyhow::{anyhow, bail, Result}; use chrono::{Offset, TimeZone, Utc}; - use imap_codec::imap_types::core::{IString, NString}; use imap_codec::imap_types::datetime::DateTime; use imap_codec::imap_types::fetch::{ @@ -17,13 +16,13 @@ use eml_codec::{ part::{composite::Message, AnyPart}, }; -use crate::imap::response::Body; -use crate::imap::mime_view; -use crate::imap::flags; use crate::imap::attributes::AttributesProxy; -use crate::mail::mailbox::MailMeta; -use crate::imap::mailbox_view::MailIdentifiers; +use crate::imap::flags; use crate::imap::imf_view::message_envelope; +use crate::imap::mailbox_view::MailIdentifiers; +use crate::imap::mime_view; +use crate::imap::response::Body; +use crate::mail::mailbox::MailMeta; pub struct MailView<'a> { pub ids: &'a MailIdentifiers, @@ -121,10 +120,11 @@ impl<'a> MailView<'a> { } // Process message - let (text, origin) = match mime_view::body_ext(self.content.as_anypart()?, section, partial)? { - mime_view::BodySection::Full(body) => (body, None), - mime_view::BodySection::Slice { body, origin_octet } => (body, Some(origin_octet)), - }; + let (text, origin) = + match mime_view::body_ext(self.content.as_anypart()?, section, partial)? { + mime_view::BodySection::Full(body) => (body, None), + mime_view::BodySection::Slice { body, origin_octet } => (body, Some(origin_octet)), + }; let data = NString(text.to_vec().try_into().ok().map(IString::Literal)); @@ -185,14 +185,12 @@ impl<'a> MailView<'a> { } } - pub enum SeenFlag { DoNothing, MustAdd, } - -// ------------------- +// ------------------- pub enum FetchedMail<'a> { Partial(imf::Imf<'a>), @@ -229,4 +227,3 @@ impl<'a> FetchedMail<'a> { } } } - diff --git a/src/imap/mailbox_view.rs b/src/imap/mailbox_view.rs index 04253d0..f5cf394 100644 --- a/src/imap/mailbox_view.rs +++ b/src/imap/mailbox_view.rs @@ -5,18 +5,16 @@ use anyhow::{anyhow, bail, Error, Result}; use futures::stream::{FuturesOrdered, StreamExt}; -use imap_codec::imap_types::fetch::{ - MacroOrMessageDataItemNames, MessageDataItem -}; +use imap_codec::imap_types::fetch::{MacroOrMessageDataItemNames, MessageDataItem}; use imap_codec::imap_types::flag::{Flag, FlagFetch, FlagPerm, StoreResponse, StoreType}; use imap_codec::imap_types::response::{Code, Data, Status}; use imap_codec::imap_types::sequence::{self, SequenceSet}; -use crate::imap::flags; -use crate::imap::response::Body; use crate::imap::attributes::AttributesProxy; -use crate::imap::selectors::MailSelectionBuilder; +use crate::imap::flags; use crate::imap::mail_view::SeenFlag; +use crate::imap::response::Body; +use crate::imap::selectors::MailSelectionBuilder; use crate::mail::mailbox::Mailbox; use crate::mail::uidindex::{ImapUid, ImapUidvalidity, UidIndex}; use crate::mail::unique_ident::UniqueIdent; @@ -519,7 +517,6 @@ impl MailboxView { } } - pub struct MailIdentifiers { pub i: NonZeroU32, pub uid: ImapUid, @@ -536,22 +533,19 @@ impl MailIdentifiersList { #[cfg(test)] mod tests { use super::*; - use imap_codec::imap_types::fetch::{ - MacroOrMessageDataItemNames, MessageDataItemName, - }; use imap_codec::encode::Encoder; use imap_codec::imap_types::core::NonEmptyVec; use imap_codec::imap_types::fetch::Section; + use imap_codec::imap_types::fetch::{MacroOrMessageDataItemNames, MessageDataItemName}; use imap_codec::imap_types::response::Response; use imap_codec::ResponseCodec; use std::fs; - use crate::cryptoblob; - use crate::mail::unique_ident; - use crate::mail::mailbox::MailMeta; - use crate::imap::mail_view::{MailView, FetchedMail}; + use crate::imap::mail_view::{FetchedMail, MailView}; use crate::imap::mime_view; + use crate::mail::mailbox::MailMeta; + use crate::mail::unique_ident; #[test] fn mailview_body_ext() -> Result<()> { @@ -673,4 +667,3 @@ mod tests { Ok(()) } } - diff --git a/src/imap/mime_view.rs b/src/imap/mime_view.rs index b9e21f9..1f36c47 100644 --- a/src/imap/mime_view.rs +++ b/src/imap/mime_view.rs @@ -1,28 +1,23 @@ use std::borrow::Cow; -use std::num::NonZeroU32; use std::collections::HashSet; +use std::num::NonZeroU32; use anyhow::{anyhow, bail, Result}; use imap_codec::imap_types::body::{BasicFields, Body as FetchBody, BodyStructure, SpecificFields}; use imap_codec::imap_types::core::{AString, IString, NString, NonEmptyVec}; -use imap_codec::imap_types::fetch::{ - Section as FetchSection, Part as FetchPart -}; +use imap_codec::imap_types::fetch::{Part as FetchPart, Section as FetchSection}; use eml_codec::{ - header, mime, - mime::r#type::Deductible, - part::AnyPart, part::composite, part::discrete, + header, mime, mime::r#type::Deductible, part::composite, part::discrete, part::AnyPart, }; use crate::imap::imf_view::message_envelope; - pub enum BodySection<'a> { Full(Cow<'a, [u8]>), Slice { - body: Cow<'a, [u8]>, + body: Cow<'a, [u8]>, origin_octet: u32, }, } @@ -57,9 +52,9 @@ pub enum BodySection<'a> { /// 4.2.2.2 TEXT/RICHTEXT /// ``` pub fn body_ext<'a>( - part: &'a AnyPart<'a>, - section: &'a Option>, - partial: &'a Option<(u32, NonZeroU32)> + part: &'a AnyPart<'a>, + section: &'a Option>, + partial: &'a Option<(u32, NonZeroU32)>, ) -> Result> { let root_mime = NodeMime(part); let (extractor, path) = SubsettedSection::from(section); @@ -88,12 +83,12 @@ pub fn bodystructure(part: &AnyPart) -> Result> { } /// NodeMime -/// +/// /// Used for recursive logic on MIME. /// See SelectedMime for inspection. struct NodeMime<'a>(&'a AnyPart<'a>); impl<'a> NodeMime<'a> { - /// A MIME object is a tree of elements. + /// A MIME object is a tree of elements. /// The path indicates which element must be picked. /// This function returns the picked element as the new view fn subset(self, path: Option<&'a FetchPart>) -> Result> { @@ -138,7 +133,7 @@ impl<'a> NodeMime<'a> { /// A FetchSection must be handled in 2 times: /// - First we must extract the MIME part /// - Then we must process it as desired -/// The given struct mixes both work, so +/// The given struct mixes both work, so /// we separate this work here. enum SubsettedSection<'a> { Part, @@ -153,8 +148,12 @@ impl<'a> SubsettedSection<'a> { match section { Some(FetchSection::Text(maybe_part)) => (Self::Text, maybe_part.as_ref()), Some(FetchSection::Header(maybe_part)) => (Self::Header, maybe_part.as_ref()), - Some(FetchSection::HeaderFields(maybe_part, fields)) => (Self::HeaderFields(fields), maybe_part.as_ref()), - Some(FetchSection::HeaderFieldsNot(maybe_part, fields)) => (Self::HeaderFieldsNot(fields), maybe_part.as_ref()), + Some(FetchSection::HeaderFields(maybe_part, fields)) => { + (Self::HeaderFields(fields), maybe_part.as_ref()) + } + Some(FetchSection::HeaderFieldsNot(maybe_part, fields)) => { + (Self::HeaderFieldsNot(fields), maybe_part.as_ref()) + } Some(FetchSection::Mime(part)) => (Self::Mime, Some(part)), Some(FetchSection::Part(part)) => (Self::Part, Some(part)), None => (Self::Part, None), @@ -212,17 +211,24 @@ impl<'a> SelectedMime<'a> { /// returned by HEADER.FIELDS.NOT contains only the header fields /// with a non-matching field-name. The field-matching is /// case-insensitive but otherwise exact. - fn header_fields(&self, fields: &'a NonEmptyVec>, invert: bool) -> Result> { + fn header_fields( + &self, + fields: &'a NonEmptyVec>, + invert: bool, + ) -> Result> { // Build a lowercase ascii hashset with the fields to fetch let index = fields .as_ref() .iter() - .map(|x| match x { - AString::Atom(a) => a.inner().as_bytes(), - AString::String(IString::Literal(l)) => l.as_ref(), - AString::String(IString::Quoted(q)) => q.inner().as_bytes(), - }.to_ascii_lowercase()) - .collect::>(); + .map(|x| { + match x { + AString::Atom(a) => a.inner().as_bytes(), + AString::String(IString::Literal(l)) => l.as_ref(), + AString::String(IString::Quoted(q)) => q.inner().as_bytes(), + } + .to_ascii_lowercase() + }) + .collect::>(); // Extract MIME headers let mime = match &self.0 { @@ -234,7 +240,9 @@ impl<'a> SelectedMime<'a> { // 1. Keep only the correctly formatted headers // 2. Keep only based on the index presence or absence // 3. Reduce as a byte vector - let buffer = mime.kv.iter() + let buffer = mime + .kv + .iter() .filter_map(|field| match field { header::Field::Good(header::Kv2(k, v)) => Some((k, v)), _ => None, @@ -257,13 +265,19 @@ impl<'a> SelectedMime<'a> { /// HEADER ([RFC-2822] header of the message) /// ``` fn header(&self) -> Result> { - let msg = self.0.as_message().ok_or(anyhow!("Selected part must be a message/rfc822"))?; + let msg = self + .0 + .as_message() + .ok_or(anyhow!("Selected part must be a message/rfc822"))?; Ok(ExtractedFull(msg.raw_headers.into())) } /// The TEXT part specifier refers to the text body of the message, omitting the [RFC-2822] header. fn text(&self) -> Result> { - let msg = self.0.as_message().ok_or(anyhow!("Selected part must be a message/rfc822"))?; + let msg = self + .0 + .as_message() + .ok_or(anyhow!("Selected part must be a message/rfc822"))?; Ok(ExtractedFull(msg.raw_body.into())) } @@ -289,34 +303,36 @@ impl<'a> SelectedMime<'a> { ( IString::try_from(String::from_utf8_lossy(p.name).to_string()), IString::try_from(p.value.to_string()), - ) + ) }) - .filter(|(k, v)| k.is_ok() && v.is_ok()) + .filter(|(k, v)| k.is_ok() && v.is_ok()) .map(|(k, v)| (k.unwrap(), v.unwrap())) .collect() }) - .unwrap_or(vec![]); + .unwrap_or(vec![]); Ok(BasicFields { parameter_list, id: NString( m.id.as_ref() - .and_then(|ci| IString::try_from(ci.to_string()).ok()), - ), - description: NString( - m.description + .and_then(|ci| IString::try_from(ci.to_string()).ok()), + ), + description: NString( + m.description .as_ref() .and_then(|cd| IString::try_from(cd.to_string()).ok()), - ), - content_transfer_encoding: match m.transfer_encoding { - mime::mechanism::Mechanism::_8Bit => unchecked_istring("8bit"), - mime::mechanism::Mechanism::Binary => unchecked_istring("binary"), - mime::mechanism::Mechanism::QuotedPrintable => unchecked_istring("quoted-printable"), - mime::mechanism::Mechanism::Base64 => unchecked_istring("base64"), - _ => unchecked_istring("7bit"), - }, - // @FIXME we can't compute the size of the message currently... - size: u32::try_from(sz)?, + ), + content_transfer_encoding: match m.transfer_encoding { + mime::mechanism::Mechanism::_8Bit => unchecked_istring("8bit"), + mime::mechanism::Mechanism::Binary => unchecked_istring("binary"), + mime::mechanism::Mechanism::QuotedPrintable => { + unchecked_istring("quoted-printable") + } + mime::mechanism::Mechanism::Base64 => unchecked_istring("base64"), + _ => unchecked_istring("7bit"), + }, + // @FIXME we can't compute the size of the message currently... + size: u32::try_from(sz)?, }) } } @@ -325,7 +341,7 @@ impl<'a> SelectedMime<'a> { struct NodeMsg<'a>(&'a NodeMime<'a>, &'a composite::Message<'a>); impl<'a> NodeMsg<'a> { fn structure(&self) -> Result> { - let basic = SelectedMime(self.0.0).basic_fields()?; + let basic = SelectedMime(self.0 .0).basic_fields()?; Ok(BodyStructure::Single { body: FetchBody { @@ -347,7 +363,8 @@ impl<'a> NodeMult<'a> { let subtype = IString::try_from(itype.subtype.to_string()) .unwrap_or(unchecked_istring("alternative")); - let inner_bodies = self.1 + let inner_bodies = self + .1 .children .iter() .filter_map(|inner| NodeMime(&inner).structure().ok()) @@ -361,19 +378,19 @@ impl<'a> NodeMult<'a> { subtype, extension_data: None, /*Some(MultipartExtensionData { - parameter_list: vec![], - disposition: None, - language: None, - location: None, - extension: vec![], - })*/ + parameter_list: vec![], + disposition: None, + language: None, + location: None, + extension: vec![], + })*/ }) } } struct NodeTxt<'a>(&'a NodeMime<'a>, &'a discrete::Text<'a>); impl<'a> NodeTxt<'a> { fn structure(&self) -> Result> { - let mut basic = SelectedMime(self.0.0).basic_fields()?; + let mut basic = SelectedMime(self.0 .0).basic_fields()?; // Get the interpreted content type, set it let itype = match &self.1.mime.interpreted_type { @@ -407,7 +424,7 @@ impl<'a> NodeTxt<'a> { struct NodeBin<'a>(&'a NodeMime<'a>, &'a discrete::Binary<'a>); impl<'a> NodeBin<'a> { fn structure(&self) -> Result> { - let basic = SelectedMime(self.0.0).basic_fields()?; + let basic = SelectedMime(self.0 .0).basic_fields()?; let default = mime::r#type::NaiveType { main: &b"application"[..], @@ -416,15 +433,13 @@ impl<'a> NodeBin<'a> { }; let ct = self.1.mime.fields.ctype.as_ref().unwrap_or(&default); - let r#type = - IString::try_from(String::from_utf8_lossy(ct.main).to_string()).or(Err( - anyhow!("Unable to build IString from given Content-Type type given"), - ))?; + let r#type = IString::try_from(String::from_utf8_lossy(ct.main).to_string()).or(Err( + anyhow!("Unable to build IString from given Content-Type type given"), + ))?; - let subtype = - IString::try_from(String::from_utf8_lossy(ct.sub).to_string()).or(Err(anyhow!( - "Unable to build IString from given Content-Type subtype given" - )))?; + let subtype = IString::try_from(String::from_utf8_lossy(ct.sub).to_string()).or(Err( + anyhow!("Unable to build IString from given Content-Type subtype given"), + ))?; Ok(BodyStructure::Single { body: FetchBody { @@ -473,8 +488,8 @@ impl<'a> ExtractedFull<'a> { return BodySection::Slice { body: Cow::Borrowed(&[][..]), origin_octet: begin, - } - } + }; + } // Asked range is ending after the end of the content, // slice only the beginning of the buffer @@ -485,15 +500,19 @@ impl<'a> ExtractedFull<'a> { Cow::Owned(body) => Cow::Owned(body[begin as usize..].to_vec()), }, origin_octet: begin, - } + }; } // Range is included inside the considered content, // this is the "happy case" BodySection::Slice { body: match self.0 { - Cow::Borrowed(body) => Cow::Borrowed(&body[begin as usize..(begin + len.get()) as usize]), - Cow::Owned(body) => Cow::Owned(body[begin as usize..(begin + len.get()) as usize].to_vec()), + Cow::Borrowed(body) => { + Cow::Borrowed(&body[begin as usize..(begin + len.get()) as usize]) + } + Cow::Owned(body) => { + Cow::Owned(body[begin as usize..(begin + len.get()) as usize].to_vec()) + } }, origin_octet: begin, } diff --git a/src/imap/mod.rs b/src/imap/mod.rs index d3cf966..4f33bfe 100644 --- a/src/imap/mod.rs +++ b/src/imap/mod.rs @@ -1,14 +1,14 @@ +mod attributes; mod capability; mod command; +mod flags; mod flow; -mod attributes; +mod imf_view; mod mail_view; mod mailbox_view; mod mime_view; -mod imf_view; -mod selectors; -mod flags; mod response; +mod selectors; mod session; use std::net::SocketAddr; diff --git a/src/imap/selectors.rs b/src/imap/selectors.rs index 6b5372a..09320c3 100644 --- a/src/imap/selectors.rs +++ b/src/imap/selectors.rs @@ -2,9 +2,8 @@ use std::iter::zip; use anyhow::{anyhow, Result}; - use crate::cryptoblob::Key; -use crate::imap::mail_view::{MailView, FetchedMail}; +use crate::imap::mail_view::{FetchedMail, MailView}; use crate::imap::mailbox_view::MailIdentifiers; use crate::mail::mailbox::MailMeta; use crate::mail::unique_ident::UniqueIdent; @@ -14,7 +13,6 @@ pub struct BodyIdentifier<'a> { pub msg_key: &'a Key, } - #[derive(Default)] pub struct MailSelectionBuilder<'a> { //attrs: AttributeProxy, @@ -100,4 +98,3 @@ impl<'a> MailSelectionBuilder<'a> { .collect()) } } - diff --git a/tests/behavior.rs b/tests/behavior.rs index 82fdc53..17f3a72 100644 --- a/tests/behavior.rs +++ b/tests/behavior.rs @@ -26,15 +26,18 @@ fn rfc3501_imap4rev1_base() { lmtp_handshake(lmtp_socket).context("handshake lmtp done")?; lmtp_deliver_email(lmtp_socket, Email::Multipart).context("mail delivered successfully")?; noop_exists(imap_socket).context("noop loop must detect a new email")?; - fetch_rfc822(imap_socket, Selection::FirstId, Email::Multipart).context("fetch rfc822 message, should be our first message")?; - copy(imap_socket, Selection::FirstId, Mailbox::Archive).context("copy message to the archive mailbox")?; + fetch_rfc822(imap_socket, Selection::FirstId, Email::Multipart) + .context("fetch rfc822 message, should be our first message")?; + copy(imap_socket, Selection::FirstId, Mailbox::Archive) + .context("copy message to the archive mailbox")?; append_email(imap_socket, Email::Basic).context("insert email in INBOX")?; // SEARCH IS NOT IMPLEMENTED YET //search(imap_socket).expect("search should return something"); add_flags_email(imap_socket, Selection::FirstId, Flag::Deleted) .context("should add delete flag to the email")?; expunge(imap_socket).context("expunge emails")?; - rename_mailbox(imap_socket, Mailbox::Archive, Mailbox::Drafts).context("Archive mailbox is renamed Drafts")?; + rename_mailbox(imap_socket, Mailbox::Archive, Mailbox::Drafts) + .context("Archive mailbox is renamed Drafts")?; delete_mailbox(imap_socket, Mailbox::Drafts).context("Drafts mailbox is deleted")?; Ok(()) }) @@ -53,13 +56,16 @@ fn rfc3691_imapext_unselect() { login(imap_socket, Account::Alice).context("login test")?; select(imap_socket, Mailbox::Inbox, None).context("select inbox")?; noop_exists(imap_socket).context("noop loop must detect a new email")?; - add_flags_email(imap_socket, Selection::FirstId, Flag::Deleted).context("add delete flags to the email")?; + add_flags_email(imap_socket, Selection::FirstId, Flag::Deleted) + .context("add delete flags to the email")?; unselect(imap_socket) .context("unselect inbox while preserving email with the \\Delete flag")?; select(imap_socket, Mailbox::Inbox, Some(1)).context("select inbox again")?; - fetch_rfc822(imap_socket, Selection::FirstId, Email::Basic).context("message is still present")?; + fetch_rfc822(imap_socket, Selection::FirstId, Email::Basic) + .context("message is still present")?; close(imap_socket).context("close inbox and expunge message")?; - select(imap_socket, Mailbox::Inbox, Some(0)).context("select inbox again and check it's empty")?; + select(imap_socket, Mailbox::Inbox, Some(0)) + .context("select inbox again and check it's empty")?; Ok(()) }) @@ -94,7 +100,8 @@ fn rfc6851_imapext_move() { lmtp_deliver_email(lmtp_socket, Email::Basic).context("mail delivered successfully")?; noop_exists(imap_socket).context("noop loop must detect a new email")?; - r#move(imap_socket, Selection::FirstId, Mailbox::Archive).context("message from inbox moved to archive")?; + r#move(imap_socket, Selection::FirstId, Mailbox::Archive) + .context("message from inbox moved to archive")?; unselect(imap_socket) .context("unselect inbox while preserving email with the \\Delete flag")?; @@ -116,5 +123,6 @@ fn rfc7888_imapext_literal() { login_with_literal(imap_socket, Account::Alice).context("use literal to connect Alice")?; Ok(()) - }).expect("test fully run"); + }) + .expect("test fully run"); } diff --git a/tests/common/fragments.rs b/tests/common/fragments.rs index 3ed14cc..2e2fbd4 100644 --- a/tests/common/fragments.rs +++ b/tests/common/fragments.rs @@ -52,7 +52,7 @@ pub enum Mailbox { pub enum Flag { Deleted, - Important + Important, } pub enum Email { @@ -287,8 +287,6 @@ pub fn append_email(imap: &mut TcpStream, content: Email) -> Result<()> { Ok(()) } - - pub fn add_flags_email(imap: &mut TcpStream, selection: Selection, flag: Flag) -> Result<()> { let mut buffer: [u8; 1500] = [0; 1500]; assert!(matches!(selection, Selection::FirstId)); @@ -390,7 +388,7 @@ pub fn enable(imap: &mut TcpStream, ask: Enable, done: Option) -> Result Some(Enable::Utf8Accept) => { assert_eq!(srv_msg.lines().count(), 2); assert!(srv_msg.contains("* ENABLED UTF8=ACCEPT")); - }, + } _ => unimplemented!(), }