summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPhilipp Korber <philippkorber@gmail.com>2018-05-31 19:15:07 +0200
committerPhilipp Korber <philippkorber@gmail.com>2018-05-31 19:15:07 +0200
commita88b6f6912d686f4f9cae987bf1a0bc3008eaec2 (patch)
treea5fd522f0d5018fcaab34d9fbf55a9e4f4e4f8c9
parentcbdb37ade79b87cd7bdcd7047cb66933cec4b33c (diff)
chore(Embedded): impl Serialize to content id for it
- needed for template engines using serialization for data access
-rw-r--r--Cargo.toml4
-rw-r--r--src/lib.rs3
-rw-r--r--src/resource/mod.rs41
3 files changed, 47 insertions, 1 deletions
diff --git a/Cargo.toml b/Cargo.toml
index f6d63a6..605e1a7 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -21,6 +21,7 @@ failure = "0.1.1"
futures = "0.1.14"
vec1 = { git="https://github.com/1aim/vec1" }
soft-ascii-string = "0.2.0"
+serde = { version="1.0.64", optional=true }
@@ -37,4 +38,5 @@ optional = true
[features]
default = []
-askama-engine = ["askama"] \ No newline at end of file
+askama-engine = ["askama"]
+serialize-content-id = ["serde"] \ No newline at end of file
diff --git a/src/lib.rs b/src/lib.rs
index 763eb10..6d4f430 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -11,6 +11,9 @@ extern crate soft_ascii_string;
#[macro_use]
extern crate vec1;
+#[cfg(feature="serialize-content-id")]
+extern crate serde;
+
#[cfg(feature="askama-engine")]
#[cfg_attr(test, macro_use)]
extern crate askama;
diff --git a/src/resource/mod.rs b/src/resource/mod.rs
index 38e7123..9bcd090 100644
--- a/src/resource/mod.rs
+++ b/src/resource/mod.rs
@@ -3,11 +3,22 @@ use std::ops::Deref;
use mail::Context;
use headers::components::ContentId;
use mail::Resource;
+#[cfg(feature="serialize-content-id")]
+use serde::{Serialize, Serializer, ser};
pub use headers::components::DispositionKind as Disposition;
mod impl_inspect;
+/// # Serialize (feature `serialize-content-id`)
+///
+/// If serialized this struct **turns into it's
+/// content id failing if it has no content id**.
+///
+/// Normally this struct would not be serializeable
+/// (Resource isn't) but for template engines which
+/// use serialization for data access serializing it
+/// to it's content id string is quite use full
#[derive(Debug, Clone)]
pub struct Embedded {
content_id: Option<ContentId>,
@@ -70,6 +81,19 @@ impl Embedded {
}
}
+#[cfg(feature="serialize-content-id")]
+impl<'a> Serialize for Embedded {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where S: Serializer
+ {
+ if let Some(cid) = self.content_id() {
+ cid.serialize(serializer)
+ } else {
+ Err(ser::Error::custom("can not serialize Embedded without content id"))
+ }
+ }
+}
+
pub trait InspectEmbeddedResources {
fn inspect_resources(&self, visitor: &mut FnMut(&Embedded));
fn inspect_resources_mut(&mut self, visitor: &mut FnMut(&mut Embedded));
@@ -93,6 +117,15 @@ impl Into<Resource> for Embedded {
}
+/// # Serialize (feature `serialize-content-id`)
+///
+/// If serialized this struct **turns into it's
+/// content id**.
+///
+/// Normally this struct would not be serializeable
+/// (Resource isn't) but for template engines which
+/// use serialization for data access serializing it
+/// to it's content id string is quite use full
#[derive(Debug, Clone)]
pub struct EmbeddedWithCId {
inner: Embedded
@@ -138,6 +171,14 @@ impl EmbeddedWithCId {
}
}
+#[cfg(feature="serialize-content-id")]
+impl<'a> Serialize for EmbeddedWithCId {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where S: Serializer
+ {
+ self.content_id().serialize(serializer)
+ }
+}
impl Into<Resource> for EmbeddedWithCId {
fn into(self) -> Resource {
let EmbeddedWithCId { inner } = self;