diff --git a/src/client.rs b/src/client.rs index 649012e..d705ed3 100644 --- a/src/client.rs +++ b/src/client.rs @@ -4,6 +4,7 @@ use std::io::{Error, ErrorKind, Read, Result, Write}; use regex::Regex; use super::mailbox::Mailbox; +use super::parse::parse_response_ok; static TAG_PREFIX: &'static str = "a"; const INITIAL_TAG: u32 = 0; @@ -81,7 +82,7 @@ impl Client { let permanent_flags_regex = Regex::new(r"^\* OK \[PERMANENTFLAGS (.+)\]\r\n").unwrap(); //Check Ok - match self.parse_response_ok(lines.clone()) { + match parse_response_ok(lines.clone()) { Ok(_) => (), Err(e) => return Err(e) }; @@ -178,7 +179,7 @@ impl Client { let capability_regex = Regex::new(r"^\* CAPABILITY (.*)\r\n").unwrap(); //Check Ok - match self.parse_response_ok(lines.clone()) { + match parse_response_ok(lines.clone()) { Ok(_) => (), Err(e) => return Err(e) }; @@ -218,7 +219,7 @@ impl Client { pub fn run_command_and_check_ok(&mut self, command: &str) -> Result<()> { match self.run_command(command) { - Ok(lines) => self.parse_response_ok(lines), + Ok(lines) => parse_response_ok(lines), Err(e) => Err(e) } } @@ -234,20 +235,6 @@ impl Client { self.read_response() } - fn parse_response_ok(&mut self, lines: Vec) -> Result<()> { - let ok_regex = Regex::new(r"^([a-zA-Z0-9]+) ([a-zA-Z0-9]+)(.*)").unwrap(); - let last_line = lines.last().unwrap(); - - for cap in ok_regex.captures_iter(last_line) { - let response_type = cap.at(2).unwrap_or(""); - if response_type == "OK" { - return Ok(()); - } - } - - return Err(Error::new(ErrorKind::Other, format!("Invalid Response: {}", last_line).to_string())); - } - fn read_response(&mut self) -> Result> { let mut found_tag_line = false; let start_str = format!("{}{} ", TAG_PREFIX, self.tag); diff --git a/src/lib.rs b/src/lib.rs index 9727a66..b0afb0b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,5 +9,7 @@ extern crate regex; pub mod client; pub mod mailbox; +mod parse; + #[cfg(test)] mod mock_stream; diff --git a/src/parse.rs b/src/parse.rs new file mode 100644 index 0000000..4e2f71e --- /dev/null +++ b/src/parse.rs @@ -0,0 +1,16 @@ +use std::io::{Error, ErrorKind, Result}; +use regex::Regex; + +pub fn parse_response_ok(lines: Vec) -> Result<()> { + let ok_regex = Regex::new(r"^([a-zA-Z0-9]+) ([a-zA-Z0-9]+)(.*)").unwrap(); + let last_line = lines.last().unwrap(); + + for cap in ok_regex.captures_iter(last_line) { + let response_type = cap.at(2).unwrap_or(""); + if response_type == "OK" { + return Ok(()); + } + } + + return Err(Error::new(ErrorKind::Other, format!("Invalid Response: {}", last_line).to_string())); +}