Add helper function to transform a vec of flag strings into a vec of Flags.

Also use it where we were previously iterating manually.
This commit is contained in:
Todd Mortimer 2021-04-05 15:59:12 -04:00
parent b8bd1e4cc7
commit e1db863691
3 changed files with 12 additions and 17 deletions

View file

@ -109,9 +109,7 @@ pub fn parse_fetches(
use imap_proto::AttributeValue; use imap_proto::AttributeValue;
match attr { match attr {
AttributeValue::Flags(flags) => { AttributeValue::Flags(flags) => {
fetch fetch.flags.extend(Flag::from_vec(flags));
.flags
.extend(flags.iter().map(|f| Flag::from(f.to_string())));
} }
AttributeValue::Uid(uid) => fetch.uid = Some(*uid), AttributeValue::Uid(uid) => fetch.uid = Some(*uid),
AttributeValue::Rfc822Size(sz) => fetch.size = Some(*sz), AttributeValue::Rfc822Size(sz) => fetch.size = Some(*sz),
@ -271,9 +269,7 @@ pub fn parse_mailbox(
mailbox.unseen = Some(n); mailbox.unseen = Some(n);
} }
Some(ResponseCode::PermanentFlags(flags)) => { Some(ResponseCode::PermanentFlags(flags)) => {
mailbox mailbox.permanent_flags.extend(Flag::from_vec(&flags));
.permanent_flags
.extend(flags.into_iter().map(String::from).map(Flag::from));
} }
_ => {} _ => {}
} }
@ -297,9 +293,7 @@ pub fn parse_mailbox(
mailbox.recent = r; mailbox.recent = r;
} }
MailboxDatum::Flags(flags) => { MailboxDatum::Flags(flags) => {
mailbox mailbox.flags.extend(Flag::from_vec(&flags));
.flags
.extend(flags.into_iter().map(String::from).map(Flag::from));
} }
_ => {} _ => {}
} }

View file

@ -168,6 +168,11 @@ impl Flag<'static> {
_ => None, _ => None,
} }
} }
/// Helper function to transform a [`Vec`] of flag strings into a [`Vec`] of [`Flag`].
pub fn from_vec<S: ToString>(v: &[S]) -> Vec<Self> {
v.iter().map(|s| Self::from(s.to_string())).collect()
}
} }
impl<'a> fmt::Display for Flag<'a> { impl<'a> fmt::Display for Flag<'a> {

View file

@ -167,12 +167,9 @@ impl<'a> TryFrom<Response<'a>> for UnsolicitedResponse {
}) })
} }
Response::MailboxData(MailboxDatum::Recent(n)) => Ok(UnsolicitedResponse::Recent(n)), Response::MailboxData(MailboxDatum::Recent(n)) => Ok(UnsolicitedResponse::Recent(n)),
Response::MailboxData(MailboxDatum::Flags(flags)) => Ok(UnsolicitedResponse::Flags( Response::MailboxData(MailboxDatum::Flags(flags)) => {
flags Ok(UnsolicitedResponse::Flags(Flag::from_vec(&flags)))
.into_iter() }
.map(|s| Flag::from(s.to_string()))
.collect(),
)),
Response::MailboxData(MailboxDatum::Exists(n)) => Ok(UnsolicitedResponse::Exists(n)), Response::MailboxData(MailboxDatum::Exists(n)) => Ok(UnsolicitedResponse::Exists(n)),
Response::MailboxData(MailboxDatum::MetadataUnsolicited { mailbox, values }) => { Response::MailboxData(MailboxDatum::MetadataUnsolicited { mailbox, values }) => {
Ok(UnsolicitedResponse::Metadata { Ok(UnsolicitedResponse::Metadata {
@ -286,8 +283,7 @@ impl<'a> TryFrom<&ImapProtoAttributeValue<'a>> for AttributeValue {
fn try_from(val: &ImapProtoAttributeValue<'a>) -> Result<Self, Self::Error> { fn try_from(val: &ImapProtoAttributeValue<'a>) -> Result<Self, Self::Error> {
match val { match val {
ImapProtoAttributeValue::Flags(flags) => { ImapProtoAttributeValue::Flags(flags) => {
let v = flags.iter().map(|v| Flag::from(v.to_string())).collect(); Ok(AttributeValue::Flags(Flag::from_vec(&flags)))
Ok(AttributeValue::Flags(v))
} }
ImapProtoAttributeValue::ModSeq(seq) => Ok(AttributeValue::ModSeq(*seq)), ImapProtoAttributeValue::ModSeq(seq) => Ok(AttributeValue::ModSeq(*seq)),
ImapProtoAttributeValue::Uid(uid) => Ok(AttributeValue::Uid(*uid)), ImapProtoAttributeValue::Uid(uid) => Ok(AttributeValue::Uid(*uid)),