adjust parse_until_done to return an Option so it is more versatile
This commit is contained in:
parent
1da3eb5571
commit
9c08e14523
2 changed files with 18 additions and 6 deletions
15
src/parse.rs
15
src/parse.rs
|
|
@ -58,12 +58,14 @@ where
|
|||
/// Parse and return an expected single `T` Response with `F`.
|
||||
/// 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>(
|
||||
input: &'input [u8],
|
||||
optional: bool,
|
||||
unsolicited: &mut mpsc::Sender<UnsolicitedResponse>,
|
||||
map: F,
|
||||
) -> Result<T>
|
||||
) -> Result<Option<T>>
|
||||
where
|
||||
F: FnMut(Response<'input>) -> Result<MapOrNot<'input, T>>,
|
||||
{
|
||||
|
|
@ -72,7 +74,14 @@ where
|
|||
parse_many_into(input, &mut temp_output, unsolicited, map)?;
|
||||
|
||||
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()))),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -123,7 +123,7 @@ impl AclResponse {
|
|||
data: owned,
|
||||
acl_builder: |input| {
|
||||
// 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 {
|
||||
mailbox: a.mailbox,
|
||||
acls: a
|
||||
|
|
@ -137,6 +137,7 @@ impl AclResponse {
|
|||
})),
|
||||
resp => Ok(MapOrNot::Not(resp)),
|
||||
})
|
||||
.map(|o| o.unwrap())
|
||||
},
|
||||
}
|
||||
.try_build()
|
||||
|
|
@ -206,7 +207,7 @@ impl ListRightsResponse {
|
|||
data: owned,
|
||||
rights_builder: |input| {
|
||||
// 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 {
|
||||
mailbox: a.mailbox,
|
||||
identifier: a.identifier,
|
||||
|
|
@ -215,6 +216,7 @@ impl ListRightsResponse {
|
|||
})),
|
||||
resp => Ok(MapOrNot::Not(resp)),
|
||||
})
|
||||
.map(|o| o.unwrap())
|
||||
},
|
||||
}
|
||||
.try_build()
|
||||
|
|
@ -286,13 +288,14 @@ impl MyRightsResponse {
|
|||
data: owned,
|
||||
rights_builder: |input| {
|
||||
// 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 {
|
||||
mailbox: a.mailbox,
|
||||
rights: a.rights.into(),
|
||||
})),
|
||||
resp => Ok(MapOrNot::Not(resp)),
|
||||
})
|
||||
.map(|o| o.unwrap())
|
||||
},
|
||||
}
|
||||
.try_build()
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue