tera-contrib 0.3.0

Additional filters, functions and tests for the Tera template engine
Documentation
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
use icu_calendar::{Gregorian, Iso};
use icu_datetime::fieldsets::enums::CompositeFieldSet;
use icu_datetime::pattern::{DateTimePattern, FixedCalendarDateTimeNames};
use icu_locale::Locale;
use icu_time::zone::models::AtTime;
use icu_time::{TimeZoneInfo, ZonedDateTime};
use jiff::fmt::temporal::{DateTimeParser, Pieces};
use jiff::tz::TimeZone;
use jiff::{Timestamp, Zoned};
use jiff_icu::ConvertFrom;
use tera::{Kwargs, Number, State, TeraResult, Value};
use writeable::TryWriteable;

static PARSER: DateTimeParser = DateTimeParser::new();

/// Parse a Value (string or integer) into a Zoned datetime.
fn parse_to_zoned(val: &Value, tz: Option<TimeZone>) -> TeraResult<Zoned> {
    let default_tz = tz.unwrap_or(TimeZone::UTC);
    if let Some(s) = val.as_str() {
        PARSER
            .parse_zoned(s)
            .or_else(|_| {
                PARSER.parse_timestamp(s).map(|t| {
                    let zone = Pieces::parse(s)
                        .ok()
                        .and_then(|p| p.to_numeric_offset())
                        .map_or_else(|| default_tz.clone(), TimeZone::fixed);
                    t.to_zoned(zone)
                })
            })
            .or_else(|_| {
                PARSER
                    .parse_datetime(s)
                    .and_then(|d| d.to_zoned(default_tz.clone()))
            })
            .or_else(|_| PARSER.parse_date(s).and_then(|d| d.to_zoned(default_tz)))
            .map_err(|e| {
                tera::Error::message(format!(
                    "The string {s} cannot be parsed as a valid date: {e}"
                ))
            })
    } else if let Some(Number::Integer(ts)) = val.as_number() {
        let ts = i64::try_from(ts)
            .map_err(|_| tera::Error::message(format!("Invalid timestamp: {ts}")))?;
        Timestamp::new(ts, 0)
            .map(|t| t.to_zoned(default_tz))
            .map_err(|e| tera::Error::message(format!("Invalid timestamp: {e}")))
    } else {
        Err(tera::Error::message(format!(
            "Invalid value: expected a string or integer, got {}",
            val.name()
        )))
    }
}

/// Returns the current datetime.
/// You can pass an optional `timezone` name. Defaults to UTC if not provided.
///
/// ```text
/// {{ now() }}
/// {{ now(timezone="America/New_York") }}
/// ```
pub fn now(kwargs: Kwargs, _: &State) -> TeraResult<Value> {
    let tz_str = kwargs.get::<&str>("timezone")?.unwrap_or("UTC");
    let timezone = TimeZone::get(tz_str)
        .map_err(|_| tera::Error::message(format!("Unknown timezone: {tz_str}")))?;
    let now = Zoned::now().with_time_zone(timezone);
    Ok(Value::from(now.to_string()))
}

