askama/filters/
mod.rs

1//! Module for built-in filter functions
2//!
3//! Contains all the built-in filter functions for use in templates.
4//! You can define your own filters, as well.
5//!
6//! ## Note
7//!
8//! All **result types of any filter function** in this module is **subject to change** at any
9//! point, and is **not indicated by as semver breaking** version bump.
10//! The traits [`AutoEscape`] and [`WriteWritable`] are used by [`askama_macros`]'s generated code
11//! to work with all compatible types.
12
13#[cfg(feature = "alloc")]
14mod alloc;
15mod core;
16mod default;
17mod escape;
18mod humansize;
19mod indent;
20#[cfg(feature = "serde_json")]
21mod json;
22#[cfg(feature = "std")]
23mod std;
24#[cfg(feature = "urlencode")]
25mod urlencode;
26
27#[cfg(feature = "alloc")]
28pub use self::alloc::{
29    capitalize, fmt, format, lower, lowercase, title, titlecase, trim, upper, uppercase,
30};
31pub use self::core::{
32    Either, PluralizeCount, center, join, linebreaks, linebreaksbr, paragraphbreaks, pluralize,
33    reject, reject_with, truncate, wordcount,
34};
35pub use self::default::{DefaultFilterable, assigned_or};
36pub use self::escape::{
37    AutoEscape, AutoEscaper, Escaper, Html, HtmlSafe, HtmlSafeOutput, MaybeSafe, Safe, Text,
38    Unsafe, Writable, WriteWritable, e, escape, safe,
39};
40pub use self::humansize::filesizeformat;
41pub use self::indent::{AsIndent, indent};
42#[cfg(feature = "serde_json")]
43pub use self::json::{json, json_pretty};
44#[cfg(feature = "std")]
45pub use self::std::unique;
46#[cfg(feature = "urlencode")]
47pub use self::urlencode::{urlencode, urlencode_strict};
48
49// MAX_LEN is maximum allowed length for filters.
50const MAX_LEN: usize = 10_000;
51
52/// Internal trait that is used by the `filter_fn` proc-macro to produce nicer error messages when
53/// too many arguments were passed to a filter invocation.
54#[doc(hidden)]
55#[diagnostic::on_unimplemented(
56    message = "Argument at position {IDX} is invalid on filter {Self}. Too many arguments supplied?",
57    label = "Filter function"
58)]
59pub trait ValidArgIdx<const IDX: usize> {
60    const VALID: bool = true;
61}
62
63/// Internal marker trait that is used by the `filter_fn` proc-macro to produce nicer error messages
64/// too few arguments were passed to a filter invocation.
65#[doc(hidden)]
66#[diagnostic::on_unimplemented(
67    message = "Invalid filter function invocation. Not all required arguments were supplied.",
68    label = "Filter function"
69)]
70pub trait ValidFilterInvocation: Sized {
71    #[inline(always)]
72    fn wrap(self) -> Self {
73        self
74    }
75}