diff --git a/.travis.yml b/.travis.yml index 4081daa..952ce42 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,7 @@ rust: - stable - beta - nightly -- 1.13.0 +- 1.15.0 matrix: allow_failures: diff --git a/README.md b/README.md index 8c2cae6..f4527ed 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ Development version: ## Install -This library requires rust 1.13 or newer. +This library requires rust 1.15 or newer. To use this library, add the following to your `Cargo.toml`: ```toml diff --git a/lettre/Cargo.toml b/lettre/Cargo.toml index c01ab13..a06d668 100644 --- a/lettre/Cargo.toml +++ b/lettre/Cargo.toml @@ -21,6 +21,9 @@ openssl = "^0.9" base64 = "^0.6" hex = "^0.2" rust-crypto = "^0.2" +serde = "^1.0" +serde_json = "^1.0" +serde_derive = "^1.0" [dev-dependencies] env_logger = "^0.4" diff --git a/lettre/src/file/error.rs b/lettre/src/file/error.rs index e74f566..ab1b2b0 100644 --- a/lettre/src/file/error.rs +++ b/lettre/src/file/error.rs @@ -1,6 +1,7 @@ //! Error and result type for file transport use self::Error::*; +use serde_json; use std::error::Error as StdError; use std::fmt; use std::fmt::{Display, Formatter}; @@ -13,6 +14,8 @@ pub enum Error { Client(&'static str), /// IO error Io(io::Error), + /// JSON serialization error + JsonSerialization(serde_json::Error), } impl Display for Error { @@ -26,6 +29,7 @@ impl StdError for Error { match *self { Client(_) => "an unknown error occured", Io(_) => "an I/O error occured", + JsonSerialization(_) => "a JSON serialization error occured", } } @@ -43,6 +47,12 @@ impl From for Error { } } +impl From for Error { + fn from(err: serde_json::Error) -> Error { + JsonSerialization(err) + } +} + impl From<&'static str> for Error { fn from(string: &'static str) -> Error { Client(string) diff --git a/lettre/src/file/mod.rs b/lettre/src/file/mod.rs index c11cd82..0780b65 100644 --- a/lettre/src/file/mod.rs +++ b/lettre/src/file/mod.rs @@ -35,7 +35,10 @@ use EmailTransport; use SendableEmail; +use SimpleSendableEmail; use file::error::FileResult; + +use serde_json; use std::fs::File; use std::io::prelude::*; use std::path::{Path, PathBuf}; @@ -64,17 +67,16 @@ impl EmailTransport for FileEmailTransport { let mut f = try!(File::create(file.as_path())); - let log_line = format!( - "{}: from=<{}> to=<{}>\n", - email.message_id(), - email.from(), - email.to().join("> to=<") + let simple_email = SimpleSendableEmail::new( + &email.from(), + email.to().iter().map(String::as_str).collect(), + &email.message_id(), + &email.message(), ); - try!(f.write_all(log_line.as_bytes())); - try!(f.write_all(email.message().as_bytes())); - - info!("{} status=", log_line); + try!(f.write_all( + serde_json::to_string(&simple_email)?.as_bytes(), + )); Ok(()) } diff --git a/lettre/src/lib.rs b/lettre/src/lib.rs index 7d2c184..8b6a303 100644 --- a/lettre/src/lib.rs +++ b/lettre/src/lib.rs @@ -13,6 +13,10 @@ extern crate hex; extern crate crypto; extern crate bufstream; extern crate openssl; +extern crate serde_json; +extern crate serde; +#[macro_use] +extern crate serde_derive; pub mod smtp; pub mod sendmail; @@ -40,7 +44,7 @@ pub trait EmailTransport { } /// Minimal email structure -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct SimpleSendableEmail { /// To to: Vec, diff --git a/lettre/tests/transport_file.rs b/lettre/tests/transport_file.rs index 4ff9c76..e7eafc6 100644 --- a/lettre/tests/transport_file.rs +++ b/lettre/tests/transport_file.rs @@ -28,11 +28,8 @@ fn file_transport() { assert_eq!( buffer, - format!( - "{}: from= to=\n{}", - message_id, - email.message() - ) + "{\"to\":[\"root@localhost\"],\"from\":\"user@localhost\",\ + \"message_id\":\"file_id\",\"message\":\"Hello file\"}" ); remove_file(file).unwrap();