Skip to content

Try handle unsolicited & clippy #184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(())
}
Expand Down
21 changes: 9 additions & 12 deletions src/extensions/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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())
)
}
}
Expand Down Expand Up @@ -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",
}
}
}
Expand All @@ -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());
}
}
Expand Down Expand Up @@ -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
)
Expand Down
17 changes: 9 additions & 8 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {}
Expand Down Expand Up @@ -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());
}
}
Expand Down Expand Up @@ -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());
}
}
Expand Down Expand Up @@ -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());
}
}
Expand Down Expand Up @@ -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());
}
}
Expand All @@ -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>> {
Expand Down