feat(transport): Use serde to serialize email in the file transport

This commit is contained in:
Alexis Mousset
2017-06-17 17:33:53 +02:00
parent 1378b8959d
commit 63f35f78a6
7 changed files with 33 additions and 17 deletions

View File

@@ -4,7 +4,7 @@ rust:
- stable - stable
- beta - beta
- nightly - nightly
- 1.13.0 - 1.15.0
matrix: matrix:
allow_failures: allow_failures:

View File

@@ -33,7 +33,7 @@ Development version:
## Install ## 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`: To use this library, add the following to your `Cargo.toml`:
```toml ```toml

View File

@@ -21,6 +21,9 @@ openssl = "^0.9"
base64 = "^0.6" base64 = "^0.6"
hex = "^0.2" hex = "^0.2"
rust-crypto = "^0.2" rust-crypto = "^0.2"
serde = "^1.0"
serde_json = "^1.0"
serde_derive = "^1.0"
[dev-dependencies] [dev-dependencies]
env_logger = "^0.4" env_logger = "^0.4"

View File

@@ -1,6 +1,7 @@
//! Error and result type for file transport //! Error and result type for file transport
use self::Error::*; use self::Error::*;
use serde_json;
use std::error::Error as StdError; use std::error::Error as StdError;
use std::fmt; use std::fmt;
use std::fmt::{Display, Formatter}; use std::fmt::{Display, Formatter};
@@ -13,6 +14,8 @@ pub enum Error {
Client(&'static str), Client(&'static str),
/// IO error /// IO error
Io(io::Error), Io(io::Error),
/// JSON serialization error
JsonSerialization(serde_json::Error),
} }
impl Display for Error { impl Display for Error {
@@ -26,6 +29,7 @@ impl StdError for Error {
match *self { match *self {
Client(_) => "an unknown error occured", Client(_) => "an unknown error occured",
Io(_) => "an I/O error occured", Io(_) => "an I/O error occured",
JsonSerialization(_) => "a JSON serialization error occured",
} }
} }
@@ -43,6 +47,12 @@ impl From<io::Error> for Error {
} }
} }
impl From<serde_json::Error> for Error {
fn from(err: serde_json::Error) -> Error {
JsonSerialization(err)
}
}
impl From<&'static str> for Error { impl From<&'static str> for Error {
fn from(string: &'static str) -> Error { fn from(string: &'static str) -> Error {
Client(string) Client(string)

View File

@@ -35,7 +35,10 @@
use EmailTransport; use EmailTransport;
use SendableEmail; use SendableEmail;
use SimpleSendableEmail;
use file::error::FileResult; use file::error::FileResult;
use serde_json;
use std::fs::File; use std::fs::File;
use std::io::prelude::*; use std::io::prelude::*;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
@@ -64,17 +67,16 @@ impl EmailTransport<FileResult> for FileEmailTransport {
let mut f = try!(File::create(file.as_path())); let mut f = try!(File::create(file.as_path()));
let log_line = format!( let simple_email = SimpleSendableEmail::new(
"{}: from=<{}> to=<{}>\n", &email.from(),
email.message_id(), email.to().iter().map(String::as_str).collect(),
email.from(), &email.message_id(),
email.to().join("> to=<") &email.message(),
); );
try!(f.write_all(log_line.as_bytes())); try!(f.write_all(
try!(f.write_all(email.message().as_bytes())); serde_json::to_string(&simple_email)?.as_bytes(),
));
info!("{} status=<written>", log_line);
Ok(()) Ok(())
} }

View File

@@ -13,6 +13,10 @@ extern crate hex;
extern crate crypto; extern crate crypto;
extern crate bufstream; extern crate bufstream;
extern crate openssl; extern crate openssl;
extern crate serde_json;
extern crate serde;
#[macro_use]
extern crate serde_derive;
pub mod smtp; pub mod smtp;
pub mod sendmail; pub mod sendmail;
@@ -40,7 +44,7 @@ pub trait EmailTransport<U> {
} }
/// Minimal email structure /// Minimal email structure
#[derive(Debug, Clone)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SimpleSendableEmail { pub struct SimpleSendableEmail {
/// To /// To
to: Vec<String>, to: Vec<String>,

View File

@@ -28,11 +28,8 @@ fn file_transport() {
assert_eq!( assert_eq!(
buffer, buffer,
format!( "{\"to\":[\"root@localhost\"],\"from\":\"user@localhost\",\
"{}: from=<user@localhost> to=<root@localhost>\n{}", \"message_id\":\"file_id\",\"message\":\"Hello file\"}"
message_id,
email.message()
)
); );
remove_file(file).unwrap(); remove_file(file).unwrap();