Skip to content

Commit 39a78fd

Browse files
authored
Merge pull request #184 from mordak/try_handle_unsolicited
Try handle unsolicited & clippy
2 parents bd0a045 + d0e61c7 commit 39a78fd

File tree

3 files changed

+20
-22
lines changed

3 files changed

+20
-22
lines changed

src/client.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,7 +1459,7 @@ impl<T: Read + Write> Connection<T> {
14591459
// Remove CRLF
14601460
let len = into.len();
14611461
let line = &into[(len - read)..(len - 2)];
1462-
eprint!("S: {}\n", String::from_utf8_lossy(line));
1462+
eprintln!("S: {}", String::from_utf8_lossy(line));
14631463
}
14641464

14651465
Ok(read)
@@ -1475,7 +1475,7 @@ impl<T: Read + Write> Connection<T> {
14751475
self.stream.write_all(&[CR, LF])?;
14761476
self.stream.flush()?;
14771477
if self.debug {
1478-
eprint!("C: {}\n", String::from_utf8(buf.to_vec()).unwrap());
1478+
eprintln!("C: {}", String::from_utf8(buf.to_vec()).unwrap());
14791479
}
14801480
Ok(())
14811481
}

src/extensions/metadata.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
1313
use crate::client::*;
1414
use crate::error::{Error, ParseError, Result};
15-
use crate::parse::handle_unilateral;
15+
use crate::parse::try_handle_unilateral;
1616
use crate::types::*;
1717
use imap_proto::types::{MailboxDatum, Metadata, Response, ResponseCode};
1818
use std::io::{Read, Write};
@@ -34,7 +34,7 @@ impl CmdListItemFormat for Metadata {
3434
self.value
3535
.as_ref()
3636
.map(|v| validate_str(v.as_str()).unwrap())
37-
.unwrap_or("NIL".to_string())
37+
.unwrap_or_else(|| "NIL".to_string())
3838
)
3939
}
4040
}
@@ -69,9 +69,9 @@ impl Default for MetadataDepth {
6969
impl MetadataDepth {
7070
fn depth_str<'a>(self) -> &'a str {
7171
match self {
72-
MetadataDepth::Zero => return "0",
73-
MetadataDepth::One => return "1",
74-
MetadataDepth::Infinity => return "infinity",
72+
MetadataDepth::Zero => "0",
73+
MetadataDepth::One => "1",
74+
MetadataDepth::Infinity => "infinity",
7575
}
7676
}
7777
}
@@ -97,7 +97,7 @@ fn parse_metadata<'a>(
9797
res.append(&mut values);
9898
}
9999
_ => {
100-
if let Some(unhandled) = handle_unilateral(resp, unsolicited) {
100+
if let Some(unhandled) = try_handle_unilateral(resp, unsolicited) {
101101
break Err(unhandled.into());
102102
}
103103
}
@@ -175,18 +175,15 @@ impl<T: Read + Write> Session<T> {
175175
let s = v.as_slice().join(" ");
176176
let mut command = format!("GETMETADATA (DEPTH {}", depth.depth_str());
177177

178-
match maxsize {
179-
Some(size) => {
180-
command.push_str(format!(" MAXSIZE {}", size).as_str());
181-
}
182-
_ => {}
178+
if let Some(size) = maxsize {
179+
command.push_str(format!(" MAXSIZE {}", size).as_str());
183180
}
184181

185182
command.push_str(
186183
format!(
187184
") {} ({})",
188185
mailbox
189-
.map(|mbox| validate_str(mbox.as_ref()).unwrap())
186+
.map(|mbox| validate_str(mbox).unwrap())
190187
.unwrap_or_else(|| "\"\"".to_string()),
191188
s
192189
)

src/parse.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ where
5050

5151
match map(resp)? {
5252
MapOrNot::Map(t) => things.push(t),
53-
MapOrNot::Not(resp) => match handle_unilateral(resp, unsolicited) {
53+
MapOrNot::Not(resp) => match try_handle_unilateral(resp, unsolicited) {
5454
Some(Response::Fetch(..)) => continue,
5555
Some(resp) => break Err(resp.into()),
5656
None => {}
@@ -150,7 +150,7 @@ pub fn parse_expunge(
150150
}
151151
Ok((rest, data)) => {
152152
lines = rest;
153-
if let Some(resp) = handle_unilateral(data, unsolicited) {
153+
if let Some(resp) = try_handle_unilateral(data, unsolicited) {
154154
return Err(resp.into());
155155
}
156156
}
@@ -185,7 +185,7 @@ pub fn parse_capabilities(
185185
}
186186
Ok((rest, data)) => {
187187
lines = rest;
188-
if let Some(resp) = handle_unilateral(data, unsolicited) {
188+
if let Some(resp) = try_handle_unilateral(data, unsolicited) {
189189
break Err(resp.into());
190190
}
191191
}
@@ -217,7 +217,7 @@ pub fn parse_noop(
217217
match imap_proto::parser::parse_response(lines) {
218218
Ok((rest, data)) => {
219219
lines = rest;
220-
if let Some(resp) = handle_unilateral(data, unsolicited) {
220+
if let Some(resp) = try_handle_unilateral(data, unsolicited) {
221221
break Err(resp.into());
222222
}
223223
}
@@ -339,7 +339,7 @@ pub fn parse_ids(
339339
}
340340
Ok((rest, data)) => {
341341
lines = rest;
342-
if let Some(resp) = handle_unilateral(data, unsolicited) {
342+
if let Some(resp) = try_handle_unilateral(data, unsolicited) {
343343
break Err(resp.into());
344344
}
345345
}
@@ -350,9 +350,10 @@ pub fn parse_ids(
350350
}
351351
}
352352

353-
// check if this is simply a unilateral server response
354-
// (see Section 7 of RFC 3501):
355-
pub(crate) fn handle_unilateral<'a>(
353+
// Check if this is simply a unilateral server response (see Section 7 of RFC 3501).
354+
//
355+
// Returns `None` if the response was handled, `Some(res)` if not.
356+
pub(crate) fn try_handle_unilateral<'a>(
356357
res: Response<'a>,
357358
unsolicited: &mut mpsc::Sender<UnsolicitedResponse>,
358359
) -> Option<Response<'a>> {

0 commit comments

Comments
 (0)