diff options
author | Philipp Korber <p.korber@1aim.com> | 2018-11-19 20:39:16 +0100 |
---|---|---|
committer | Philipp Korber <p.korber@1aim.com> | 2018-11-19 20:39:16 +0100 |
commit | d750524ad8094fcd6745ddbefaacb694d7c77178 (patch) | |
tree | 390c706a0e6bc7dac13ec122cc9539f8db8068b1 | |
parent | 43e697b166a0d7bbdab3ca6177357a1402c2c685 (diff) |
chore(new-api) impl loading of resources when loading template
-rw-r--r-- | src/lib.rs | 45 | ||||
-rw-r--r-- | src/serde_impl.rs | 75 |
2 files changed, 79 insertions, 41 deletions
@@ -8,11 +8,10 @@ extern crate vec1; extern crate toml; use std::{ - fs, collections::HashMap, fmt::Debug, - path::Path, + path::{PathBuf}, sync::Arc }; @@ -25,7 +24,7 @@ use failure::{Fail, Error}; use futures::{ Future, Poll, Async, try_ready, - future::Join + future::{self, Join, Either} }; use vec1::Vec1; @@ -55,25 +54,45 @@ pub trait TemplateEngine: Sized { type Id: Debug; type Error: Fail; - type LazyBodyTemplate: PathRebaseable + Debug + Serialize + for<'a> Deserialize<'a>; + type LazyBodyTemplate: PathRebaseable + Debug + Send + Serialize + for<'a> Deserialize<'a>; fn load_body_template(&mut self, tmpl: Self::LazyBodyTemplate) -> Result<BodyTemplate<Self>, Error>; fn load_subject_template(&mut self, template_string: String) -> Result<Self::Id, Error>; +} - fn load_toml_template_from_path<P>(self, path: P, ctx: &impl Context) -> Result<Template<Self>, Error> - where P: AsRef<Path> - { +pub fn load_toml_template_from_path<TE, C>( + engine: TE, + path: PathBuf, + ctx: &C +) -> impl Future<Item=Template<TE>, Error=Error> + where TE: TemplateEngine + 'static, C: Context +{ + + let ctx2 = ctx.clone(); + ctx.offload_fn(move || { let content = fs::read_to_string(path)?; - self.load_toml_template_from_str(&content, ctx) - } + let base: serde_impl::TemplateBase<TE> = toml::from_str(&content)?; + Ok(base) + }).and_then(move |base| base.load(engine, &ctx2)) +} - fn load_toml_template_from_str(self, desc: &str, ctx: &impl Context) -> Result<Template<Self>, Error> { - let base: serde_impl::TemplateBase<Self> = toml::from_str(desc)?; - Ok(base.load(self)?) - } +pub fn load_toml_template_from_str<TE, C>( + engine: TE, + content: &str, + ctx: &C +) -> impl Future<Item=Template<TE>, Error=Error> + where TE: TemplateEngine, C: Context +{ + let base: serde_impl::TemplateBase<TE> = + match toml::from_str(content) { + Ok(base) => base, + Err(err) => { return Either::B(future::err(Error::from(err))); } + }; + + Either::A(base.load(engine, ctx)) } pub struct PreparationData<'a, PD: for<'any> BoundExt<'any>> { diff --git a/src/serde_impl.rs b/src/serde_impl.rs index 9282509..75b2095 100644 --- a/src/serde_impl.rs +++ b/src/serde_impl.rs @@ -10,9 +10,10 @@ use serde::{ }, }; use failure::Error; +use futures::{Future, future::{self, Either}}; use vec1::Vec1; -use mail_core::{Resource, Source, IRI}; +use mail_core::{Resource, Source, IRI, Context}; use super::{ Template, @@ -63,7 +64,7 @@ impl<TE> TemplateBase<TE> //TODO!! make this load all embeddings/attachments and make it a future /// Couples the template base with a specific engine instance. - pub fn load(self, mut engine: TE) -> Result<Template<TE>, Error> { + pub fn load(self, mut engine: TE, ctx: &impl Context) -> impl Future<Item=Template<TE>, Error=Error> { let TemplateBase { template_name, base_dir, @@ -73,34 +74,52 @@ impl<TE> TemplateBase<TE> mut attachments } = self; - let subject = Subject{ template_id: engine.load_subject_template(subject.template_string)? }; - - let bodies = bodies.try_mapped(|mut lazy_body| -> Result<_, Error> { - lazy_body.rebase_to_include_base_dir(&base_dir)?; - Ok(engine.load_body_template(lazy_body)?) - })?; - - - for embedding in embeddings.values_mut() { - embedding.rebase_to_include_base_dir(&base_dir)?; - } - - for attachment in attachments.iter_mut() { - attachment.rebase_to_include_base_dir(&base_dir)?; - } - + //FIXME[rust/catch block] use catch block + let catch_res = (|| -> Result<_, Error> { + let subject = Subject{ template_id: engine.load_subject_template(subject.template_string)? }; + + let bodies = bodies.try_mapped(|mut lazy_body| -> Result<_, Error> { + lazy_body.rebase_to_include_base_dir(&base_dir)?; + Ok(engine.load_body_template(lazy_body)?) + })?; + + for embedding in embeddings.values_mut() { + embedding.rebase_to_include_base_dir(&base_dir)?; + } + + for attachment in attachments.iter_mut() { + attachment.rebase_to_include_base_dir(&base_dir)?; + } + + Ok((subject, bodies)) + })(); + + let (subject, bodies) = + match catch_res { + Ok(vals) => vals, + Err(err) => { return Either::B(future::err(err)); } + }; + + let loading_fut = Resource::load_container(embeddings, ctx) + .join(Resource::load_container(attachments, ctx)); + + let fut = loading_fut + .map_err(Error::from) + .map(|(embeddings, attachments)| { + let inner = InnerTemplate { + template_name, + base_dir, + subject, + bodies, + embeddings, + attachments, + engine + }; - let inner = InnerTemplate { - template_name, - base_dir, - subject, - bodies, - embeddings, - attachments, - engine - }; + Template { inner: Arc::new(inner) } + }); - Ok(Template { inner: Arc::new(inner) }) + Either::A(fut) } } |