Adding errors for parsing problems

This commit is contained in:
Matt McCoy 2016-06-24 19:38:04 -04:00
parent 6992615921
commit 378b4bbb96
2 changed files with 38 additions and 5 deletions

View file

@ -18,6 +18,8 @@ pub enum Error {
BadResponse(Vec<String>), BadResponse(Vec<String>),
/// A NO response from the IMAP server. /// A NO response from the IMAP server.
NoResponse(Vec<String>), NoResponse(Vec<String>),
// Error parsing a server response.
Parse(ParseError)
} }
impl From<IoError> for Error { impl From<IoError> for Error {
@ -47,8 +49,9 @@ impl StdError for Error {
match *self { match *self {
Error::Io(ref e) => e.description(), Error::Io(ref e) => e.description(),
Error::Ssl(ref e) => e.description(), Error::Ssl(ref e) => e.description(),
Error::Parse(ref e) => e.description(),
Error::BadResponse(_) => "Bad Response", Error::BadResponse(_) => "Bad Response",
Error::NoResponse(_) => "No Response" Error::NoResponse(_) => "No Response",
} }
} }
@ -60,3 +63,34 @@ impl StdError for Error {
} }
} }
} }
#[derive(Debug)]
pub enum ParseError {
// Indicates an error parsing the status response. Such as OK, NO, and BAD.
StatusResponse(Vec<String>),
// Error parsing the cabability response.
Capability(Vec<String>)
}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
ref e => f.write_str(e.description()),
}
}
}
impl StdError for ParseError {
fn description(&self) -> &str {
match *self {
ParseError::StatusResponse(_) => "Unable to parse status response",
ParseError::Capability(_) => "Unable to parse capability response"
}
}
fn cause(&self) -> Option<&StdError> {
match *self {
_ => None
}
}
}

View file

@ -1,8 +1,7 @@
use std::io::{self};
use regex::Regex; use regex::Regex;
use super::mailbox::Mailbox; use super::mailbox::Mailbox;
use super::error::{Error, Result}; use super::error::{Error, ParseError, Result};
pub fn parse_capability(lines: Vec<String>) -> Result<Vec<String>> { pub fn parse_capability(lines: Vec<String>) -> Result<Vec<String>> {
let capability_regex = Regex::new(r"^\* CAPABILITY (.*)\r\n").unwrap(); let capability_regex = Regex::new(r"^\* CAPABILITY (.*)\r\n").unwrap();
@ -21,7 +20,7 @@ pub fn parse_capability(lines: Vec<String>) -> Result<Vec<String>> {
} }
} }
Err(Error::Io(io::Error::new(io::ErrorKind::Other, "Error parsing capabilities response"))) Err(Error::Parse(ParseError::Capability(lines)))
} }
pub fn parse_response_ok(lines: Vec<String>) -> Result<()> { pub fn parse_response_ok(lines: Vec<String>) -> Result<()> {
@ -35,7 +34,7 @@ pub fn parse_response_ok(lines: Vec<String>) -> Result<()> {
} }
} }
Err(Error::Io(io::Error::new(io::ErrorKind::Other, format!("Invalid Response: {}", last_line).to_string()))) Err(Error::Parse(ParseError::StatusResponse(lines.clone())))
} }
pub fn parse_select_or_examine(lines: Vec<String>) -> Result<Mailbox> { pub fn parse_select_or_examine(lines: Vec<String>) -> Result<Mailbox> {