diff --git a/src/types/mod.rs b/src/types/mod.rs index b1b1d2a..d175f43 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -228,7 +228,7 @@ mod deleted; pub use self::deleted::Deleted; mod unsolicited_response; -pub use self::unsolicited_response::{AttributeValue, ResponseCode, UnsolicitedResponse}; +pub use self::unsolicited_response::{AttributeValue, UnsolicitedResponse}; /// This type wraps an input stream and a type that was constructed by parsing that input stream, /// which allows the parsed type to refer to data in the underlying stream instead of copying it. diff --git a/src/types/unsolicited_response.rs b/src/types/unsolicited_response.rs index 80aa46a..214bb51 100644 --- a/src/types/unsolicited_response.rs +++ b/src/types/unsolicited_response.rs @@ -4,11 +4,9 @@ use super::{Flag, Seq, Uid}; use crate::error::ParseError; /// re-exported from imap_proto; +pub use imap_proto::ResponseCode; pub use imap_proto::StatusAttribute; -use imap_proto::{ - AttributeValue as ImapProtoAttributeValue, MailboxDatum, Response, - ResponseCode as ImapProtoResponseCode, Status, -}; +use imap_proto::{AttributeValue as ImapProtoAttributeValue, MailboxDatum, Response, Status}; /// Responses that the server sends that are not related to the current command. /// [RFC 3501](https://tools.ietf.org/html/rfc3501#section-7) states that clients need to be able @@ -30,7 +28,7 @@ pub enum UnsolicitedResponse { /// information, per [RFC3501](https://tools.ietf.org/html/rfc3501#section-7.1.5). Bye { /// Optional response code. - code: Option, + code: Option>, /// Information text that may be presented to the user. information: Option, }, @@ -94,7 +92,7 @@ pub enum UnsolicitedResponse { /// information, per [RFC3501](https://tools.ietf.org/html/rfc3501#section-7.1.1). Ok { /// Optional response code. - code: Option, + code: Option>, /// Information text that may be presented to the user. information: Option, }, @@ -188,46 +186,20 @@ impl<'a> TryFrom> for UnsolicitedResponse { } Response::Data { status: Status::Ok, - ref code, - ref information, - } => { - let info = information.as_ref().map(|s| s.to_string()); - if let Some(code) = code { - match ResponseCode::try_from(code) { - Ok(owncode) => Ok(UnsolicitedResponse::Ok { - code: Some(owncode), - information: info, - }), - _ => Err(response), - } - } else { - Ok(UnsolicitedResponse::Ok { - code: None, - information: info, - }) - } - } + code, + information, + } => Ok(UnsolicitedResponse::Ok { + code: code.map(|c| c.into_owned()), + information: information.map(|s| s.to_string()), + }), Response::Data { status: Status::Bye, - ref code, - ref information, - } => { - let info = information.as_ref().map(|s| s.to_string()); - if let Some(code) = code { - match ResponseCode::try_from(code) { - Ok(owncode) => Ok(UnsolicitedResponse::Bye { - code: Some(owncode), - information: info, - }), - _ => Err(response), - } - } else { - Ok(UnsolicitedResponse::Bye { - code: None, - information: info, - }) - } - } + code, + information, + } => Ok(UnsolicitedResponse::Bye { + code: code.map(|c| c.into_owned()), + information: information.map(|s| s.to_string()), + }), Response::Fetch(id, ref attributes) => { match AttributeValue::try_from_imap_proto_vec(attributes) { Ok(attrs) => Ok(UnsolicitedResponse::Fetch { @@ -242,34 +214,6 @@ impl<'a> TryFrom> for UnsolicitedResponse { } } -/// Owned version of ResponseCode that wraps a subset of [`imap_proto::ResponseCode`] -#[derive(Debug, Eq, PartialEq)] -#[non_exhaustive] -pub enum ResponseCode { - /// Highest ModSeq in the mailbox, [RFC4551](https://tools.ietf.org/html/rfc4551#section-3.1.1) - HighestModSeq(u64), - /// Next UID in the mailbox, [RFC3501](https://tools.ietf.org/html/rfc3501#section-2.3.1.1) - UidNext(Uid), - /// Mailbox UIDVALIDITY, [RFC3501](https://tools.ietf.org/html/rfc3501#section-2.3.1.1) - UidValidity(u32), - /// Sequence number of first message without the `\\Seen` flag - Unseen(Seq), -} - -impl<'a> TryFrom<&ImapProtoResponseCode<'a>> for ResponseCode { - type Error = ParseError; - - fn try_from(val: &ImapProtoResponseCode<'a>) -> Result { - match val { - ImapProtoResponseCode::HighestModSeq(seq) => Ok(ResponseCode::HighestModSeq(*seq)), - ImapProtoResponseCode::UidNext(uid) => Ok(ResponseCode::UidNext(*uid)), - ImapProtoResponseCode::UidValidity(uid) => Ok(ResponseCode::UidValidity(*uid)), - ImapProtoResponseCode::Unseen(seq) => Ok(ResponseCode::Unseen(*seq)), - unhandled => Err(ParseError::Unexpected(format!("{:?}", unhandled))), - } - } -} - /// Owned version of AttributeValue that wraps a subset of [`imap_proto::AttributeValue`]. #[derive(Debug, Eq, PartialEq)] #[non_exhaustive]