feat(transport): Use serde to serialize email in the file transport
This commit is contained in:
@@ -4,7 +4,7 @@ rust:
|
||||
- stable
|
||||
- beta
|
||||
- nightly
|
||||
- 1.13.0
|
||||
- 1.15.0
|
||||
|
||||
matrix:
|
||||
allow_failures:
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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<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 {
|
||||
fn from(string: &'static str) -> Error {
|
||||
Client(string)
|
||||
|
||||
@@ -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<FileResult> 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=<written>", log_line);
|
||||
try!(f.write_all(
|
||||
serde_json::to_string(&simple_email)?.as_bytes(),
|
||||
));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -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<U> {
|
||||
}
|
||||
|
||||
/// Minimal email structure
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct SimpleSendableEmail {
|
||||
/// To
|
||||
to: Vec<String>,
|
||||
|
||||
@@ -28,11 +28,8 @@ fn file_transport() {
|
||||
|
||||
assert_eq!(
|
||||
buffer,
|
||||
format!(
|
||||
"{}: from=<user@localhost> to=<root@localhost>\n{}",
|
||||
message_id,
|
||||
email.message()
|
||||
)
|
||||
"{\"to\":[\"root@localhost\"],\"from\":\"user@localhost\",\
|
||||
\"message_id\":\"file_id\",\"message\":\"Hello file\"}"
|
||||
);
|
||||
|
||||
remove_file(file).unwrap();
|
||||
|
||||
Reference in New Issue
Block a user