/// Formats the given value using the given format if it can be parsed as a date/datetime.
/// Takes:
///   1. optional `format` argument, defaulting to `%Y-%m-%d`
///   2. optional `timezone` argument, defaulting to not set
///   3. optional `locale` argument, defaulting to not set
///
/// If `locale` is set, `format` is required.
/// `format` uses 2 different formats depending on whether `locale` is set or not:
///
///   1. if `locale` is not set: `strftime` like format seen in [jiff documentation](https://docs.rs/jiff/latest/jiff/fmt/strtime/index.html#conversion-specifications)
///   2. if `locale` is set: [UTS-35 datetime patterns](https://unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table)
///
/// ```text
/// {{ value | date }}
/// {{ value | date(format="%B %d, %Y") }}
/// {{ timestamp | date(format="%Y-%m-%d %H:%M", timezone="Europe/Paris") }}
/// {{ value | date(locale="fr", format="d MMMM y") }}
/// {{ timestamp | date(locale="de", format="EEEE d. MMMM y, HH:mm", timezone="Europe/Berlin") }}
/// ```
pub fn date(val: &Value, kwargs: Kwargs, _: &State) -> TeraResult<String> {
    let format = kwargs.get::<&str>("format")?;
    let locale = kwargs.get::<&str>("locale")?;
    let timezone = match kwargs.get::<&str>("timezone")? {
        Some(t) => Some(
            TimeZone::get(t).map_err(|_| tera::Error::message(format!("Unknown timezone: {t}")))?,
        ),
        None => None,
    };

    let mut zoned = parse_to_zoned(val, timezone.clone())?;
    if let Some(tz) = timezone {
        zoned = zoned.with_time_zone(tz);
    }

    match locale {
        // that's a lof ot types just to format a date...
        // https://docs.rs/icu_datetime/latest/icu_datetime/pattern/struct.FixedCalendarDateTimeNames.html#method.include_for_pattern
        Some(locale_str) => {
            let Some(fmt) = format else {
                return Err(tera::Error::message(
                    "Setting `locale` requires setting a `format` as well",
                ));
            };

            // A `%` in the format is almost certainly a strftime format and is likely and error
            // since we can't use strftime here.
            // Revisit if someone actually needs the literal % in their date output but it's better
            // to error for now IMO
            if fmt.contains('%') {
                return Err(tera::Error::message(format!(
                    "Invalid date format `{fmt}`: strftime formats cannot be localized, \
                     use a UTS-35 pattern instead (eg `d MMMM y`)"
                )));
            }
            // Allow using fr-FR or fr_FR
            let locale = Locale::try_from_str(&locale_str.replace('_', "-"))
                .map_err(|_| tera::Error::message(format!("Invalid locale: {locale_str}")))?;
            let pattern = DateTimePattern::try_from_pattern_str(fmt).map_err(|e| {
                tera::Error::message(format!("Invalid UTS-35 date format `{fmt}`: {e}"))
            })?;
            let mut names =
                FixedCalendarDateTimeNames::<Gregorian, CompositeFieldSet>::try_new(locale.into())
                    .map_err(|e| {
                        tera::Error::message(format!(
                            "Failed to load data for locale {locale_str}: {e}"
                        ))
                    })?;
            let formatter = names.include_for_pattern(&pattern).map_err(|e| {
                tera::Error::message(format!(
                    "Invalid date format `{fmt}` for locale {locale_str}: {e}"
                ))
            })?;

            let iso = ZonedDateTime::<Iso, TimeZoneInfo<AtTime>>::convert_from(&zoned);
            let zdt = ZonedDateTime {
                date: iso.date.to_calendar(Gregorian),
                time: iso.time,
                zone: iso.zone,
            };

            formatter
                .format(&zdt)
                .try_write_to_string()
                .map(|s| s.into_owned())
                .map_err(|(e, _)| {
                    tera::Error::message(format!("Failed to format date with `{fmt}`: {e}"))
                })
        }
        None => {
            let fmt = format.unwrap_or("%Y-%m-%d");
            jiff::fmt::strtime::format(fmt, &zoned)
                .map_err(|e| tera::Error::message(format!("Invalid date format `{fmt}`: {e}")))
        }
    }
}

/// Tests whether a date is before another date.
/// Errors if one of the values cannot be parsed as a date.
/// Takes an optional `inclusive` argument defaulting to false to make this test be `<=` instead of `<`.
///
/// ```text
/// {% if date is before(other="2024-06-01") %}...{% endif %}
/// {% if date is before(other=other_date, inclusive=true) %}...{% endif %}
/// ```
pub fn is_before(val: &Value, kwargs: Kwargs, _: &State) -> TeraResult<bool> {
    let other = kwargs.must_get::<&Value>("other")?;
    let inclusive = kwargs.get::<bool>("inclusive")?.unwrap_or(false);
    let val_zoned = parse_to_zoned(val, None)?;
    let other_zoned = parse_to_zoned(other, None)?;
    if inclusive {
        Ok(val_zoned <= other_zoned)
    } else {
        Ok(val_zoned < other_zoned)
    }
}

