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
use std::ops::Range;
use bytes::Bytes;



const FRAGMENT_START: &[u8] = b"{{: ";
const FRAGMENT_END: &[u8] = b" :}}";

/// Fragments longer than this are ignored. This is just to protect against
/// random fragment start/end markers in large generated files.
pub const MAX_FRAGMENT_LEN: usize = 256;


/// A byte string that you can append to. Used for [`Template::render`].
pub struct Appender<'a>(&'a mut Vec<u8>);

impl Appender<'_> {
    pub fn append(&mut self, s: &[u8]) {
        self.0.extend_from_slice(s);
    }
}

/// A parsed template.
///
/// # Template syntax
///
/// Our template syntax is super simple and is really just a glorified
/// search-and-replace. The input is checked for "fragments" which have the
/// syntax `{{: foo :}}`. The start token is actually `{{: ` (note the
/// whitespace!). So `{{:foo:}}` is not recognized as fragment.
///
/// There are two additional constraints: the fragment must not contain a
/// newline and must be shorter than [`MAX_FRAGMENT_LEN`]. If these conditions
/// are not met, the fragment start token is ignored.
///
/// The string between the start and end tag is then trimmed (excess whitespace
/// removed) and parsed into a [`Fragment`]. See that type's documentation for
/// information about the existing kinds of fragments.
#[derive(Debug)]
pub struct Template {
    raw: Bytes,
    fragments: Vec<SpannedFragment>,
}

/// Error returned by the parsing functions.
#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("template fragment does not contain valid UTF8: {0:?}")]
    NonUtf8TemplateFragment(Vec<u8>),
    #[error("unknown template fragment specifier '{0}'")]
    UnknownTemplateSpecifier(String),
}

/// A fragment with a span.
#[derive(Debug)]
struct SpannedFragment {
    span: Range<usize>,
    kind: Fragment,
}

/// A parsed template fragment.
#[derive(Debug)]
pub enum Fragment {
    // TODO: one could avoid allocating those `String`s by storing `Byte`s
    // instead. However, this would add more UTF8 checks and/or unsafe blocks.
    // Not worth it for now.

    /// Inserts the public path of another asset. Example:
    /// `{{: path:bundle.js :}}`.
    Path(String),

    /// Includes another asset. Example: `{{: include:fonts.css :}}`.
    Include(String),

    /// Interpolates a runtime variable. Example: `{{: var:name :}}`.
    Var(String),
}

impl Fragment {
    fn parse(bytes: &[u8]) -> Result<Self, Error> {
        let val = |s: &str| s[s.find(':').unwrap() + 1..].to_string();

        let s = std::str::from_utf8(bytes)
            .map_err(|_| Error::NonUtf8TemplateFragment(bytes.into()))?
            .trim();

        match () {
            () if s.starts_with("path:") => Ok(Self::Path(val(s))),
            () if s.starts_with("include:") => Ok(Self::Include(val(s))),
            () if s.starts_with("var:") => Ok(Self::Var(val(s))),

            _ => {
                let specifier = s[..s.find(':').unwrap_or(s.len())].to_string();
                Err(Error::UnknownTemplateSpecifier(specifier))
            }
        }
    }

    pub fn as_include(&self) -> Option<&str> {
        match self {
            Self::Include(p) => Some(p),
            _ => None,
        }
    }
}

impl Template {
    /// Parses the input byte string as template. Returns `Err` on parse error.
    pub fn parse(input: Bytes) -> Result<Self, Error> {
        let fragments = FragmentSpans::new(&input)
            .map(|span| {
                let kind = Fragment::parse(&input[span.clone()])?;
                Ok(SpannedFragment { span, kind })
            })
            .collect::<Result<_, _>>()?;

        Ok(Self {
            raw: input,
            fragments,
        })
    }

    /// Returns `Some(out)` if this template does not have any fragments at all.
    /// `out` is equal to the `input` that was passed to `parse`.
    pub fn into_already_rendered(self) -> Result<Bytes, Self> {
        if self.fragments.is_empty() {
            Ok(self.raw)
        } else {
            Err(self)
        }
    }

    /// Returns an iterator over all fragments.
    pub fn fragments(&self) -> impl Iterator<Item = &Fragment> {
        self.fragments.iter().map(|f| &f.kind)
    }

    /// Returns the raw input that was passed to `parse`.
    pub fn raw_input(&self) -> &Bytes {
        &self.raw
    }

    /// Renders the template using `replacer` to evaluate the fragments.
    ///
    /// # Replacing/evaluating fragments
    ///
    /// For each fragment in the `input` template, the `replacer` is called with
    /// the parsed fragment. For example, the template string `foo {{: bar :}}
    /// baz {{: config.data   :}}x` would lead to two calls to `replacer`, with
    /// the following strings as first parameter:
    ///
    /// - `bar`
    /// - `config.data`
    ///
    /// As you can see, excess whitespace is stripped before passing the string
    /// within the fragment.
    pub fn render<R, E>(self, mut replacer: R) -> Result<Bytes, E>
    where
        R: FnMut(Fragment, Appender) -> Result<(), E>,
    {
        if self.fragments.is_empty() {
            return Ok(self.raw);
        }

        let mut out = Vec::new();
        let mut last_fragment_end = 0;

        for fragment in self.fragments {
            // Add the part from the last fragment (or start) to the beginning
            // of this fragment.
            out.extend_from_slice(
                &self.raw[last_fragment_end..fragment.span.start - FRAGMENT_START.len()]
            );

            // Evaluate the fragment.
            replacer(fragment.kind, Appender(&mut out))?;

            last_fragment_end = fragment.span.end +  FRAGMENT_END.len();
        }

        // Add the stuff after the last fragment.
        out.extend_from_slice(&self.raw[last_fragment_end..]);

        Ok(out.into())
    }
}


