summaryrefslogtreecommitdiffstats
path: root/src/resource/mod.rs
blob: 3f16a967487802ee51c9ffc9e5ce066bff7125e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
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>,
    resource: Resource,
    disposition: Disposition,
}

impl Embedded {
    pub fn inline(resource: Resource) -> Self {
        Embedded::new(resource, Disposition::Inline)
    }

    pub fn attachment(resource: Resource) -> Self {
        Embedded::new(resource, Disposition::Attachment)
    }

    pub fn new(resource: Resource, disposition: Disposition) -> Self {
        Embedded {
            content_id: None,
            resource,
            disposition
        }
    }

    pub fn with_content_id(resource: Resource, disposition: Disposition, content_id: ContentId) -> Self {
        Embedded {
            content_id: Some(content_id),
            resource,
            disposition
        }
    }

    pub fn resource(&self) -> &Resource {
        &self.resource
    }

    pub fn resource_mut(&mut self) -> &mut Resource {
        &mut self.resource
    }

    pub fn content_id(&self) -> Option<&ContentId> {
        self.content_id.as_ref()
    }

    pub fn disposition(&self) -> Disposition {
        self.disposition
    }

    pub fn assure_content_id(&mut self, ctx: &impl Context) -> &ContentId {
        if self.content_id.is_none() {
            self.content_id = Some(ctx.generate_content_id());
        }

        self.content_id().unwrap()
    }

    pub fn assure_content_id_and_copy(&mut self, ctx: &impl Context) -> EmbeddedWithCId {
        self.assure_content_id(ctx);
        EmbeddedWithCId { inner: self.clone() }
    }
}

#[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"))
        }
    }
}

/// This trait is used to iterate over all `Embedded` instances of "arbitrary" data.
///
/// This functionality is needed so that the Content-Id's for embedded resources
/// and attachments can be retrieved and generated for any kind of data a user might
/// pass to any kind of template engine.
///
/// This trait is implemented for many types from `std`, this include two kind
/// of implementations:
///
/// 1. such for containers e.g. `Vec<T> where T: InspectEmbeddedResources`
/// 2. such for values which definitely can _not_ contain a `Embedded` type,
///    e.g. u32 or String (currently not all types of `std` for which this is
///    the case have this "empty" implementation, more can/will be added)
///
/// For types which can contain a `Embedded` but accessing it `&mut` might
/// not be possible not implementation is provided intentionally. This includes
/// `Rc<T>`, `Mutex<T>` (might be locked/poisoned), etc. When specialization
/// is stable this might be possible extended to include more cases where
/// a only a "empty" implementation makes sense e.g. `Rc<u32>`.
///
/// (note a "empty" implementation simple does nothing when called, which
///  is fine and more or less like not calling it at all in this case)
///
/// # Derive
///
/// There is a custom derive for this type which can
/// be used as shown in the example below. It tries
/// to call the `inspect_resources*` methods on every
/// field and considers following (field) attributes:
///
/// - `#[mail(inspect_skip)]`
///   skip field, does not call `inspect_resource*` on it
/// - `#[mail(inspect_with="(some::path, another::path_mut)")]`
///   use the functions `some::path` and `another::path_mut` to inspect
///   the field (using the former for `inspect_resources` and the later
///   for `inspect_resources_mut`)
///
/// The derive works for struct and enum types in all variations but not
/// unions.
///
/// ```
/// # #[macro_use]
/// # extern crate mail_template;
/// # use std::sync::{Mutex, Arc};
///
/// // due to some current limitations of rust the macro
/// // can not import the types, so we need to do so
/// use mail_template::{Embedded, InspectEmbeddedResources};
///
/// struct Bloop {
///     //...
/// # bla: u32
/// }
///
/// #[derive(InspectEmbeddedResources)]
/// struct Foo {
///     // has an "empty" implementation
///     field1: u32,
///
///     // skips this field
///     #[mail(inspect_skip)]
///     field2: Bloop,
///
///     // inspects this field
///     dings: Vec<Embedded>,
///
///     // custom inspection handling (2 pathes to functions)
///     #[mail(inspect_with="(inspect_mutex, inspect_mutex_mut)")]
///     shared_dings: Arc<Mutex<Embedded>>
/// }
///
/// // due too auto-deref we could use `me: &Mutex<Embedded>`
/// fn inspect_mutex(me: &Arc<Mutex<Embedded>>, visitor: &mut FnMut(&Embedded)) {
///     let embedded = me.lock().unwrap();
///     visitor(&*embedded);
/// }
///
/// fn inspect_mutex_mut(me: &mut Arc<Mutex<Embedded>>, visitor: &mut FnMut(&mut Embedded)) {
///     let mut embedded = me.lock().unwrap();
///     visitor(&mut *embedded);
/// }
///
/// # fn main() {}
/// ```
pub trait InspectEmbeddedResources {
    fn inspect_resources(&self, visitor: &mut FnMut(&Embedded));
    fn inspect_resources_mut(&mut self, visitor: &mut FnMut(&mut Embedded));
}


