Skip to content

Commit f4198bd

Browse files
committed
book: a features section
1 parent 6c7fd3c commit f4198bd

File tree

3 files changed

+153
-4
lines changed

3 files changed

+153
-4
lines changed

book/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- [Debugging](./debugging.md)
66
- [Configuration](./configuration.md)
77
- [Template syntax](./template_syntax.md)
8+
- [Opt-in features](./features.md)
89
- [Filters](./filters.md)
910
- [Web-frameworks](./frameworks.md)
1011
- [Performance](./performance.md)

book/src/features.md

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# Opt-in features
2+
3+
Some features in rinja are opt-in to reduce the amount of dependencies,
4+
and to keep the compilation time low.
5+
6+
To opt-in to a feature, you can use `features = […]`.
7+
E.g. if you want to use the filter [`|json`](filters.html#json--tojson),
8+
you have to opt-in to the feature [`"serde_json"`](#serde_json):
9+
10+
```toml
11+
[dependencies]
12+
rinja = { version = "0.3.5", features = ["serde_json"] }
13+
```
14+
15+
Please read the [Cargo manual](https://doc.rust-lang.org/cargo/reference/features.html#dependency-features)
16+
for more information.
17+
18+
## Default features
19+
20+
Any [semver-compatible](https://doc.rust-lang.org/cargo/reference/semver.html) upgrade
21+
(e.g. `rinja = "0.3.4"` to `rinja = "0.3.5"`) will keep the same list of default features.
22+
We will treat upgrades to a newer dependency version as a semver breaking change.
23+
24+
### `"default"`
25+
26+
You can opt-out of using the feature flags by using
27+
`default-features = false`:
28+
29+
```toml
30+
[dependencies]
31+
rinja = { version = "0.3.5", default-features = false }
32+
```
33+
34+
Without `default-features = false`, i.e with default features enabled,
35+
the following features are automatically selected for you:
36+
37+
```toml
38+
default = ["config", "std", "urlencode"]
39+
```
40+
41+
This should encompass most features an average user of rinja might need.
42+
43+
*If you are writing a **library** that depends on rinja,
44+
and if you want it to be usable in by other users and in **other projects**,
45+
then you should probably **opt-out of features you do not need**.*
46+
47+
### `"config"`
48+
49+
<blockquote class="right" style="padding:0.5ex 1ex; margin:0 0 1ex 1ex; font-size:80%">
50+
enabled by <code>"default"</code>
51+
</blockquote>
52+
53+
Enables compile time [configurations](configuration.html).
54+
55+
### `"urlencode"`
56+
57+
<blockquote class="right" style="padding:0.5ex 1ex; margin:0 0 1ex 1ex; font-size:80%">
58+
enabled by <code>"default"</code>
59+
</blockquote>
60+
61+
Enables the filters [`|urlencode` and `|urlencode_strict`](filter.html#urlencode--urlencode_strict).
62+
63+
## Addition features
64+
65+
<div class="warning">
66+
67+
Please note that we reserve the right to add more features to the current list,
68+
**without** labeling it as a semver **breaking change**.
69+
The newly added features might even depend on a newer rustc version than the previous list.
70+
71+
</div>
72+
73+
The most useful catch-all feature for a quick start might be `"full"`,
74+
which enables all implemented features, i.e.:
75+
76+
```toml
77+
full = ["default", "code-in-doc", "serde_json"]
78+
```
79+
80+
In production or once your project is “maturing” you might want to manually opt-in to any needed
81+
features with a finer granularity instead of depending on `"full"`.
82+
83+
### `"serde_json"`
84+
85+
<blockquote class="right" style="padding:0.5ex 1ex; margin:0 0 1ex 1ex; font-size:80%">
86+
enabled by <code>"full"</code>
87+
</blockquote>
88+
89+
<div class="warning">
90+
91+
This feature depends on the crate [`serde_json`](https://crates.io/crates/serde_json).
92+
We won't treat upgrades to a newer `serde_json` version as a semver breaking change,
93+
even if it raises the <abbr title="Minimum Supported Rust Version">MSRV</abbr>.
94+
95+
</div>
96+
97+
Enables the filter [`|json`](filters.html#json--tojson).
98+
99+
### `"code-in-doc"`
100+
101+
<blockquote class="right" style="padding:0.5ex 1ex; margin:0 0 1ex 1ex; font-size:80%">
102+
enabled by <code>"full"</code>
103+
</blockquote>
104+
105+
<div class="warning">
106+
107+
This feature depends on the crate [`pulldown-cmark`](https://crates.io/crates/pulldown-cmark).
108+
We won't treat upgrades to a newer `pulldown-cmark` version as a semver breaking change,
109+
even if it raises the <abbr title="Minimum Supported Rust Version">MSRV</abbr>.
110+
111+
</div>
112+
113+
Enables using [documentations as template code](creating_templates.html#documentation-as-template-code).
114+
115+
## “Anti-features” in a `#![no_std]` environment
116+
117+
Opting-out of the default features `"std"` and `"alloc"` is only interesting for the use
118+
in a `#![no_std]` environment.
119+
Please find more information in [The Embedded Rust Book](https://docs.rust-embedded.org/book/intro/no-std.html).
120+
121+
### `"alloc"`
122+
123+
<blockquote class="right" style="padding:0.5ex 1ex; margin:0 0 1ex 1ex; font-size:80%">
124+
enabled by <code>"default"</code>
125+
</blockquote>
126+
127+
Without the default feature `"alloc"` rinja can be used in a `#![no_std]` environment.
128+
The method `Template::render()` will be absent, because rinja won't have access to a default allocator.
129+
130+
Many filters need intermediate allocations, and won't be usable without this feature.
131+
132+
You can still render templates using e.g.
133+
[`no_std_io2::io::Cursor`](https://docs.rs/no_std_io2/0.9.0/no_std_io2/io/struct.Cursor.html) or
134+
[`embedded_io::Write`](https://docs.rs/embedded-io/0.6.1/embedded_io/trait.Write.html#method.write_fmt)
135+
136+
### `"std"`
137+
138+
<blockquote class="right" style="padding:0.5ex 1ex; margin:0 0 1ex 1ex; font-size:80%">
139+
enabled by <code>"default"</code>
140+
</blockquote>
141+
142+
Without the feature `"std"` rinja can be used in a `#![no_std]` environment.
143+
The method `Template::write_into()` will be absent, because rinja won't have access to standard IO operations.
144+
145+
Enabling `"std"` enables `"alloc"`, too.

book/src/filters.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -379,13 +379,13 @@ Output:
379379
HELLO
380380
```
381381

382-
### urlencode
383-
[#urlencode]: #urlencode
382+
### urlencode | urlencode_strict
383+
[#urlencode]: #urlencode--urlencode_strict
384384

385385
Percent encodes the string. Replaces reserved characters with the % escape character followed by a byte value as two hexadecimal digits.
386386

387-
```text
388-
hello?world
387+
```jinja
388+
{{ "hello?world" | urlencode }}
389389
```
390390

391391
Output:
@@ -394,6 +394,9 @@ Output:
394394
hello%3Fworld
395395
```
396396

397+
With `|urlencode` all characters except ASCII letters, digits, and `_.-~/` are escaped.
398+
With `|urlencode_strict` a forward slash `/` is escaped, too.
399+
397400
### wordcount
398401
[#wordcount]: #wordcount
399402

0 commit comments

Comments
 (0)