adjust parse_until_done to return an Option so it is more versatile

This commit is contained in:
Edward Rudd 2022-09-26 14:21:34 -04:00
parent 1da3eb5571
commit 9c08e14523
2 changed files with 18 additions and 6 deletions

View file

@ -58,12 +58,14 @@ where
/// Parse and return an expected single `T` Response with `F`. /// Parse and return an expected single `T` Response with `F`.
/// Responses other than `T` go into the `unsolicited` channel. /// Responses other than `T` go into the `unsolicited` channel.
/// ///
/// If zero or more than one `T` is found then [`Error::Parse`] is returned /// If more than one `T` are found then [`Error::Parse`] is returned
/// If zero `T` are found and optional is false then [`Error::Parse`] is returned, otherwise None is
pub(crate) fn parse_until_done<'input, T, F>( pub(crate) fn parse_until_done<'input, T, F>(
input: &'input [u8], input: &'input [u8],
optional: bool,
unsolicited: &mut mpsc::Sender<UnsolicitedResponse>, unsolicited: &mut mpsc::Sender<UnsolicitedResponse>,
map: F, map: F,
) -> Result<T> ) -> Result<Option<T>>
where where
F: FnMut(Response<'input>) -> Result<MapOrNot<'input, T>>, F: FnMut(Response<'input>) -> Result<MapOrNot<'input, T>>,
{ {
@ -72,7 +74,14 @@ where
parse_many_into(input, &mut temp_output, unsolicited, map)?; parse_many_into(input, &mut temp_output, unsolicited, map)?;
match temp_output.len() { match temp_output.len() {
1 => Ok(temp_output.remove(0)), 1 => Ok(Some(temp_output.remove(0))),
0 => {
if optional {
Ok(None)
} else {
Err(Error::Parse(ParseError::Invalid(input.to_vec())))
}
}
_ => Err(Error::Parse(ParseError::Invalid(input.to_vec()))), _ => Err(Error::Parse(ParseError::Invalid(input.to_vec()))),
} }
} }

View file

@ -123,7 +123,7 @@ impl AclResponse {
data: owned, data: owned,
acl_builder: |input| { acl_builder: |input| {
// There should only be ONE single ACL response // There should only be ONE single ACL response
parse_until_done(input, unsolicited, |response| match response { parse_until_done(input, false, unsolicited, |response| match response {
Response::Acl(a) => Ok(MapOrNot::Map(Acl { Response::Acl(a) => Ok(MapOrNot::Map(Acl {
mailbox: a.mailbox, mailbox: a.mailbox,
acls: a acls: a
@ -137,6 +137,7 @@ impl AclResponse {
})), })),
resp => Ok(MapOrNot::Not(resp)), resp => Ok(MapOrNot::Not(resp)),
}) })
.map(|o| o.unwrap())
}, },
} }
.try_build() .try_build()
@ -206,7 +207,7 @@ impl ListRightsResponse {
data: owned, data: owned,
rights_builder: |input| { rights_builder: |input| {
// There should only be ONE single LISTRIGHTS response // There should only be ONE single LISTRIGHTS response
parse_until_done(input, unsolicited, |response| match response { parse_until_done(input, false, unsolicited, |response| match response {
Response::ListRights(a) => Ok(MapOrNot::Map(ListRights { Response::ListRights(a) => Ok(MapOrNot::Map(ListRights {
mailbox: a.mailbox, mailbox: a.mailbox,
identifier: a.identifier, identifier: a.identifier,
@ -215,6 +216,7 @@ impl ListRightsResponse {
})), })),
resp => Ok(MapOrNot::Not(resp)), resp => Ok(MapOrNot::Not(resp)),
}) })
.map(|o| o.unwrap())
}, },
} }
.try_build() .try_build()
@ -286,13 +288,14 @@ impl MyRightsResponse {
data: owned, data: owned,
rights_builder: |input| { rights_builder: |input| {
// There should only be ONE single MYRIGHTS response // There should only be ONE single MYRIGHTS response
parse_until_done(input, unsolicited, |response| match response { parse_until_done(input, false, unsolicited, |response| match response {
Response::MyRights(a) => Ok(MapOrNot::Map(MyRights { Response::MyRights(a) => Ok(MapOrNot::Map(MyRights {
mailbox: a.mailbox, mailbox: a.mailbox,
rights: a.rights.into(), rights: a.rights.into(),
})), })),
resp => Ok(MapOrNot::Not(resp)), resp => Ok(MapOrNot::Not(resp)),
}) })
.map(|o| o.unwrap())
}, },
} }
.try_build() .try_build()