extern crate base64;
extern crate encoding;
extern crate quoted_printable;
use std::ascii::AsciiExt;
use std::error;
use std::fmt;
use std::ops::Deref;
use encoding::Encoding;
#[macro_use]
mod macros;
mod dateparse;
pub use dateparse::dateparse;
/// An error type that represents the different kinds of errors that may be
/// encountered during message parsing.
#[derive(Debug)]
pub enum MailParseError {
/// Data that was specified as being in the quoted-printable transfer-encoding
/// could not be successfully decoded as quoted-printable data.
QuotedPrintableDecodeError(quoted_printable::QuotedPrintableError),
/// Data that was specified as being in the base64 transfer-encoding could
/// not be successfully decoded as base64 data.
Base64DecodeError(base64::Base64Error),
/// An error occurred when converting the raw byte data to Rust UTF-8 string
/// format using the charset specified in the message.
EncodingError(std::borrow::Cow<'static, str>),
/// Some other error occurred while parsing the message; the description string
/// provides additional details.
Generic(&'static str),
}
impl fmt::Display for MailParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
MailParseError::QuotedPrintableDecodeError(ref err) => {
write!(f, "QuotedPrintable decode error: {}", err)
}
MailParseError::Base64DecodeError(ref err) => write!(f, "Base64 decode error: {}", err),
MailParseError::EncodingError(ref err) => write!(f, "Encoding error: {}", err),
MailParseError::Generic(ref description) => write!(f, "{}", description),
}
}
}
impl error::Error for MailParseError {
fn description(&self) -> &str {
match *self {
MailParseError::QuotedPrintableDecodeError(ref err) => err.description(),
MailParseError::Base64DecodeError(ref err) => err.description(),
MailParseError::EncodingError(ref err) => err.deref(),
_ => "An error occurred while attempting to parse the input",
}
}
fn cause(&self) -> Option<&error::Error> {
match *self {
MailParseError::QuotedPrintableDecodeError(ref err) => Some(err),
MailParseError::Base64DecodeError(ref err) => Some(err),
_ => None,
}
}
}
impl From<quoted_printable::QuotedPrintableError> for MailParseError {
fn from(err: quoted_printable::QuotedPrintableError) -> MailParseError {
MailParseError::QuotedPrintableDecodeError(err)
}
}
impl From<base64::Base64Error> for MailParseError {
fn from(err: base64::Base64Error) -> MailParseError {
MailParseError::Base64DecodeError(err)
}
}
impl From<std::borrow::Cow<'static, str>> for MailParseError {
fn from(err: std::borrow::Cow<'static, str>) -> MailParseError {
MailParseError::EncodingError(err)
}
}
/// A struct that represents a single header in the message.
/// It holds slices into the raw byte array passed to parse_mail, and so the
/// lifetime of this struct must be contained within the lifetime of the raw
/// input. There are additional accessor functions on this struct to extract
/// the data as Rust strings.
#[derive(Debug)]
pub struct MailHeader<'a> {
key: &'a [u8],
value: &'a [u8],
}
fn is_boundary(line: &str, ix: Option<usize>) -> bool {
match ix {
None => true,
Some(v) => {
if v >= line.len() {
return true;
}
let c = line.chars().nth(v).unwrap();
return c.is_whitespace() || c == '"' || c == '(' || c == ')' || c == '<' || c == '>';
}
}
}
fn find_from(line: &str, ix_start: usize, key: &str) -> Option<usize> {
line[ix_start..].find(key).map(|v| ix_start + v)
}
fn find_from_u8(line: &[u8], ix_start: usize, key: &[u8]) -> Option<usize> {
assert!(key.len() > 0);
assert!(ix_start < line.len());
let ix_end = line.len() - key.len();
if ix_start <= ix_end {
for i in ix_start..ix_end {
if line[i] == key[0] {
let mut success = true;
for j in 1..key.len() {