summaryrefslogtreecommitdiffstats
path: root/src/serde_impl.rs
blob: 92825092d82e5ac0d075933b9edf41d8b78c2370 (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
use std::{
    collections::HashMap,
    sync::Arc
};

use serde::{
    Serialize, Deserialize,
    de::{
        Deserializer,
    },
};
use failure::Error;
use vec1::Vec1;

use mail_core::{Resource, Source, IRI};

use super::{
    Template,
    TemplateEngine,
    CwdBaseDir,
    PathRebaseable,
    InnerTemplate,
    Subject,
};

/// Type used when deserializing a template using serde.
///
/// This type should only be used as intermediate type
/// used for deserialization as templates need to be
/// bundled with a template engine.
///
/// # Serialize/Deserialize
///
/// The derserialization currently only works with
/// self-describing data formats.
///
/// There are a number of shortcuts to deserialize
/// resources (from emebddings and/or attachments):
///
/// - Resources can be deserialized normally (from a externally tagged enum)
/// - Resources can be deserialized from the serialized repr of `Source`
/// - Resources can be deserialized from a string which is used to create
///   a `Resource::Source` with a iri using the `path` scheme and the string
///   content as the iris "tail".
#[derive(Debug, Serialize, Deserialize)]
pub struct TemplateBase<TE: TemplateEngine> {
    #[serde(rename="name")]
    template_name: String,
    base_dir: CwdBaseDir,
    subject: LazySubject,
    bodies: Vec1<TE::LazyBodyTemplate>,
    //TODO impl. deserialize where
    // resource:String -> IRI::new("path", resource) -> Resource::Source
    #[serde(deserialize_with="deserialize_embeddings")]
    embeddings: HashMap<String, Resource>,
    #[serde(deserialize_with="deserialize_attachments")]
    attachments: Vec<Resource>,
}

impl<TE> TemplateBase<TE>
    where TE: TemplateEngine
{

    //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> {
        let TemplateBase {
            template_name,
            base_dir,
            subject,
            bodies,
            mut embeddings,
            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)?;
        }


        let inner = InnerTemplate {
            template_name,
            base_dir,
            subject,
            bodies,
            embeddings,
            attachments,
            engine
        };

        Ok(Template { inner: Arc::new(inner) })
    }
}

#[derive(Debug, Serialize, Deserialize)]
struct LazySubject {
    #[serde(flatten)]
    template_string: String
}

#[derive(Deserialize)]
#[serde(untagged)]
enum ResourceDeserializationHelper {
    // Note: VARIANT ORDER MATTERS (serde,untagged)
    // This allows specifying resources in three ways.
    // 1. as tagged enum `Resource` (e.g. `{"Source": { "iri": ...}}}`)
    // 2. as struct `Source` (e.g. `{"iri": ...}` )
    // 3. as String which is interpreted as path iri
    Normal(Resource),
    FromSource(Source),
    FromString(String)
}

impl Into<Resource> for ResourceDeserializationHelper {
    fn into(self) -> Resource {
        use self::ResourceDeserializationHelper::*;
        match self {
            Normal(resource) => resource,
            FromString(string) => {
                let source = Source {
                    //UNWRAP_SAFE: only scheme validation could fail,
                    // but its static "path" which is known to be valid
                    iri: IRI::from_parts("path", &string).unwrap(),
                    use_media_type: Default::default(),
                    use_file_name: Default::default()
                };

                Resource::Source(source)
            },
            FromSource(source) => Resource::Source(source)
        }
    }
}

fn deserialize_embeddings<'de, D>(deserializer: D)
    -> Result<HashMap<String, Resource>, D::Error>
    where D: Deserializer<'de>
{
    //FIXME[perf] write custom visitor etc.
    let map = <HashMap<String, ResourceDeserializationHelper>>
        ::deserialize(deserializer)?;

    let map = map.into_iter()
        .map(|(k, helper)| (k, helper.into()))
        .collect();

    Ok(map)
}

fn deserialize_attachments<'de, D>(deserializer: D)
    -> Result<Vec<Resource>, D::Error>
    where D: Deserializer<'de>
{
    //FIXME[perf] write custom visitor etc.
    let vec = <Vec<ResourceDeserializationHelper>>
        ::deserialize(deserializer)?;

    let vec = vec.into_iter()
        .map(|helper| helper.into())
        .collect();

    Ok(vec)
}