summaryrefslogtreecommitdiffstats
path: root/core/src/context.rs
diff options
context:
space:
mode:
Diffstat (limited to 'core/src/context.rs')
-rw-r--r--core/src/context.rs207
1 files changed, 108 insertions, 99 deletions
diff --git a/core/src/context.rs b/core/src/context.rs
index 88f1045..d776da5 100644
--- a/core/src/context.rs
+++ b/core/src/context.rs
@@ -1,44 +1,46 @@
//! Provides the context needed for building/encoding mails.
-use std::sync::Arc;
use std::fmt::Debug;
+use std::sync::Arc;
-use futures::{ future::{self, Either}, Future, IntoFuture };
+use futures::{
+ future::{self, Either},
+ Future, IntoFuture,
+};
use utils::SendBoxFuture;
-use headers::header_components::{
- MessageId, ContentId
-};
+use headers::header_components::{ContentId, MessageId};
use crate::{
- resource::{Source, Data, EncData, Resource},
- error::ResourceLoadingError
+ error::ResourceLoadingError,
+ resource::{Data, EncData, Resource, Source},
};
-
/// Represents Data which might already have been transfer encoded.
pub enum MaybeEncData {
/// The data is returned normally.
Data(Data),
/// The data is returned in a already transfer encoded variant.
- EncData(EncData)
+ EncData(EncData),
}
impl MaybeEncData {
-
pub fn to_resource(self) -> Resource {
match self {
MaybeEncData::Data(data) => Resource::Data(data),
- MaybeEncData::EncData(enc_data) => Resource::EncData(enc_data)
+ MaybeEncData::EncData(enc_data) => Resource::EncData(enc_data),
}
}
- pub fn encode(self, ctx: &impl Context)
- -> impl Future<Item=EncData,Error=ResourceLoadingError>
- {
+ pub fn encode(
+ self,
+ ctx: &impl Context,
+ ) -> impl Future<Item = EncData, Error = ResourceLoadingError> {
match self {
- MaybeEncData::Data(data) => Either::A(ctx.load_transfer_encoded_resource(&Resource::Data(data))),
- MaybeEncData::EncData(enc) => Either::B(future::ok(enc))
+ MaybeEncData::Data(data) => {
+ Either::A(ctx.load_transfer_encoded_resource(&Resource::Data(data)))
+ }
+ MaybeEncData::EncData(enc) => Either::B(future::ok(enc)),
}
}
}
@@ -76,8 +78,6 @@ impl MaybeEncData {
/// implementor to have a outer+inner type where the inner type is wrapped
/// into a `Arc` e.g. `struct SomeCtx { inner: Arc<InnerSomeCtx> }`.
pub trait Context: Debug + Clone + Send + Sync + 'static {
-
-
/// Loads and transfer encodes a `Data` instance.
///
/// This is called when a `Mail` instance is converted into
@@ -98,8 +98,7 @@ pub trait Context: Debug + Clone + Send + Sync + 'static {
/// This function should not block and schedule the encoding
/// in some other place e.g. by using the contexts offload
/// functionality.
- fn load_resource(&self, source: &Source)
- -> SendBoxFuture<MaybeEncData, ResourceLoadingError>;
+ fn load_resource(&self, source: &Source) -> SendBoxFuture<MaybeEncData, ResourceLoadingError>;
/// Loads and Transfer encodes a `Resource` instance.
///
@@ -124,9 +123,10 @@ pub trait Context: Debug + Clone + Send + Sync + 'static {
/// This function should not block and schedule the encoding
/// in some other place e.g. by using the contexts offload
/// functionality.
- fn load_transfer_encoded_resource(&self, resource: &Resource)
- -> SendBoxFuture<EncData, ResourceLoadingError>
- {
+ fn load_transfer_encoded_resource(
+ &self,
+ resource: &Resource,
+ ) -> SendBoxFuture<EncData, ResourceLoadingError> {
default_impl_for_load_transfer_encoded_resource(self, resource)
}
@@ -161,60 +161,56 @@ pub trait Context: Debug + Clone + Send + Sync + 'static {
//TODO[futures/v>=0.2]: integrate this with Context
/// offloads the execution of the future `fut` to somewhere else e.g. a cpu pool
fn offload<F>(&self, fut: F) -> SendBoxFuture<F::Item, F::Error>
- where F: Future + Send + 'static,
- F::Item: Send + 'static,
- F::Error: Send + 'static;
+ where
+ F: Future + Send + 'static,
+ F::Item: Send + 'static,
+ F::Error: Send + 'static;
//TODO[futures/v>=0.2]: integrate this with Context
/// offloads the execution of the function `func` to somewhere else e.g. a cpu pool
- fn offload_fn<FN, I>(&self, func: FN ) -> SendBoxFuture<I::Item, I::Error>
- where FN: FnOnce() -> I + Send + 'static,
- I: IntoFuture + 'static,
- I::Future: Send + 'static,
- I::Item: Send + 'static,
- I::Error: Send + 'static
+ fn offload_fn<FN, I>(&self, func: FN) -> SendBoxFuture<I::Item, I::Error>
+ where
+ FN: FnOnce() -> I + Send + 'static,
+ I: IntoFuture + 'static,
+ I::Future: Send + 'static,
+ I::Item: Send + 'static,
+ I::Error: Send + 'static,
{
- self.offload( future::lazy( func ) )
+ self.offload(future::lazy(func))
}
}
-
/// Provides the default impl for the `load_transfer_encoded_resource` method of `Context`.
///
/// This function guarantees to only call `load_resource` and `offload`/`offload_fn` on the
/// passed in context, to prevent infinite recursion.
-pub fn default_impl_for_load_transfer_encoded_resource(ctx: &impl Context, resource: &Resource)
- -> SendBoxFuture<EncData, ResourceLoadingError>
-{
+pub fn default_impl_for_load_transfer_encoded_resource(
+ ctx: &impl Context,
+ resource: &Resource,
+) -> SendBoxFuture<EncData, ResourceLoadingError> {
match resource {
Resource::Source(source) => {
let ctx2 = ctx.clone();
- let fut = ctx.load_resource(&source)
- .and_then(move |me_data| {
- match me_data {
- MaybeEncData::Data(data) => {
- Either::A(ctx2.offload_fn(move || Ok(data.transfer_encode(Default::default()))))
- },
- MaybeEncData::EncData(enc_data) => {
- Either::B(future::ok(enc_data))
- }
- }
+ let fut = ctx
+ .load_resource(&source)
+ .and_then(move |me_data| match me_data {
+ MaybeEncData::Data(data) => Either::A(
+ ctx2.offload_fn(move || Ok(data.transfer_encode(Default::default()))),
+ ),
+ MaybeEncData::EncData(enc_data) => Either::B(future::ok(enc_data)),
});
Box::new(fut)
- },
+ }
Resource::Data(data) => {
let data = data.clone();
ctx.offload_fn(move || Ok(data.transfer_encode(Default::default())))
- },
- Resource::EncData(enc_data) => {
- Box::new(future::ok(enc_data.clone()))
}
+ Resource::EncData(enc_data) => Box::new(future::ok(enc_data.clone())),
}
}
/// Trait needed to be implemented for providing the resource loading parts to a`CompositeContext`.
pub trait ResourceLoaderComponent: Debug + Send + Sync + 'static {
-
/// Calls to `Context::load_resource` will be forwarded to this method.
///
/// It is the same as `Context::load_resource` except that a reference
@@ -222,8 +218,11 @@ pub trait ResourceLoaderComponent: Debug + Send + Sync + 'static {
/// infinite recursion the `Context.load_resource` method _must not_
/// be called. Additionally the `Context.load_transfer_encoded_resource` _must not_
/// be called if it uses `Context.load_resource`.
- fn load_resource(&self, source: &Source, ctx: &impl Context)
- -> SendBoxFuture<MaybeEncData, ResourceLoadingError>;
+ fn load_resource(
+ &self,
+ source: &Source,
+ ctx: &impl Context,
+ ) -> SendBoxFuture<MaybeEncData, ResourceLoadingError>;
/// Calls to `Context::transfer_encode_resource` will be forwarded to this method.
///
@@ -233,21 +232,23 @@ pub trait ResourceLoaderComponent: Debug + Send + Sync + 'static {
///
/// To prevent infinite recursion the `load_transfer_encoded_resource` method
/// of the context _must not_ be called.
- fn load_transfer_encoded_resource(&self, resource: &Resource, ctx: &impl Context)
- -> SendBoxFuture<EncData, ResourceLoadingError>
- {
+ fn load_transfer_encoded_resource(
+ &self,
+ resource: &Resource,
+ ctx: &impl Context,
+ ) -> SendBoxFuture<EncData, ResourceLoadingError> {
default_impl_for_load_transfer_encoded_resource(ctx, resource)
}
}
/// Trait needed to be implemented for providing the offloading parts to a `CompositeContext`.
pub trait OffloaderComponent: Debug + Send + Sync + 'static {
-
/// Calls to `Context::offload` and `Context::offload_fn` will be forwarded to this method.
fn offload<F>(&self, fut: F) -> SendBoxFuture<F::Item, F::Error>
- where F: Future + Send + 'static,
- F::Item: Send+'static,
- F::Error: Send+'static;
+ where
+ F: Future + Send + 'static,
+ F::Item: Send + 'static,
+ F::Error: Send + 'static;
}
/// Trait needed to be implemented for providing the id generation parts to a `CompositeContext`.
@@ -258,7 +259,6 @@ pub trait OffloaderComponent: Debug + Send + Sync + 'static {
/// more important for an message id to be "world unique" then for an content id,
/// expect in some cases where external bodies are used).
pub trait MailIdGenComponent: Debug + Send + Sync + 'static {
-
/// Calls to `Context::generate_message_id` will be forwarded to this method.
fn generate_message_id(&self) -> MessageId;
@@ -283,15 +283,16 @@ pub trait MailIdGenComponent: Debug + Send + Sync + 'static {
pub struct CompositeContext<
R: ResourceLoaderComponent,
O: OffloaderComponent,
- M: MailIdGenComponent
->{
+ M: MailIdGenComponent,
+> {
inner: Arc<(R, O, M)>,
}
impl<R, O, M> Clone for CompositeContext<R, O, M>
- where R: ResourceLoaderComponent,
- O: OffloaderComponent,
- M: MailIdGenComponent
+where
+ R: ResourceLoaderComponent,
+ O: OffloaderComponent,
+ M: MailIdGenComponent,
{
fn clone(&self) -> Self {
CompositeContext {
@@ -301,9 +302,10 @@ impl<R, O, M> Clone for CompositeContext<R, O, M>
}
impl<R, O, M> CompositeContext<R, O, M>
- where R: ResourceLoaderComponent,
- O: OffloaderComponent,
- M: MailIdGenComponent
+where
+ R: ResourceLoaderComponent,
+ O: OffloaderComponent,
+ M: MailIdGenComponent,
{
/// Create a new context from the given components.
pub fn new(resource_loader: R, offloader: O, message_id_gen: M) -> Self {
@@ -329,27 +331,28 @@ impl<R, O, M> CompositeContext<R, O, M>
}
impl<R, O, M> Context for CompositeContext<R, O, M>
- where R: ResourceLoaderComponent,
- O: OffloaderComponent,
- M: MailIdGenComponent
+where
+ R: ResourceLoaderComponent,
+ O: OffloaderComponent,
+ M: MailIdGenComponent,
{
-
- fn load_resource(&self, source: &Source)
- -> SendBoxFuture<MaybeEncData, ResourceLoadingError>
- {
+ fn load_resource(&self, source: &Source) -> SendBoxFuture<MaybeEncData, ResourceLoadingError> {
self.resource_loader().load_resource(source, self)
}
- fn load_transfer_encoded_resource(&self, resource: &Resource)
- -> SendBoxFuture<EncData, ResourceLoadingError>
- {
- self.resource_loader().load_transfer_encoded_resource(resource, self)
+ fn load_transfer_encoded_resource(
+ &self,
+ resource: &Resource,
+ ) -> SendBoxFuture<EncData, ResourceLoadingError> {
+ self.resource_loader()
+ .load_transfer_encoded_resource(resource, self)
}
fn offload<F>(&self, fut: F) -> SendBoxFuture<F::Item, F::Error>
- where F: Future + Send + 'static,
- F::Item: Send+'static,
- F::Error: Send+'static
+ where
+ F: Future + Send + 'static,
+ F::Item: Send + 'static,
+ F::Error: Send + 'static,
{
self.offloader().offload(fut)
}
@@ -361,12 +364,12 @@ impl<R, O, M> Context for CompositeContext<R, O, M>
fn generate_message_id(&self) -> MessageId {
self.id_gen().generate_message_id()
}
-
}
/// Allows using a part of an context as an component.
impl<C> MailIdGenComponent for C
- where C: Context
+where
+ C: Context,
{
fn generate_message_id(&self) -> MessageId {
<Self as Context>::generate_message_id(self)
@@ -379,12 +382,14 @@ impl<C> MailIdGenComponent for C
/// Allows using a part of an context as an component.
impl<C> OffloaderComponent for C
- where C: Context
+where
+ C: Context,
{
fn offload<F>(&self, fut: F) -> SendBoxFuture<F::Item, F::Error>
- where F: Future + Send + 'static,
- F::Item: Send+'static,
- F::Error: Send+'static
+ where
+ F: Future + Send + 'static,
+ F::Item: Send + 'static,
+ F::Error: Send + 'static,
{
<Self as Context>::offload(self, fut)
}
@@ -392,18 +397,22 @@ impl<C> OffloaderComponent for C
/// Allows using a part of an context as an component.
impl<C> ResourceLoaderComponent for C
- where C: Context
+where
+ C: Context,
{
-
- fn load_resource(&self, source: &Source, _: &impl Context)
- -> SendBoxFuture<MaybeEncData, ResourceLoadingError>
- {
+ fn load_resource(
+ &self,
+ source: &Source,
+ _: &impl Context,
+ ) -> SendBoxFuture<MaybeEncData, ResourceLoadingError> {
<Self as Context>::load_resource(self, source)
}
- fn load_transfer_encoded_resource(&self, resource: &Resource, _: &impl Context)
- -> SendBoxFuture<EncData, ResourceLoadingError>
- {
+ fn load_transfer_encoded_resource(
+ &self,
+ resource: &Resource,
+ _: &impl Context,
+ ) -> SendBoxFuture<EncData, ResourceLoadingError> {
<Self as Context>::load_transfer_encoded_resource(self, resource)
}
-} \ No newline at end of file
+}