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
//! Functions and types for source tree transformations.

use ast::*;
use error::*;

/// Transformation result type
pub type TResult = Result<Element, TransformationError>;

/// Result type for a list of transformed elements.
pub type TListResult = Result<Vec<Element>, TransformationError>;

/// Signature of an in-place transformation function
pub type TFuncInplace<S> = Fn(Element, S) -> TResult;

/// Signature of a cloning transformation function
pub type TFunc<S> = Fn(&Element, &[&Element], S) -> TResult;


/// Apply a given transformation function to a list of elements, without mutating the original.
pub fn apply_func_clone<S: Copy>(
    func: &TFunc<S>,
    content: &[Element],
    path: &[&Element],
    settings: S,
) -> TListResult {
    let mut result = vec![];
    for child in content {
        result.push(func(child, path, settings)?);
    }
    Ok(result)
}

/// Apply a given transformation to every item in a list, consuming this list.
pub fn apply_func_drain<S: Copy>(
    func: &TFuncInplace<S>,
    content: &mut Vec<Element>,
    settings: S,
) -> TListResult {
    let mut result = vec![];
    for child in content.drain(..) {
        result.push(func(child, settings)?);
    }
    Ok(result)
}

/// Recursively apply a transformation function `func` to all children of element `root`.
pub fn recurse_inplace<S: Copy>(func: &TFuncInplace<S>, root: Element, settings: S) -> TResult {
    recurse_inplace_template(func, root, settings, &apply_func_drain)
}

/// Recursively apply  a function `content_func` to the children list of a node.
pub fn recurse_inplace_template<S: Copy>(
    func: &TFuncInplace<S>,
    mut root: Element,
    settings: S,
    content_func: &Fn(&TFuncInplace<S>, &mut Vec<Element>, S) -> TListResult,
) -> TResult {

    match root {
        Element::Document(ref mut e) => {
            let mut temp = content_func(func, &mut e.content, settings)?;
            e.content.append(&mut temp);
        },
        Element::Formatted(ref mut e) => {
            let mut temp = content_func(func, &mut e.content, settings)?;
            e.content.append(&mut temp);
        },
        Element::Paragraph(ref mut e) => {
            let mut temp = content_func(func, &mut e.content, settings)?;
            e.content.append(&mut temp);
        },
        Element::ListItem(ref mut e) => {
            let mut temp = content_func(func, &mut e.content, settings)?;
            e.content.append(&mut temp);
        },
        Element::List(ref mut e) => {
            let mut temp = content_func(func, &mut e.content, settings)?;
            e.content.append(&mut temp);
        },
        Element::TableCell(ref mut e) => {
            let mut temp = content_func(func, &mut e.content, settings)?;
            e.content.append(&mut temp);
        },
        Element::HtmlTag(ref mut e) => {
            let mut temp = content_func(func, &mut e.content, settings)?;
            e.content.append(&mut temp);
        },
        Element::Gallery(ref mut e) => {
            let mut temp = content_func(func, &mut e.content, settings)?;
            e.content.append(&mut temp);
        },
        Element::Heading(ref mut e) => {
            let mut content = content_func(func, &mut e.content, settings)?;
            let mut caption = content_func(func, &mut e.caption, settings)?;
            e.caption.append(&mut caption);
            e.content.append(&mut content);
        },
        Element::Template(ref mut e) => {
            let mut name = content_func(func, &mut e.name, settings)?;
            let mut content = content_func(func, &mut e.content, settings)?;
            e.name.append(&mut name);
            e.content.append(&mut content);
        },
        Element::TemplateArgument(ref mut e) => {
            let mut value = content_func(func, &mut e.value, settings)?;
            e.value.append(&mut value);
        },
        Element::InternalReference(ref mut e) => {
            let mut target = content_func(func, &mut e.target, settings)?;
            let mut caption = content_func(func, &mut e.caption, settings)?;

            let mut new_options = vec![];
            for mut option in e.options.drain(..) {
                new_options.push(content_func(func, &mut option, settings)?);
            }

            e.target.append(&mut target);
            e.options.append(&mut new_options);
            e.caption.append(&mut caption);
        },
        Element::ExternalReference(ref mut e) => {
            let mut caption = content_func(func, &mut e.caption, settings)?;
            e.caption.append(&mut caption);
        },
        Element::Table(ref mut e) => {
            let mut caption = content_func(func, &mut e.caption, settings)?;
            let mut rows = content_func(func, &mut e.rows, settings)?;
            e.caption.append(&mut caption);
            e.rows.append(&mut rows);
        },
        Element::TableRow(ref mut e) => {
            let mut cells = content_func(func, &mut e.cells, settings)?;
            e.cells.append(&mut cells);
        }
        Element::Text(_)
        | Element::Comment(_)
        | Element::Error(_)
        => (),
    };
    Ok(root)
}

