From 3a5c2e9d91f0fd92d0289d3e2ce55c60100f1027 Mon Sep 17 00:00:00 2001 From: Lucas Date: Fri, 3 May 2019 14:44:16 +1000 Subject: [PATCH 1/5] Implemented new enumerated Capabilities from imap-proto crate. --- src/client.rs | 2 +- src/parse.rs | 4 ++-- src/types/capabilities.rs | 27 ++++++++++++++++++--------- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/client.rs b/src/client.rs index 5540f4e..8192cfe 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1620,7 +1620,7 @@ mod tests { ); assert_eq!(capabilities.len(), 4); for e in expected_capabilities { - assert!(capabilities.has(e)); + assert!(capabilities.has_str(e)); } } diff --git a/src/parse.rs b/src/parse.rs index 8b32c17..a2de763 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -342,7 +342,7 @@ mod tests { assert!(recv.try_recv().is_err()); assert_eq!(capabilities.len(), 4); for e in expected_capabilities { - assert!(capabilities.has(e)); + assert!(capabilities.has_str(e)); } } @@ -445,7 +445,7 @@ mod tests { assert_eq!(capabilities.len(), 4); for e in expected_capabilities { - assert!(capabilities.has(e)); + assert!(capabilities.has_str(e)); } assert_eq!( diff --git a/src/types/capabilities.rs b/src/types/capabilities.rs index 166bfa8..fbabacd 100644 --- a/src/types/capabilities.rs +++ b/src/types/capabilities.rs @@ -1,7 +1,9 @@ -use std::borrow::Borrow; +use imap_proto::types::Capability; use std::collections::hash_set::Iter; use std::collections::HashSet; -use std::hash::Hash; + +const IMAP4REV1_CAPABILITY: &str = "IMAP4rev1"; +const AUTH_CAPABILITY_PREFIX: &str = "AUTH="; /// From [section 7.2.1 of RFC 3501](https://tools.ietf.org/html/rfc3501#section-7.2.1). /// @@ -31,21 +33,28 @@ use std::hash::Hash; pub struct Capabilities( // Note that this field isn't *actually* 'static. // Rather, it is tied to the lifetime of the `ZeroCopy` that contains this `Name`. - pub(crate) HashSet<&'static str>, + pub(crate) HashSet>, ); impl Capabilities { /// Check if the server has the given capability. - pub fn has(&self, s: &S) -> bool - where - for<'a> &'a str: Borrow, - S: Hash + Eq, - { + pub fn has<'a>(&self, s: &Capability<'a>) -> bool { self.0.contains(s) } + /// Check if the server has the given capability via str. + pub fn has_str(&self, s: &str) -> bool { + if s == IMAP4REV1_CAPABILITY { + self.has(&Capability::Imap4rev1) + } else if s.starts_with(AUTH_CAPABILITY_PREFIX) { + self.has(&Capability::Auth(&s[AUTH_CAPABILITY_PREFIX.len()..])) + } else { + self.has(&Capability::Atom(s)) + } + } + /// Iterate over all the server's capabilities - pub fn iter(&self) -> Iter<&str> { + pub fn iter(&self) -> Iter { self.0.iter() } From ff8e638591de6c3be4a56f31f743eede3d903cd0 Mon Sep 17 00:00:00 2001 From: avitex Date: Mon, 2 Sep 2019 23:01:40 +1000 Subject: [PATCH 2/5] Fix dyn warnings --- src/error.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/error.rs b/src/error.rs index 6891406..ef89939 100644 --- a/src/error.rs +++ b/src/error.rs @@ -106,7 +106,7 @@ impl StdError for Error { } } - fn cause(&self) -> Option<&StdError> { + fn cause(&self) -> Option<&dyn StdError> { match *self { Error::Io(ref e) => Some(e), Error::Tls(ref e) => Some(e), @@ -148,7 +148,7 @@ impl StdError for ParseError { } } - fn cause(&self) -> Option<&StdError> { + fn cause(&self) -> Option<&dyn StdError> { match *self { ParseError::Authentication(_, Some(ref e)) => Some(e), _ => None, @@ -173,7 +173,7 @@ impl StdError for ValidateError { "Invalid character in input" } - fn cause(&self) -> Option<&StdError> { + fn cause(&self) -> Option<&dyn StdError> { None } } From 1c348a4ffac2388fe59c13482eba5db88ea9928f Mon Sep 17 00:00:00 2001 From: avitex Date: Mon, 2 Sep 2019 23:02:51 +1000 Subject: [PATCH 3/5] Make has_str input generic --- src/types/capabilities.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/types/capabilities.rs b/src/types/capabilities.rs index fbabacd..2cc3672 100644 --- a/src/types/capabilities.rs +++ b/src/types/capabilities.rs @@ -38,12 +38,13 @@ pub struct Capabilities( impl Capabilities { /// Check if the server has the given capability. - pub fn has<'a>(&self, s: &Capability<'a>) -> bool { - self.0.contains(s) + pub fn has<'a>(&self, cap: &Capability<'a>) -> bool { + self.0.contains(cap) } /// Check if the server has the given capability via str. - pub fn has_str(&self, s: &str) -> bool { + pub fn has_str>(&self, cap: S) -> bool { + let s = cap.as_ref(); if s == IMAP4REV1_CAPABILITY { self.has(&Capability::Imap4rev1) } else if s.starts_with(AUTH_CAPABILITY_PREFIX) { From 130361856524e73dccf8c5fcc2649c8852a31d59 Mon Sep 17 00:00:00 2001 From: avitex Date: Mon, 2 Sep 2019 23:06:37 +1000 Subject: [PATCH 4/5] Update imap-proto dependency --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index a0b23cb..adb39f6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,7 +29,7 @@ path = "src/lib.rs" native-tls = "0.2.2" regex = "1.0" bufstream = "0.1" -imap-proto = "0.7" +imap-proto = "0.8" nom = "4.0" base64 = "0.10" chrono = "0.4" From 1c51fbe3acfe688480985905975ba728cb0d4bea Mon Sep 17 00:00:00 2001 From: avitex Date: Mon, 2 Sep 2019 23:44:18 +1000 Subject: [PATCH 5/5] Make clippy happy again --- src/client.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client.rs b/src/client.rs index 8192cfe..5aeb99b 100644 --- a/src/client.rs +++ b/src/client.rs @@ -14,7 +14,7 @@ use super::extensions; use super::parse::*; use super::types::*; -static TAG_PREFIX: &'static str = "a"; +static TAG_PREFIX: &str = "a"; const INITIAL_TAG: u32 = 0; const CR: u8 = 0x0d; const LF: u8 = 0x0a;