/// Tests whether a date is after another date.
/// Errors if one of the values cannot be parsed as a date.
/// Takes an optional `inclusive` argument defaulting to false to make this test be `>=` instead of `>`.
///
/// ```text
/// {% if date is after(other="2024-01-01") %}...{% endif %}
/// {% if date is after(other=other_date, inclusive=true) %}...{% endif %}
/// ```
pub fn is_after(val: &Value, kwargs: Kwargs, _: &State) -> TeraResult<bool> {
    let other = kwargs.must_get::<&Value>("other")?;
    let inclusive = kwargs.get::<bool>("inclusive")?.unwrap_or(false);
    let val_zoned = parse_to_zoned(val, None)?;
    let other_zoned = parse_to_zoned(other, None)?;
    if inclusive {
        Ok(val_zoned >= other_zoned)
    } else {
        Ok(val_zoned > other_zoned)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;
    use tera::value::Map;
    use tera::{Context, Kwargs, State};

    // copy from https://github.com/Keats/tera/blob/master/src/builtins/filters/common.rs#L326
    #[test]
    fn test_ok_date() {
        let inputs = vec![
            (Value::from(1482720453), None, None, None, "2016-12-26"),
            (
                Value::from(1482720453),
                Some("%Y-%m-%d %H:%M"),
                None,
                None,
                "2016-12-26 02:47",
            ),
            // RFC3339
            (
                Value::from("1985-04-12T23:20:50.52Z"),
                None,
                None,
                None,
                "1985-04-12",
            ),
            // timezones are preserved
            (
                Value::from("1996-12-19T16:39:57[-08:00]"),
                Some("%Y-%m-%d %z"),
                None,
                None,
                "1996-12-19 -0800",
            ),
            // a bare RFC3339 offset (no brackets) preserves the offset too
            (
                Value::from("1996-12-19T16:39:57-08:00"),
                Some("%Y-%m-%d %H:%M %z"),
                None,
                None,
                "1996-12-19 16:39 -0800",
            ),
            // simple date
            (
                Value::from("2017-03-05"),
                Some("%a, %d %b %Y %H:%M:%S %z"),
                None,
                None,
                "Sun, 05 Mar 2017 00:00:00 +0000",
            ),
            // naive datetime
            (
                Value::from("2017-03-05T00:00:00.602"),
                Some("%a, %d %b %Y %H:%M:%S"),
                None,
                None,
                "Sun, 05 Mar 2017 00:00:00",
            ),
            // with a timezone
            (
                Value::from("2019-09-19T01:48:44.581Z"),
                None,
                None,
                Some("America/New_York"),
                "2019-09-18",
            ),
            (
                Value::from(1648252203),
                None,
                None,
                Some("Europe/Berlin"),
                "2022-03-26",
            ),
            // And now with locales
            (
                Value::from("2026-08-24"),
                Some("d MMMM y"),
                Some("fr"),
                None,
                "24 août 2026",
            ),
            (
                Value::from("2026-08-24"),
                Some("d MMMM y"),
                Some("fr-FR"),
                None,
                "24 août 2026",
            ),
            (
                Value::from("2026-08-24"),
                Some("d MMMM y"),
                Some("fr_FR"),
                None,
                "24 août 2026",
            ),
            (
                Value::from("2026-08-24"),
                Some("EEEE d MMMM y"),
                Some("fr_FR"),
                None,
                "lundi 24 août 2026",
            ),
            (
                Value::from("2026-08-24"),
                Some("y年M月d日"),
                Some("ja"),
                None,
                "2026年8月24日",
            ),
            (
                Value::from(1787574924),
                Some("d MMMM y à HH:mm"),
                Some("fr"),
                Some("Europe/Paris"),
                "24 août 2026 à 14:35",
            ),
            (
                Value::from("2026-08-24T14:35:00"),
                Some("MMMM d y [zzz]"),
                Some("it"),
                Some("America/New_York"),
                "agosto 24 2026 [GMT-4]",
            ),
        ];

        for (value, format, locale, timezone, expected) in inputs {
            let mut map = Map::new();
            if let Some(f) = format {
                map.insert("format".into(), f.into());
            }
            if let Some(tz) = timezone {
                map.insert("timezone".into(), tz.into());
            }
            if let Some(l) = locale {
                map.insert("locale".into(), l.into());
            }
            let kwargs = Kwargs::new(Arc::new(map));
            let ctx = Context::new();
            let res = date(&value, kwargs, &State::new(&ctx)).unwrap();
            assert_eq!(expected, res);
        }
    }

    #[test]
    fn test_bad_date_call() {
        let inputs = vec![
            (Value::from(1482720453), Some("%1"), None, None),
            (Value::from(1482720453), Some("%+S"), None, None),
            (
                Value::from("2019-09-19T01:48:44.581Z"),
                Some("%+S"),
                Some("Narnia"),
                None,
            ),
            // unknown locale
            (Value::from(1482720453), Some("MMMM"), None, Some("unknown")),
            // missing format with locale
            (Value::from(1482720453), None, None, Some("fr")),
            // strftime formats error when a locale is set
            (Value::from(1482720453), Some("%Y-%m-%d"), None, Some("fr")),
        ];

        for (value, format, timezone, locale) in inputs {
            let mut map = Map::new();
            if let Some(f) = format {
                map.insert("format".into(), f.into());
            }
            if let Some(tz) = timezone {
                map.insert("timezone".into(), tz.into());
            }
            if let Some(l) = locale {
                map.insert("locale".into(), l.into());
            }
            let kwargs = Kwargs::new(Arc::new(map));
            let ctx = Context::new();
            let res = date(&value, kwargs, &State::new(&ctx));
            println!("{res:?}");
            assert!(res.is_err());
        }
    }

    #[test]
    fn test_register() {
        let mut tera = tera::Tera::default();
        tera.register_filter("date", date);
        tera.register_test("before", is_before);
        tera.register_test("after", is_after);
        tera.register_function("now", now);
    }

    #[test]
    fn test_is_before() {
        let ctx = Context::new();
        let state = State::new(&ctx);

        // (val, other, inclusive, expected)
        let cases: Vec<(Value, Value, bool, bool)> = vec![
            (Value::from(500), Value::from(1000), false, true),
            (Value::from(1000), Value::from(500), false, false),
            (
                Value::from("2024-01-01"),
                Value::from("2024-06-01"),
                false,
                true,
            ),
            (
                Value::from("2024-06-01"),
                Value::from("2024-01-01"),
                false,
                false,
            ),
            (Value::from(1000), Value::from("2020-01-01"), false, true), // mixed formats
            (
                Value::from("2024-01-01"),
                Value::from("2024-01-01"),
                false,
                false,
            ), // equal
            (
                Value::from("2024-01-01"),
                Value::from("2024-01-01"),
                true,
                true,
            ), // equal + inclusive
        ];

        for (val, other, inclusive, expected) in cases {
            let mut map = Map::new();
            map.insert("other".into(), other);
            if inclusive {
                map.insert("inclusive".into(), Value::from(true));
            }
            let kwargs = Kwargs::new(Arc::new(map));
            assert_eq!(is_before(&val, kwargs, &state).unwrap(), expected);
        }
    }

    #[test]
    fn test_is_after() {
        let ctx = Context::new();
        let state = State::new(&ctx);

        // (val, other, inclusive, expected)
        let cases: Vec<(Value, Value, bool, bool)> = vec![
            (Value::from(1000), Value::from(500), false, true),
            (Value::from(500), Value::from(1000), false, false),
            (
                Value::from("2024-06-01"),
                Value::from("2024-01-01"),
                false,
                true,
            ),
            (
                Value::from("2024-01-01"),
                Value::from("2024-06-01"),
                false,
                false,
            ),
            (Value::from("2020-01-01"), Value::from(1000), false, true), // mixed formats
            (
                Value::from("2024-01-01"),
                Value::from("2024-01-01"),
                false,
                false,
            ), // equal
            (
                Value::from("2024-01-01"),
                Value::from("2024-01-01"),
                true,
                true,
            ), // equal + inclusive
        ];

        for (val, other, inclusive, expected) in cases {
            let mut map = Map::new();
            map.insert("other".into(), other);
            if inclusive {
                map.insert("inclusive".into(), Value::from(true));
            }
            let kwargs = Kwargs::new(Arc::new(map));
            assert_eq!(is_after(&val, kwargs, &state).unwrap(), expected);
        }
    }
}