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
//! This project is a reimplementation of the nice [MJML](https://mjml.io/) markup language in Rust.
//!
//! [](https://github.com/jdrouet/mrml/actions/workflows/mrml-core-main.yml)
//! [](https://codecov.io/gh/jdrouet/mrml)
//! [](https://codeclimate.com/github/jdrouet/mrml/maintainability)
//!
//! # How to use?
//!
//! To use it you can simply update your `Cargo.toml` by adding
//! ```toml
//! [dependencies]
//! mrml = { version = "3" }
//! serde = { version = "1", features = ["derive"] }
//! ```
//!
//! And you can then just create a `main.rs` with the following code
//! ```rust
//! # #[cfg(feature = "parse")]
//! # {
//! let root = mrml::parse("<mjml><mj-body></mj-body></mjml>").expect("parse template");
//! let opts = mrml::prelude::render::Options::default();
//! match root.element.render(&opts) {
//! Ok(content) => println!("{}", content),
//! Err(_) => println!("couldn't render mjml template"),
//! };
//! # }
//! ```
//!
//! ## Using `mj-include`
//!
//! You can also use the `mj-include` component by specifying a
//! [loader](crate::prelude::parser).
//!
//! ```rust
//! # #[cfg(feature = "parse")]
//! # {
//! use mrml::prelude::parser::ParserOptions;
//! use mrml::prelude::parser::memory_loader::MemoryIncludeLoader;
//!
//! let loader = MemoryIncludeLoader::from(vec![("partial.mjml", "<mj-button>Hello</mj-button>")]);
//! let options = ParserOptions {
//! include_loader: Box::new(loader),
//! };
//! match mrml::parse_with_options("<mjml><mj-head /><mj-body><mj-include path=\"partial.mjml\" /></mj-body></mjml>", &options) {
//! Ok(_) => println!("Success!"),
//! Err(err) => eprintln!("Something went wrong: {err:?}"),
//! }
//! # }
//! ```
//!
//! ## Using `mj-include` with an async loader
//!
//! If you want to use the async version to fetch the includes, you've to enable
//! the `async` feature and the required loaders (`http-loader-async-reqwest` in
//! this example).
//!
//! ```rust
//! # #[cfg(all(feature = "parse", feature = "render", feature = "async", feature = "local-loader", feature = "http-loader", feature = "http-loader-async-reqwest"))]
//! # tokio_test::block_on(async {
//! use mrml::prelude::parser::http_loader::{AsyncReqwestFetcher, HttpIncludeLoader};
//! use mrml::prelude::parser::memory_loader::MemoryIncludeLoader;
//! use mrml::prelude::parser::local_loader::LocalIncludeLoader;
//! use mrml::prelude::parser::multi_loader::MultiIncludeLoader;
//! use mrml::prelude::parser::noop_loader::NoopIncludeLoader;
//! use mrml::prelude::parser::loader::AsyncIncludeLoader;
//! use mrml::prelude::parser::AsyncParserOptions;
//! use mrml::prelude::render::RenderOptions;
//! use std::path::PathBuf;
//! use std::sync::Arc;
//!
//! let resolver = MultiIncludeLoader::<Box<dyn AsyncIncludeLoader + Send + Sync + 'static>>::new()
//! .with_starts_with("memory://", Box::new(MemoryIncludeLoader::from(vec![("basic.mjml", "<mj-button>Hello</mj-button>")])))
//! .with_starts_with("file://", Box::new(LocalIncludeLoader::new(PathBuf::default().join("resources").join("compare").join("success"))))
//! .with_starts_with("https://", Box::new(HttpIncludeLoader::<AsyncReqwestFetcher>::allow_all()))
//! .with_any(Box::<NoopIncludeLoader>::default());
//! let parser_options = AsyncParserOptions {
//! include_loader: Box::new(resolver),
//! };
//! let render_options = RenderOptions::default();
//! let json = r#"<mjml>
//! <mj-body>
//! <mj-include path="file://basic.mjml" />
//! <mj-include path="memory://basic.mjml" />
//! </mj-body>
//! </mjml>"#;
//! match mrml::async_parse_with_options(json, Arc::new(parser_options)).await {
//! Ok(mjml) => match mjml.render(&render_options) {
//! Ok(html) => println!("{html}"),
//! Err(err) => eprintln!("Couldn't render template: {err:?}"),
//! },
//! Err(err) => eprintln!("Couldn't parse template: {err:?}"),
//! }
//! # })
//! ```
//!
//! ## Using `mrml` in Python
//!
//! This crate can also be used in Python. The crate is available with pypi and
//! you can find some documentation [here](https://pypi.org/project/mrml/).
//!
//! ```python
//! import mrml
//!
//! # without options
//! result = mrml.to_html("<mjml></mjml>")
//! assert result.startswith("<!doctype html>")
//!
//! # with options
//! parser_options = mrml.ParserOptions(include_loader = mrml.memory_loader({
//! 'hello-world.mjml': '<mj-text>Hello World!</mj-text>',
//! }))
//! result = mrml.to_html("<mjml><mj-body><mj-include path=\"hello-world.mjml\" /></mj-body></mjml>", parser_options = parser_options)
//! assert result.startswith("<!doctype html>")
//! ```
//!
//! # Why?
//!
//! A Node.js server rendering an MJML template takes around **20 MB** of RAM at
//! startup and **130 MB** under stress test. In Rust, less than **1.7 MB** at
//! startup and a bit less that **3 MB** under stress test. The Rust version can
//! also handle twice as many requests per second. You can perform the
//! benchmarks by running `bash script/run-bench.sh`.
//!
//! Also, the JavaScript implementation cannot be run in the browser; the Rust
//! one (and WebAssembly one) can be.
// Only used to ignore the comments at the root level
/// Function to parse a raw mjml template with some parsing
/// [options](crate::prelude::parser::ParserOptions). This function is just an
/// alias to [the `Mjml::parse_with_options` function](crate::mjml::Mjml).
///
/// You can specify the kind of loader mrml needs to use for loading the content
/// of [`mj-include`](crate::mj_include) elements.
///
/// You can take a look at the available loaders [here](crate::prelude::parser).
///
/// ```rust
/// use mrml::prelude::parser::ParserOptions;
/// use mrml::prelude::parser::memory_loader::MemoryIncludeLoader;
///
/// let options = ParserOptions {
/// include_loader: Box::new(MemoryIncludeLoader::default()),
/// };
/// match mrml::parse_with_options("<mjml><mj-head /><mj-body /></mjml>", &options) {
/// Ok(_) => println!("Success!"),
/// Err(err) => eprintln!("Something went wrong: {err:?}"),
/// }
/// ```
/// Function to parse asynchronously a raw mjml template with some parsing
/// [options](crate::prelude::parser::AsyncParserOptions). This function is just
/// an alias to [the `Mjml::async_parse_with_options`
/// function](crate::mjml::Mjml).
///
/// You can specify the kind of loader mrml needs to use for loading the content
/// of [`mj-include`](crate::mj_include) elements.
///
/// You can take a look at the available loaders [here](crate::prelude::parse).
///
/// ```rust
/// # tokio_test::block_on(async {
/// use mrml::prelude::parser::AsyncParserOptions;
/// use mrml::prelude::parser::memory_loader::MemoryIncludeLoader;
///
/// let options = std::sync::Arc::new(AsyncParserOptions {
/// include_loader: Box::new(MemoryIncludeLoader::default()),
/// });
/// match mrml::async_parse_with_options("<mjml><mj-head /><mj-body /></mjml>", options).await {
/// Ok(_) => println!("Success!"),
/// Err(err) => eprintln!("Something went wrong: {err:?}"),
/// }
/// # })
/// ```
pub async
/// Function to parse a raw mjml template using the default parsing
/// [options](crate::prelude::parser::ParserOptions).
///
/// ```rust
/// match mrml::parse("<mjml><mj-head /><mj-body /></mjml>") {
/// Ok(_) => println!("Success!"),
/// Err(err) => eprintln!("Something went wrong: {err:?}"),
/// }
/// ```
/// Function to parse a raw mjml template using the default parsing
/// [options](crate::prelude::parser::ParserOptions).
///
/// ```rust
/// # tokio_test::block_on(async {
/// match mrml::async_parse("<mjml><mj-head /><mj-body /></mjml>").await {
/// Ok(_) => println!("Success!"),
/// Err(err) => eprintln!("Something went wrong: {err:?}"),
/// }
/// # })
/// ```
pub async