summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPhilipp Korber <philippkorber@gmail.com>2018-04-16 11:53:24 +0200
committerPhilipp Korber <philippkorber@gmail.com>2018-04-16 11:53:24 +0200
commit36e778f76303788e87801e7ca776aa31d66db203 (patch)
treefc8e83f412251a222d173ada4741c5017c6e4d07
parentca322cccdf2f1ffa5688e63bd6f58aac199b9de7 (diff)
parent75e7a5c6bc6c93edd70a983959c3518e055aa156 (diff)
Merge branch 'smtp'
While all smtp features where moved out into their own crate, there where still some nice generall changes made when impl smtp, so the branch is still merged
-rw-r--r--Cargo.toml7
-rw-r--r--examples/composition.rs29
-rw-r--r--src/compositor/_impl.rs16
-rw-r--r--src/compositor/mail_send_data.rs16
-rw-r--r--src/compositor/mod.rs45
-rw-r--r--src/default_impl/mod.rs7
-rw-r--r--src/default_impl/simple_context.rs24
-rw-r--r--src/lib.rs10
-rw-r--r--src/macros.rs40
-rw-r--r--test_resources/template_a.out.regex2
-rw-r--r--tests/rte/main.rs2
-rw-r--r--tests/tera/main.rs2
12 files changed, 145 insertions, 55 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 499c05d..6e224b7 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -40,7 +40,6 @@ version="0.4.0"
[dev-dependencies]
serde_json = "1.0.2"
regex = "0.2.6"
-futures-cpupool="0.1.5"
[features]
debug_trace_tokens = []
@@ -48,9 +47,13 @@ render-template-engine = ["conduit-mime-types", "lazy_static"]
tera-bindings = ["tera", "render-template-engine"]
default = ["default_impl"]
default_impl = [
- "default_impl_name_composer", "default_impl_component_id",
+ "default_impl_name_composer", "default_impl_component_id", "default_impl_simple_context"
]
default_impl_name_composer = []
default_impl_component_id = []
+default_impl_simple_context = [
+ "default_impl_component_id", "mail-codec/default_impl_fs", "mail-codec/default_impl_cpupool",
+ "futures-cpupool"
+]
traceing = [ "mail-codec/traceing" ]
diff --git a/examples/composition.rs b/examples/composition.rs
index 58d5677..1286068 100644
--- a/examples/composition.rs
+++ b/examples/composition.rs
@@ -9,11 +9,9 @@ extern crate serde;
extern crate serde_json;
#[macro_use]
extern crate serde_derive;
-extern crate futures_cpupool;
use std::borrow::Cow;
-use futures_cpupool::{CpuPool, Builder as CpuPoolBuilder};
use futures::Future;
use template_engine::Teng;
@@ -22,30 +20,15 @@ use compose::error::Error;
use compose::composition_prelude::*;
use compose::resource_prelude::*;
-use compose::default_impl::{RandomContentId, NoNameComposer};
-use compose::CompositeContext;
-use mail::default_impl::FsResourceLoader;
-use mail::context::CompositeBuilderContext;
-
-type MyContext =
-CompositeContext<RandomContentId, CompositeBuilderContext<FsResourceLoader, CpuPool>>;
-
-fn setup_context() -> MyContext {
- CompositeContext::new(
- RandomContentId::new("content_id_postfix.is.this"),
- CompositeBuilderContext::new(
- FsResourceLoader::with_cwd_root().unwrap(),
- CpuPoolBuilder::new().create()
- )
- )
-}
+use compose::default_impl::simple_context;
+
fn main() {
_main().unwrap();
}
fn _main() -> Result<(), Error> {
- let context = setup_context();
+ let context = simple_context::new("blablup").unwrap();
let template_engine = Teng::new();
let data = Resorts {
@@ -63,7 +46,7 @@ fn _main() -> Result<(), Error> {
]
};
- let mut send_data = MailSendData::simple_new(
+ let send_data = MailSendData::simple_new(
Email::try_from( "my@sender.yupyup" )?.into(),
Email::try_from( "goblin@dog.spider" )?.into(),
"Dear randomness",
@@ -71,10 +54,8 @@ fn _main() -> Result<(), Error> {
data
);
- //this doesn't realy do anything as the NoNameComposer is used
- send_data.auto_gen_display_names(NoNameComposer)?;
- let mail = (&context, &template_engine).compose_mail(send_data)?;
+ let (mail, _envelop) = (&context, &template_engine).compose_mail(send_data)?;
let mut encoder = Encoder::new( MailType::Ascii );
let encodable_mail = mail.into_encodeable_mail( &context ).wait().unwrap();
diff --git a/src/compositor/_impl.rs b/src/compositor/_impl.rs
index 9e835c1..8bb83bf 100644
--- a/src/compositor/_impl.rs
+++ b/src/compositor/_impl.rs
@@ -21,7 +21,7 @@ use template::{
};
use super::mail_send_data::MailSendData;
-use super::CompositionBase;
+use super::{CompositionBase, EnvelopData};
pub(crate) trait InnerCompositionBaseExt: CompositionBase {
@@ -29,25 +29,27 @@ pub(crate) trait InnerCompositionBaseExt: CompositionBase {
&self,
send_data:
MailSendData<<Self::TemplateEngine as TemplateEngine<Self::Context>>::TemplateId, D>
- ) -> Result<Mail>
+ ) -> Result<(Mail, EnvelopData)>
where D: Serialize
{
-
+ let envelop = EnvelopData::from(&send_data);
//compose display name => create Address with display name;
let (core_headers, data, template_id) = self.process_mail_send_data(send_data)?;
let MailParts { alternative_bodies, shared_embeddings, attachments }
- = self.use_template_engine(&*template_id, data)?;
+ = self.use_template_engine(&*template_id, data)?;
+
+ let mail = self.build_mail( alternative_bodies, shared_embeddings.into_iter(),
+ attachments, core_headers )?;
- self.build_mail( alternative_bodies, shared_embeddings.into_iter(), attachments,
- core_headers )
+ Ok((mail, envelop))
}
fn process_mail_send_data<'a, D>(
&self,
send_data:
MailSendData<'a, <Self::TemplateEngine as TemplateEngine<Self::Context>>::TemplateId, D>
- ) -> Result<(
+ ) -> Result<(
HeaderMap,
D,
Cow<'a, <Self::TemplateEngine as TemplateEngine<Self::Context>>::TemplateId>
diff --git a/src/compositor/mail_send_data.rs b/src/compositor/mail_send_data.rs
index 2608e3d..9ff9d04 100644
--- a/src/compositor/mail_send_data.rs
+++ b/src/compositor/mail_send_data.rs
@@ -113,15 +113,13 @@ impl<'a, TId: ?Sized + 'a, D> MailSendData<'a, TId, D>
}
}
- pub fn sender(&self) -> Option<&Mailbox> {
- self.sender.as_ref()
+ /// returns a reference to a explicity set sender or else the first (and only) from mailbox
+ pub fn sender(&self) -> &Mailbox {
+ self.sender.as_ref().unwrap_or_else(|| self.from.first())
}
- pub fn sender_mut(&mut self) -> Option<&mut Mailbox> {
- self.sender.as_mut()
- }
- pub fn from(&self) -> &MailboxList {
+ pub fn _from(&self) -> &MailboxList {
&self.from
}
@@ -129,7 +127,7 @@ impl<'a, TId: ?Sized + 'a, D> MailSendData<'a, TId, D>
///
/// this does only expose a &mut Slice of Mailboxes, instead of a &mut MailboxList
/// to make sure that no from mailbox can be added as sender might be empty
- pub fn from_mut(&mut self) -> &mut [Mailbox] {
+ pub fn _from_mut(&mut self) -> &mut [Mailbox] {
&mut self.from
}
@@ -137,11 +135,11 @@ impl<'a, TId: ?Sized + 'a, D> MailSendData<'a, TId, D>
//TODO add try_add_from method failing if sender is None
//TODO maybe add a try_set_from(MailboxList) too
- pub fn to(&self) -> &MailboxList {
+ pub fn _to(&self) -> &MailboxList {
&self.to
}
- pub fn to_mut(&mut self) -> &mut MailboxList {
+ pub fn _to_mut(&mut self) -> &mut MailboxList {
&mut self.to
}
diff --git a/src/compositor/mod.rs b/src/compositor/mod.rs
index d1af8f4..8123f4a 100644
--- a/src/compositor/mod.rs
+++ b/src/compositor/mod.rs
@@ -1,9 +1,12 @@
+use std::borrow::ToOwned;
+
use serde::Serialize;
use error::Result;
use context::Context;
use template::TemplateEngine;
use mail::Mail;
+use mail::headers::components::{MailboxList, Mailbox};
//TODO make sure Box/Arc auto wrapping is impl for all parts
use self::_impl::InnerCompositionBaseExt;
@@ -31,9 +34,9 @@ pub trait CompositionBase {
/// composes a mail based on the given MailSendData
fn compose_mail<D>(
&self,
- send_data:
- MailSendData<<Self::TemplateEngine as TemplateEngine<Self::Context>>::TemplateId, D>
- ) -> Result<Mail>
+ send_data: MailSendData<
+ <Self::TemplateEngine as TemplateEngine<Self::Context>>::TemplateId, D>
+ ) -> Result<(Mail, EnvelopData)>
where D: Serialize
{
InnerCompositionBaseExt::_compose_mail(self, send_data)
@@ -41,4 +44,40 @@ pub trait CompositionBase {
fn template_engine(&self) -> &Self::TemplateEngine;
fn context(&self) -> &Self::Context;
+}
+
+
+//NOTE: this might get more complex at some point, wrt. e.g. cc, bcc, resent etc.
+pub struct EnvelopData {
+ sender: Mailbox,
+ to: MailboxList
+ //cc: MailboxList, //add if added to MailSendData
+ //bcc: MailboxList, //add if added to MailSendData
+}
+
+impl EnvelopData {
+
+ pub fn new(sender: Mailbox, to: MailboxList) -> Self {
+ EnvelopData {
+ sender, to
+ }
+ }
+
+ pub fn sender(&self) -> &Mailbox {
+ &self.sender
+ }
+
+ pub fn _to(&self) -> &MailboxList {
+ &self.to
+ }
+}
+
+impl<'a, T: ?Sized, D> From<&'a MailSendData<'a, T, D>> for EnvelopData
+ where T: ToOwned, D: Serialize
+{
+ fn from(msd: &'a MailSendData<'a, T, D>) -> Self {
+ let sender = msd.sender().clone();
+ let to = msd._to().clone();
+ EnvelopData::new(sender, to)
+ }
} \ No newline at end of file
diff --git a/src/default_impl/mod.rs b/src/default_impl/mod.rs
index a8cd749..8d77609 100644
--- a/src/default_impl/mod.rs
+++ b/src/default_impl/mod.rs
@@ -10,9 +10,6 @@ mod component_id;
pub use self::component_id::*;
-#[cfg(all(feature="default_impl_simple_context"))]
-mod simple_context;
-
-#[cfg(all(feature="default_impl_simple_context"))]
-pub use self::simple_context::*;
+#[cfg(feature="default_impl_simple_context")]
+pub mod simple_context;
diff --git a/src/default_impl/simple_context.rs b/src/default_impl/simple_context.rs
new file mode 100644
index 0000000..34699f0
--- /dev/null
+++ b/src/default_impl/simple_context.rs
@@ -0,0 +1,24 @@
+
+
+use futures_cpupool::{CpuPool, Builder};
+
+use context::CompositeContext;
+use default_impl::RandomContentId;
+use ::error::Error;
+use mail::context::CompositeBuilderContext;
+use mail::default_impl::FsResourceLoader;
+
+pub fn new<I: Into<String>>(content_id_postfix: I) -> Result<SimpleContext, Error> {
+ Ok(CompositeContext::new(
+ RandomContentId::new(content_id_postfix),
+ CompositeBuilderContext::new(
+ FsResourceLoader::with_cwd_root()?,
+ Builder::new().create()
+ )
+ ))
+}
+
+pub type SimpleContext = CompositeContext<RandomContentId,
+ CompositeBuilderContext<FsResourceLoader, CpuPool>>;
+
+
diff --git a/src/lib.rs b/src/lib.rs
index 4a71b9f..d449c16 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -8,6 +8,8 @@ extern crate mail_codec_headers as headers;
extern crate error_chain;
extern crate log;
extern crate mime as media_type;
+//#[cfg_attr(feature="smtp", macro_use)]
+#[macro_use]
extern crate futures;
extern crate serde;
extern crate rand;
@@ -30,6 +32,9 @@ extern crate lazy_static;
#[cfg(feature="tera-bindings")]
extern crate tera as tera_crate;
+#[macro_use]
+mod macros;
+
pub mod error;
mod builder_extension;
pub use self::builder_extension::{
@@ -40,7 +45,8 @@ mod compositor;
pub use self::compositor::{
CompositionBase, NameComposer,
MailSendData, MailSendDataBuilder,
- SharedCompositionBase, SimpleCompositionBase
+ SharedCompositionBase, SimpleCompositionBase,
+ EnvelopData
};
mod utils;
@@ -70,8 +76,6 @@ pub mod default_impl;
pub mod render_template_engine;
#[cfg(feature="tera-bindings")]
pub mod tera;
-#[cfg(feature="smtp")]
-pub mod smtp;
//################# preludes ###########################
diff --git a/src/macros.rs b/src/macros.rs
new file mode 100644
index 0000000..055a476
--- /dev/null
+++ b/src/macros.rs
@@ -0,0 +1,40 @@
+
+///// like try on a result but converts the error to a boxed error-future before returning it
+//#[cfg(feature="smtp")]
+//macro_rules! r2f_try {
+// ($code:expr) => ({
+// use futures::future;
+// match $code {
+// Ok(val) => val,
+// Err(error) => return Box::new(future::err(error))
+// }
+// });
+//}
+
+///
+/// ```
+/// cloned!([service] => move |name| {
+/// drop(service)
+/// })
+/// ```
+#[cfg(feature="smtp")]
+macro_rules! cloned {
+ ([$($toclo:ident),*] => $doit:expr) => ({
+ $(
+ let $toclo = $toclo.clone();
+ )*
+ $doit
+ });
+}
+
+/// like try_ready but for streams also checking it's some
+#[cfg(features="smtp")]
+macro_rules! try_some_ready {
+ ($e:expr) => ({
+ match $e {
+ Ok(Async::Ready(Some(item))) => item,
+ Ok(other) => return Ok(other),
+ Err(err) => return Err(From::from(err))
+ }
+ });
+} \ No newline at end of file
diff --git a/test_resources/template_a.out.regex b/test_resources/template_a.out.regex
index d4e1d3a..fccf815 100644
--- a/test_resources/template_a.out.regex
+++ b/test_resources/template_a.out.regex
@@ -4,7 +4,7 @@ Content\-Type: multipart/related;
From: <a@b\.c>
To: <d@e\.f>
Subject: Dear randomness
-Date: [[:word:]]+, [0123]?[0-9] [[:word:]]+ 20[0-9]{2} [012][0-9]:[0-6][0-9]:[0-6][0-9] \+0000
+Date: [[:word:]]+,\s+[0123]?[0-9] [[:word:]]+ 20[0-9]{2} [012][0-9]:[0-6][0-9]:[0-6][0-9] \+0000
\-\-=_(?P<boundary_0>[^"]{64})
Content\-Type: multipart/alternative;
diff --git a/tests/rte/main.rs b/tests/rte/main.rs
index a3bcb77..367154e 100644
--- a/tests/rte/main.rs
+++ b/tests/rte/main.rs
@@ -1,6 +1,8 @@
extern crate mail_codec_composition as compos;
extern crate mail_codec as mail;
+#[cfg(not(feature="render-template-engine"))]
+compile_error!("need feature \"render_template_engine\" to run tests");
use std::path::Path;
diff --git a/tests/tera/main.rs b/tests/tera/main.rs
index c525711..51aacac 100644
--- a/tests/tera/main.rs
+++ b/tests/tera/main.rs
@@ -81,7 +81,7 @@ fn use_tera_template_a() {
template_id, data
);
- let mail = compositor.compose_mail(send_data).unwrap();
+ let (mail, _envelop) = compositor.compose_mail(send_data).unwrap();
let out_string = send_mail_to_string(mail, &context);