From fb89e4a50ae85b1e988c49a2a2d51bdab87a04f0 Mon Sep 17 00:00:00 2001 From: Matt McCoy Date: Tue, 21 Jun 2016 15:46:43 -0400 Subject: [PATCH] Changing IMAPMailbox to Mailbox --- example.rs | 5 ++--- src/client.rs | 32 +++++++++++++++++++------------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/example.rs b/example.rs index a204e7d..0f4161a 100644 --- a/example.rs +++ b/example.rs @@ -2,8 +2,7 @@ extern crate imap; extern crate openssl; use openssl::ssl::{SslContext, SslMethod}; -use imap::client::Client; -use imap::client::IMAPMailbox; +use imap::client::{Client, Mailbox}; fn main() { let mut imap_socket = match Client::secure_connect(("imap.gmail.com", 993), SslContext::new(SslMethod::Sslv23).unwrap()) { @@ -25,7 +24,7 @@ fn main() { }; match imap_socket.select("INBOX") { - Ok(IMAPMailbox{flags, exists, recent, unseen, permanent_flags, uid_next, uid_validity}) => { + Ok(Mailbox{flags, exists, recent, unseen, permanent_flags, uid_next, uid_validity}) => { println!("flags: {}, exists: {}, recent: {}, unseen: {:?}, permanent_flags: {:?}, uid_next: {:?}, uid_validity: {:?}", flags, exists, recent, unseen, permanent_flags, uid_next, uid_validity); }, Err(_) => println!("Error selecting INBOX") diff --git a/src/client.rs b/src/client.rs index 9ef62c8..0f24add 100644 --- a/src/client.rs +++ b/src/client.rs @@ -12,7 +12,7 @@ pub struct Client { tag: u32 } -pub struct IMAPMailbox { +pub struct Mailbox { pub flags: String, pub exists: u32, pub recent: u32, @@ -22,6 +22,20 @@ pub struct IMAPMailbox { pub uid_validity: Option } +impl Default for Mailbox { + fn default() -> Mailbox { + Mailbox { + flags: "".to_string(), + exists: 0, + recent: 0, + unseen: None, + permanent_flags: None, + uid_next: None, + uid_validity: None + } + } +} + impl Client { /// Creates a new client. pub fn connect(addr: A) -> Result> { @@ -66,14 +80,14 @@ impl Client { } /// Selects a mailbox - pub fn select(&mut self, mailbox_name: &str) -> Result { + pub fn select(&mut self, mailbox_name: &str) -> Result { match self.run_command(&format!("SELECT {}", mailbox_name).to_string()) { Ok(lines) => self.parse_select_or_examine(lines), Err(e) => Err(e) } } - fn parse_select_or_examine(&mut self, lines: Vec) -> Result { + fn parse_select_or_examine(&mut self, lines: Vec) -> Result { let exists_regex = Regex::new(r"^\* (\d+) EXISTS\r\n").unwrap(); let recent_regex = Regex::new(r"^\* (\d+) RECENT\r\n").unwrap(); @@ -94,15 +108,7 @@ impl Client { Err(e) => return Err(e) }; - let mut mailbox = IMAPMailbox{ - flags: "".to_string(), - exists: 0, - recent: 0, - unseen: None, - permanent_flags: None, - uid_next: None, - uid_validity: None - }; + let mut mailbox = Mailbox::default(); for line in lines.iter() { if exists_regex.is_match(line) { @@ -133,7 +139,7 @@ impl Client { } /// Examine is identical to Select, but the selected mailbox is identified as read-only - pub fn examine(&mut self, mailbox_name: &str) -> Result { + pub fn examine(&mut self, mailbox_name: &str) -> Result { match self.run_command(&format!("EXAMINE {}", mailbox_name).to_string()) { Ok(lines) => self.parse_select_or_examine(lines), Err(e) => Err(e)