From ec835d67e418f8bfe6ef96594e3db60133d4b449 Mon Sep 17 00:00:00 2001 From: Todd Mortimer Date: Sat, 20 Mar 2021 14:21:13 -0400 Subject: [PATCH 1/2] Rename handle_unilateral to try_handle_unilateral. More accurately conveys that the function might not actually handle it. --- src/extensions/metadata.rs | 4 ++-- src/parse.rs | 17 +++++++++-------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/extensions/metadata.rs b/src/extensions/metadata.rs index 5683a11d..726c2750 100644 --- a/src/extensions/metadata.rs +++ b/src/extensions/metadata.rs @@ -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}; @@ -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()); } } diff --git a/src/parse.rs b/src/parse.rs index ece7d36c..69f2a90b 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -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, ) -> Option> { From d0e61c73e9f8e8c38a9f7683d63ce75db78fc55c Mon Sep 17 00:00:00 2001 From: Todd Mortimer Date: Sat, 20 Mar 2021 14:26:52 -0400 Subject: [PATCH 2/2] Appease clippy. --- src/client.rs | 4 ++-- src/extensions/metadata.rs | 17 +++++++---------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/client.rs b/src/client.rs index e13dfd68..a5ddc5c2 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1459,7 +1459,7 @@ impl Connection { // 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 Connection { 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(()) } diff --git a/src/extensions/metadata.rs b/src/extensions/metadata.rs index 726c2750..619a2143 100644 --- a/src/extensions/metadata.rs +++ b/src/extensions/metadata.rs @@ -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", } } } @@ -175,18 +175,15 @@ impl Session { 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 )