/// An iterator over the spans of all template fragments in `input`, in order.
///
/// The iterator's item is the span (`Range<usize>`) of the fragment. The span
/// excludes the fragment start and end token, but includes potential excess
/// whitespace. Example:
///
/// ```text
/// input:    b"a{{: kk   :}}b"
/// indices:    0123456789012
/// ```
///
/// For that input, one span would be yielded by the iterator: `5..9`
///  (`input[5..9]` is `"kk  "`).
pub struct FragmentSpans<'a> {
    input: &'a [u8],
    idx: usize,
}

impl<'a> FragmentSpans<'a> {
    pub fn new(input: &'a [u8]) -> Self {
        Self {
            input,
            idx: 0,
        }
    }
}

impl Iterator for FragmentSpans<'_> {
    type Item = Range<usize>;
    fn next(&mut self) -> Option<Self::Item> {
        if self.idx >= self.input.len() {
            return None;
        }

        while let Some(start_pos) = find(&self.input[self.idx..], FRAGMENT_START) {
            // We have a fragment candidate. Now we need to make sure that it's
            // actually a valid fragment.
            let end_pos = self.input[self.idx + start_pos..]
                .windows(FRAGMENT_END.len())
                .take(MAX_FRAGMENT_LEN - FRAGMENT_END.len() + 1)
                .take_while(|win| win[0] != b'\n')
                .position(|win| win == FRAGMENT_END);

            match end_pos {
                // We haven't found a matching end marker: ignore this start marker.
                None => {
                    self.idx += start_pos + FRAGMENT_START.len();
                }

                // This is a real fragment.
                Some(end_pos) => {
                    let start = self.idx + start_pos;
                    self.idx = start + end_pos + FRAGMENT_END.len();

                    return Some(start + FRAGMENT_START.len()..start + end_pos);
                }
            }
        }

        self.idx = self.input.len();
        None
    }
}

fn find(haystack: &[u8], needle: &[u8]) -> Option<usize> {
    twoway::find_bytes(haystack, needle)
}


#[cfg(test)]
mod tests {
    use super::*;

    fn dummy_replacer(f: Fragment, mut appender: Appender) -> Result<(), ()> {
        match f {
            Fragment::Include(p) => {
                appender.append(b"i-");
                appender.append(p.to_uppercase().as_bytes());
            }
            Fragment::Path(p) => {
                appender.append(b"p-");
                appender.append(&p.bytes().rev().collect::<Vec<_>>())
            }
            Fragment::Var(k) => {
                appender.append(b"v-");
                appender.append(k.to_lowercase().as_bytes())
            }
        }

        Ok(())
    }

    fn render(input: &[u8]) -> Bytes {
        let template = Template::parse(Bytes::copy_from_slice(input)).expect("failed to parse");
        template.render(dummy_replacer).unwrap()
    }

    #[test]
    fn render_no_fragments() {
        let s = b"foo, bar, baz";
        let res = render(s);
        assert_eq!(res, s as &[_]);
    }

    #[test]
    fn render_simple_fragments() {
        assert_eq!(
            render(b"{{: include:banana :}}"),
            b"i-BANANA" as &[u8],
        );
        assert_eq!(
            render(b"foo {{: path:cat :}}baz"),
            b"foo p-tacbaz" as &[u8],
        );
        assert_eq!(
            render(b"foo {{: include:cat :}}baz{{: var:DOG :}}"),
            b"foo i-CATbazv-dog" as &[u8],
        );
    }

    #[test]
    fn render_ignored_fragments() {
        assert_eq!(
            render(b"x{{: a\nb :}}y"),
            b"x{{: a\nb :}}y" as &[u8],
        );
        assert_eq!(
            render(b"x{{: a\n {{: include:kiwi :}}y"),
            b"x{{: a\n i-KIWIy" as &[u8],
        );

        let long = b"foo {:: \
            abcdefghijklmnopqrstuvwxyabcdefghijklmnopqrstuvwxy\
            abcdefghijklmnopqrstuvwxyabcdefghijklmnopqrstuvwxy\
            abcdefghijklmnopqrstuvwxyabcdefghijklmnopqrstuvwxy\
            abcdefghijklmnopqrstuvwxyabcdefghijklmnopqrstuvwxy\
            abcdefghijklmnopqrstuvwxyabcdefghijklmnopqrstuvwxy\
            abcdefghijklmnopqrstuvwxyabcdefghijklmnopqrstuvwxy\
            yo ::} bar\
        " as &[u8];
        assert_eq!(render(long), long);
    }
}