tera_text_filters/
lib.rs

1//! This crate offers filters for the [Tera](https://github.com/Keats/tera) engine that are centered around text transformations.
2
3use heck::{CamelCase, KebabCase, MixedCase, SnakeCase, TitleCase};
4use std::{collections::HashMap, hash::BuildHasher};
5use tera::{to_value, try_get_value, Result, Tera, Value};
6
7/// Registers all available filters for a given `Tera` instance.
8pub fn register_all(tera: &mut Tera) {
9    tera.register_filter("camel_case", camel_case);
10    tera.register_filter("kebab_case", kebab_case);
11    tera.register_filter("lower_case", lower_case);
12    tera.register_filter("mixed_case", mixed_case);
13    tera.register_filter("snake_case", snake_case);
14    tera.register_filter("title_case", title_case);
15    tera.register_filter("upper_case", upper_case);
16}
17
18/// Converts text into CamelCase.
19///
20/// # Example
21///
22/// ```
23/// use tera::{Context, Tera};
24/// use tera_text_filters::camel_case;
25///
26/// let mut ctx = Context::new();
27/// ctx.insert("i", "some text");
28///
29/// let mut tera = Tera::default();
30/// tera.register_filter("camel_case", camel_case);
31///
32/// let i = "{{ i | camel_case }}";
33/// let rendered = tera.render_str(i, &ctx).unwrap();
34/// assert_eq!(rendered, "SomeText");
35/// ```
36pub fn camel_case<S: BuildHasher>(value: &Value, _: &HashMap<String, Value, S>) -> Result<Value> {
37    let s = try_get_value!("camel_case", "value", String, value);
38    Ok(to_value(&s.to_camel_case()).unwrap())
39}
40
41/// Converts text into kebab-case.
42///
43/// # Example
44///
45/// ```
46/// use tera::{Context, Tera};
47/// use tera_text_filters::kebab_case;
48///
49/// let mut ctx = Context::new();
50/// ctx.insert("i", "some text");
51///
52/// let mut tera = Tera::default();
53/// tera.register_filter("kebab_case", kebab_case);
54///
55/// let i = "{{ i | kebab_case }}";
56/// let rendered = tera.render_str(i, &ctx).unwrap();
57/// assert_eq!(rendered, "some-text");
58/// ```
59pub fn kebab_case<S: BuildHasher>(value: &Value, _: &HashMap<String, Value, S>) -> Result<Value> {
60    let s = try_get_value!("kebab_case", "value", String, value);
61    Ok(to_value(&s.to_kebab_case()).unwrap())
62}
63
64/// Converts text into lowercase.
65///
66/// # Example
67///
68/// ```
69/// use tera::{Context, Tera};
70/// use tera_text_filters::lower_case;
71///
72/// let mut ctx = Context::new();
73/// ctx.insert("i", "soMe Text");
74///
75/// let mut tera = Tera::default();
76/// tera.register_filter("lower_case", lower_case);
77///
78/// let i = "{{ i | lower_case }}";
79/// let rendered = tera.render_str(i, &ctx).unwrap();
80/// assert_eq!(rendered, "some text");
81/// ```
82pub fn lower_case<S: BuildHasher>(value: &Value, _: &HashMap<String, Value, S>) -> Result<Value> {
83    let s = try_get_value!("lower_case", "value", String, value);
84    Ok(to_value(&s.to_lowercase()).unwrap())
85}
86
87/// Converts text into mixedCase.
88///
89/// # Example
90///
91/// ```
92/// use tera::{Context, Tera};
93/// use tera_text_filters::mixed_case;
94///
95/// let mut ctx = Context::new();
96/// ctx.insert("i", "Some text");
97///
98/// let mut tera = Tera::default();
99/// tera.register_filter("mixed_case", mixed_case);
100///
101/// let i = "{{ i | mixed_case }}";
102/// let rendered = tera.render_str(i, &ctx).unwrap();
103/// assert_eq!(rendered, "someText");
104/// ```
105pub fn mixed_case<S: BuildHasher>(value: &Value, _: &HashMap<String, Value, S>) -> Result<Value> {
106    let s = try_get_value!("mixed_case", "value", String, value);
107    Ok(to_value(&s.to_mixed_case()).unwrap())
108}
109
110/// Converts text into snake_case.
111///
112/// # Example
113///
114/// ```
115/// use tera::{Context, Tera};
116/// use tera_text_filters::snake_case;
117///
118/// let mut ctx = Context::new();
119/// ctx.insert("i", "soMe Text");
120///
121/// let mut tera = Tera::default();
122/// tera.register_filter("snake_case", snake_case);
123///
124/// let i = "{{ i | snake_case }}";
125/// let rendered = tera.render_str(i, &ctx).unwrap();
126/// assert_eq!(rendered, "some_text");
127/// ```
128pub fn snake_case<S: BuildHasher>(value: &Value, _: &HashMap<String, Value, S>) -> Result<Value> {
129    let s = try_get_value!("snake_case", "value", String, value);
130    Ok(to_value(&s.to_snake_case()).unwrap())
131}
132
133/// Converts text into Title Case.
134///
135/// # Example
136///
137/// ```
138/// use tera::{Context, Tera};
139/// use tera_text_filters::title_case;
140///
141/// let mut ctx = Context::new();
142/// ctx.insert("i", "soMe Text");
143///
144/// let mut tera = Tera::default();
145/// tera.register_filter("title_case", title_case);
146///
147/// let i = "{{ i | title_case }}";
148/// let rendered = tera.render_str(i, &ctx).unwrap();
149/// assert_eq!(rendered, "some_text");
150/// ```
151pub fn title_case<S: BuildHasher>(value: &Value, _: &HashMap<String, Value, S>) -> Result<Value> {
152    let s = try_get_value!("title_case", "value", String, value);
153    Ok(to_value(&s.to_title_case()).unwrap())
154}
155
156/// Converts text into UPPERCASE.
157///
158/// # Example
159///
160/// ```
161/// use tera::{Context, Tera};
162/// use tera_text_filters::upper_case;
163///
164/// let mut ctx = Context::new();
165/// ctx.insert("i", "soMe Text");
166///
167/// let mut tera = Tera::default();
168/// tera.register_filter("upper_case", upper_case);
169///
170/// let i = "{{ i | upper_case }}";
171/// let rendered = tera.render_str(i, &ctx).unwrap();
172/// assert_eq!(rendered, "SOME TEXT");
173/// ```
174pub fn upper_case<S: BuildHasher>(value: &Value, _: &HashMap<String, Value, S>) -> Result<Value> {
175    let s = try_get_value!("upper_case", "value", String, value);
176    Ok(to_value(&s.to_uppercase()).unwrap())
177}