feat(transport): Read messages from FileTransport (#516)

* feat(transport): Read messages from FileTransport

* Style improvements
This commit is contained in:
Alexis Mousset
2021-02-03 10:25:47 +01:00
committed by GitHub
parent 9d8c31bef8
commit 6fbb3bf440
2 changed files with 24 additions and 1 deletions

View File

@@ -186,6 +186,23 @@ impl FileTransport {
}
}
/// Read a message that was written using the file transport.
///
/// Reads the envelope and the raw message content.
#[cfg(feature = "file-transport-envelope")]
pub fn read(&self, email_id: &str) -> Result<(Envelope, Vec<u8>), Error> {
use std::fs;
let eml_file = self.path.join(format!("{}.eml", email_id));
let eml = fs::read(eml_file)?;
let json_file = self.path.join(format!("{}.json", email_id));
let json = fs::read(&json_file)?;
let envelope = serde_json::from_slice(&json)?;
Ok((envelope, eml))
}
fn path(&self, email_id: &Uuid, extension: &str) -> PathBuf {
self.path.join(format!("{}.{}", email_id, extension))
}

View File

@@ -81,12 +81,18 @@ mod test {
"Be happy!"
)
);
remove_file(eml_file).unwrap();
assert_eq!(
json,
"{\"forward_path\":[\"hei@domain.tld\"],\"reverse_path\":\"nobody@domain.tld\"}"
);
let (e, m) = sender.read(&id).unwrap();
assert_eq!(&e, email.envelope());
assert_eq!(m, email.formatted());
remove_file(eml_file).unwrap();
remove_file(json_file).unwrap();
}