/// Recursively apply a transformation function `func` to all children of element `root`, cloning the input.
pub fn recurse_clone<S: Copy>(
    func: &TFunc<S>,
    root: &Element,
    path: &[&Element],
    settings: S,
) -> TResult {

    recurse_clone_template(func, root, path, settings, &apply_func_clone)
}


/// Recursively apply  a function `content_func` to the children list of a node, cloning the input.
pub fn recurse_clone_template<S: Copy>(
    func: &TFunc<S>,
    root: &Element,
    path: &[&Element],
    settings: S,
    content_func: &Fn(&TFunc<S>, &[Element], &[&Element], S) -> TListResult,
) -> TResult {

    let mut path = path.to_owned();
    path.push(root);
    let new = match *root {
        Element::Document(ref e) => {
            Element::Document(Document {
                position: e.position.clone(),
                content: content_func(func, &e.content, &path, settings)?,
            })
        },
        Element::Heading(ref e) => {
            Element::Heading(Heading {
                position: e.position.clone(),
                depth: e.depth,
                caption: content_func(func, &e.caption, &path, settings)?,
                content: content_func(func, &e.content, &path, settings)?,
            })
        },
        Element::Formatted(ref e) => {
            Element::Formatted(Formatted {
                position: e.position.clone(),
                markup: e.markup,
                content: content_func(func, &e.content, &path, settings)?,
            })
        },
        Element::Paragraph(ref e) => {
            Element::Paragraph(Paragraph {
                position: e.position.clone(),
                content: content_func(func, &e.content, &path, settings)?,
            })
        },
        Element::Template(ref e) => {
            Element::Template(Template {
                position: e.position.clone(),
                name: content_func(func, &e.name, &path, settings)?,
                content: content_func(func, &e.content, &path, settings)?,
            })
        },
        Element::TemplateArgument(ref e) => {
            Element::TemplateArgument(TemplateArgument {
                position: e.position.clone(),
                name: e.name.clone(),
                value: content_func(func, &e.value, &path, settings)?,
            })
        },
        Element::InternalReference(ref e) => {
            let mut new_options = vec![];
            for option in &e.options {
                new_options.push(content_func(func, &option, &path, settings)?);
            }

            Element::InternalReference(InternalReference {
                position: e.position.clone(),
                target: content_func(func, &e.target, &path, settings)?,
                options: new_options,
                caption: content_func(func, &e.caption, &path, settings)?,
            })
        },
        Element::ExternalReference(ref e) => {
            Element::ExternalReference(ExternalReference {
                position: e.position.clone(),
                target: e.target.clone(),
                caption: content_func(func, &e.caption, &path, settings)?,
            })
        },
        Element::ListItem(ref e) => {
            Element::ListItem(ListItem {
                position: e.position.clone(),
                depth: e.depth,
                kind: e.kind,
                content: content_func(func, &e.content, &path, settings)?,
            })
        },
        Element::List(ref e) => {
            Element::List(List {
                position: e.position.clone(),
                content: content_func(func, &e.content, &path, settings)?,
            })
        },
        Element::Table(ref e) => {
            Element::Table(Table {
                position: e.position.clone(),
                attributes: e.attributes.clone(),
                caption: content_func(func, &e.caption, &path, settings)?,
                caption_attributes: e.caption_attributes.clone(),
                rows: content_func(func, &e.rows, &path, settings)?,
            })
        },
        Element::TableRow(ref e) => {
            Element::TableRow(TableRow {
                position: e.position.clone(),
                attributes: e.attributes.clone(),
                cells: content_func(func, &e.cells, &path, settings)?,
            })
        },
        Element::TableCell(ref e) => {
            Element::TableCell(TableCell {
                position: e.position.clone(),
                header: e.header,
                attributes: e.attributes.clone(),
                content: content_func(func, &e.content, &path, settings)?,
            })
        },
        Element::Comment(ref e) => Element::Comment(e.clone()),
        Element::Text(ref e) => Element::Text(e.clone()),
        Element::Error(ref e) => Element::Error(e.clone()),
        Element::HtmlTag(ref e) => {
            Element::HtmlTag(HtmlTag {
                position: e.position.clone(),
                name: e.name.clone(),
                attributes: e.attributes.clone(),
                content: content_func(func, &e.content, &path, settings)?,
            })
        },
        Element::Gallery(ref e) => {
            Element::Gallery(Gallery {
                position: e.position.clone(),
                attributes: e.attributes.clone(),
                content: content_func(func, &e.content, &path, settings)?,
            })
        },
    };
    path.pop();
    Ok(new)
}