Merge pull request #184 from mordak/try_handle_unsolicited

Try handle unsolicited & clippy
This commit is contained in:
Jon Gjengset 2021-03-20 20:18:18 -04:00 committed by GitHub
commit 39a78fdea4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 22 deletions

View file

@ -1459,7 +1459,7 @@ impl<T: Read + Write> Connection<T> {
// Remove CRLF
let len = into.len();
let line = &into[(len - read)..(len - 2)];
eprint!("S: {}\n", String::from_utf8_lossy(line));
eprintln!("S: {}", String::from_utf8_lossy(line));
}
Ok(read)
@ -1475,7 +1475,7 @@ impl<T: Read + Write> Connection<T> {
self.stream.write_all(&[CR, LF])?;
self.stream.flush()?;
if self.debug {
eprint!("C: {}\n", String::from_utf8(buf.to_vec()).unwrap());
eprintln!("C: {}", String::from_utf8(buf.to_vec()).unwrap());
}
Ok(())
}

View file

@ -12,7 +12,7 @@
use crate::client::*;
use crate::error::{Error, ParseError, Result};
use crate::parse::handle_unilateral;
use crate::parse::try_handle_unilateral;
use crate::types::*;
use imap_proto::types::{MailboxDatum, Metadata, Response, ResponseCode};
use std::io::{Read, Write};
@ -34,7 +34,7 @@ impl CmdListItemFormat for Metadata {
self.value
.as_ref()
.map(|v| validate_str(v.as_str()).unwrap())
.unwrap_or("NIL".to_string())
.unwrap_or_else(|| "NIL".to_string())
)
}
}
@ -69,9 +69,9 @@ impl Default for MetadataDepth {
impl MetadataDepth {
fn depth_str<'a>(self) -> &'a str {
match self {
MetadataDepth::Zero => return "0",
MetadataDepth::One => return "1",
MetadataDepth::Infinity => return "infinity",
MetadataDepth::Zero => "0",
MetadataDepth::One => "1",
MetadataDepth::Infinity => "infinity",
}
}
}
@ -97,7 +97,7 @@ fn parse_metadata<'a>(
res.append(&mut values);
}
_ => {
if let Some(unhandled) = handle_unilateral(resp, unsolicited) {
if let Some(unhandled) = try_handle_unilateral(resp, unsolicited) {
break Err(unhandled.into());
}
}
@ -175,18 +175,15 @@ impl<T: Read + Write> Session<T> {
let s = v.as_slice().join(" ");
let mut command = format!("GETMETADATA (DEPTH {}", depth.depth_str());
match maxsize {
Some(size) => {
command.push_str(format!(" MAXSIZE {}", size).as_str());
}
_ => {}
if let Some(size) = maxsize {
command.push_str(format!(" MAXSIZE {}", size).as_str());
}
command.push_str(
format!(
") {} ({})",
mailbox
.map(|mbox| validate_str(mbox.as_ref()).unwrap())
.map(|mbox| validate_str(mbox).unwrap())
.unwrap_or_else(|| "\"\"".to_string()),
s
)

View file

@ -50,7 +50,7 @@ where
match map(resp)? {
MapOrNot::Map(t) => things.push(t),
MapOrNot::Not(resp) => match handle_unilateral(resp, unsolicited) {
MapOrNot::Not(resp) => match try_handle_unilateral(resp, unsolicited) {
Some(Response::Fetch(..)) => continue,
Some(resp) => break Err(resp.into()),
None => {}
@ -150,7 +150,7 @@ pub fn parse_expunge(
}
Ok((rest, data)) => {
lines = rest;
if let Some(resp) = handle_unilateral(data, unsolicited) {
if let Some(resp) = try_handle_unilateral(data, unsolicited) {
return Err(resp.into());
}
}
@ -185,7 +185,7 @@ pub fn parse_capabilities(
}
Ok((rest, data)) => {
lines = rest;
if let Some(resp) = handle_unilateral(data, unsolicited) {
if let Some(resp) = try_handle_unilateral(data, unsolicited) {
break Err(resp.into());
}
}
@ -217,7 +217,7 @@ pub fn parse_noop(
match imap_proto::parser::parse_response(lines) {
Ok((rest, data)) => {
lines = rest;
if let Some(resp) = handle_unilateral(data, unsolicited) {
if let Some(resp) = try_handle_unilateral(data, unsolicited) {
break Err(resp.into());
}
}
@ -339,7 +339,7 @@ pub fn parse_ids(
}
Ok((rest, data)) => {
lines = rest;
if let Some(resp) = handle_unilateral(data, unsolicited) {
if let Some(resp) = try_handle_unilateral(data, unsolicited) {
break Err(resp.into());
}
}
@ -350,9 +350,10 @@ pub fn parse_ids(
}
}
// check if this is simply a unilateral server response
// (see Section 7 of RFC 3501):
pub(crate) fn handle_unilateral<'a>(
// Check if this is simply a unilateral server response (see Section 7 of RFC 3501).
//
// Returns `None` if the response was handled, `Some(res)` if not.
pub(crate) fn try_handle_unilateral<'a>(
res: Response<'a>,
unsolicited: &mut mpsc::Sender<UnsolicitedResponse>,
) -> Option<Response<'a>> {