Use "email" instead of "mailparse"

This commit is contained in:
Matthias Beyer 2017-06-07 17:38:50 +02:00
parent 16a12af873
commit 41a12db528
4 changed files with 18 additions and 18 deletions

View file

@ -15,7 +15,7 @@ homepage = "http://imag-pim.org"
[dependencies] [dependencies]
log = "0.3" log = "0.3"
mailparse = "0.4" email = "0.0.17"
semver = "0.5" semver = "0.5"
toml = "0.4.*" toml = "0.4.*"
filters = "0.1.*" filters = "0.1.*"

View file

@ -1,7 +1,7 @@
use std::io::Read; use std::io::Read;
use std::path::PathBuf; use std::path::PathBuf;
use mailparse::{MailHeader, parse_mail}; use email::MimeMessage;
use libimagref::hasher::Hasher; use libimagref::hasher::Hasher;
use libimagref::hasher::DefaultHasher; use libimagref::hasher::DefaultHasher;
@ -32,27 +32,28 @@ impl Hasher for MailHasher {
fn create_hash<R: Read>(&mut self, pb: &PathBuf, c: &mut R) -> RResult<String> { fn create_hash<R: Read>(&mut self, pb: &PathBuf, c: &mut R) -> RResult<String> {
use filters::filter::Filter; use filters::filter::Filter;
use email::Header;
let mut s = String::new(); let mut s = String::new();
try!(c.read_to_string(&mut s).map_err_into(REK::UTF8Error).map_err_into(REK::IOError)); try!(c.read_to_string(&mut s).map_err_into(REK::UTF8Error).map_err_into(REK::IOError));
parse_mail(&s.as_bytes()) MimeMessage::parse(&s)
.map_err(Box::new) .map_err(Box::new)
.map_err(|e| MEK::MailParsingError.into_error_with_cause(e)) .map_err(|e| MEK::MailParsingError.into_error_with_cause(e))
.map_err_into(REK::RefHashingError) .map_err_into(REK::RefHashingError)
.and_then(|mail| { .and_then(|mail| {
let has_key = |hdr: &MailHeader, exp: &str| let has_key = |hdr: &Header, exp: &str| hdr.name == exp;
hdr.get_key().map(|s| s == exp).unwrap_or(false);
let subject_filter = |hdr: &MailHeader| has_key(hdr, "Subject"); let subject_filter = |hdr: &Header| has_key(hdr, "Subject");
let from_filter = |hdr: &MailHeader| has_key(hdr, "From"); let from_filter = |hdr: &Header| has_key(hdr, "From");
let to_filter = |hdr: &MailHeader| has_key(hdr, "To"); let to_filter = |hdr: &Header| has_key(hdr, "To");
let filter = subject_filter.or(from_filter).or(to_filter); let filter = subject_filter.or(from_filter).or(to_filter);
let mut v = vec![]; let mut v : Vec<String> = vec![];
for hdr in mail.headers.iter().filter(|item| filter.filter(item)) { for hdr in mail.headers.iter().filter(|item| filter.filter(item)) {
let s = try!(hdr.get_value() let s = try!(hdr
.get_value()
.map_err(Box::new) .map_err(Box::new)
.map_err(|e| REK::RefHashingError.into_error_with_cause(e))); .map_err(|e| REK::RefHashingError.into_error_with_cause(e)));

View file

@ -1,5 +1,5 @@
#[macro_use] extern crate log; #[macro_use] extern crate log;
extern crate mailparse; extern crate email;
extern crate semver; extern crate semver;
extern crate toml; extern crate toml;
extern crate filters; extern crate filters;

View file

@ -8,7 +8,8 @@ use libimagstore::store::{FileLockEntry, Store};
use libimagref::reference::Ref; use libimagref::reference::Ref;
use libimagref::flags::RefFlags; use libimagref::flags::RefFlags;
use mailparse::{MailParseError, ParsedMail, parse_mail}; use email::MimeMessage;
use email::results::ParsingResult as EmailParsingResult;
use hasher::MailHasher; use hasher::MailHasher;
use result::Result; use result::Result;
@ -17,8 +18,8 @@ use error::{MapErrInto, MailErrorKind as MEK};
struct Buffer(String); struct Buffer(String);
impl Buffer { impl Buffer {
pub fn parsed<'a>(&'a self) -> RResult<ParsedMail<'a>, MailParseError> { pub fn parsed(&self) -> EmailParsingResult<MimeMessage> {
parse_mail(self.0.as_bytes()) MimeMessage::parse(&self.0)
} }
} }
@ -83,16 +84,14 @@ impl<'a> Mail<'a> {
} }
pub fn get_field(&self, field: &str) -> Result<Option<String>> { pub fn get_field(&self, field: &str) -> Result<Option<String>> {
use mailparse::MailHeader;
self.1 self.1
.parsed() .parsed()
.map_err_into(MEK::MailParsingError) .map_err_into(MEK::MailParsingError)
.map(|parsed| { .map(|parsed| {
parsed.headers parsed.headers
.iter() .iter()
.filter(|hdr| hdr.get_key().map(|n| n == field).unwrap_or(false)) .filter(|hdr| hdr.name == field)
.next() .nth(0)
.and_then(|field| field.get_value().ok()) .and_then(|field| field.get_value().ok())
}) })
} }