impl InspectEmbeddedResources for Embedded {
    fn inspect_resources(&self, visitor: &mut FnMut(&Embedded)) {
        visitor(self)
    }
    fn inspect_resources_mut(&mut self, visitor: &mut FnMut(&mut Embedded)) {
        visitor(self)
    }
}

impl Into<Resource> for Embedded {
    fn into(self) -> Resource {
        let Embedded { content_id:_, resource, disposition:_ } = self;
        resource
    }
}


/// # 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
}

impl Deref for EmbeddedWithCId {
    type Target = Embedded;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl EmbeddedWithCId {

    /// create a new embedding with an inline disposition
    ///
    /// The context is used to generate a fitting content id.
    pub fn inline(resource: Resource, ctx: &impl Context) -> Self {
        EmbeddedWithCId::new(resource, Disposition::Inline, ctx)
    }

    /// create a new embedding with an attachment disposition
    ///
    /// The context is used to generate a fitting content id.
    pub fn attachment(resource: Resource, ctx: &impl Context) -> Self {
        EmbeddedWithCId::new(resource, Disposition::Attachment, ctx)
    }

    /// create a new embedding
    ///
    /// The context is used to generate a fitting content id.
    pub fn new(resource: Resource, disposition: Disposition, ctx: &impl Context) -> Self {
        EmbeddedWithCId {
            inner: Embedded::with_content_id(resource, disposition, ctx.generate_content_id())
        }
    }

    /// Tries to convert an `Embedded` instance to an `EmbeddedWithCId` instance.
    ///
    /// # Error
    ///
    /// If the `Embedded` instance doesn't have a content id the passed in
    /// `Embedded` instance is returned as error.
    pub fn try_from(emb: Embedded) -> Result<EmbeddedWithCId, Embedded> {
        if emb.content_id().is_some() {
            Ok(EmbeddedWithCId { inner: emb })
        } else {
            Err(emb)
        }
    }

    /// return the content id
    pub fn content_id(&self) -> &ContentId {
        self.inner.content_id().unwrap()
    }
}

#[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;
        let Embedded { content_id:_, resource, disposition:_ } = inner;
        resource
    }
}

impl Into<(ContentId, Resource)> for EmbeddedWithCId {

    fn into(self) -> (ContentId, Resource) {
        let EmbeddedWithCId { inner } = self;
        let Embedded { content_id, resource, disposition:_ } = inner;
        (content_id.unwrap(), resource)
    }
}




#[cfg(test)]
mod test {
    use soft_ascii_string::SoftAsciiString;
    use mail::{Context, Resource};
    use mail::default_impl::simple_context;
    use headers::components::{ContentId, Domain};
    use headers::HeaderTryFrom;

    use ::resource::Disposition;

    fn any_resource() -> Resource {
        Resource::sourceless_from_string("abc")
    }

    fn ctx() -> impl Context {
        simple_context::new(
            Domain::try_from("hy.test").unwrap(),
            SoftAsciiString::from_unchecked("9ddqdq")
        ).unwrap()
    }

    mod Embedded {
        #![allow(non_snake_case)]

        use super::*;
        use super::super::Embedded;

        #[test]
        fn inline_uses_disposition_inline() {
            let emb = Embedded::inline(any_resource());

            assert_eq!(emb.disposition(), Disposition::Inline)
        }

        #[test]
        fn attachment_uses_disposition_attachment() {
            let emb = Embedded::attachment(any_resource());