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
#![no_std]
#![doc(html_root_url = "https://docs.rs/maud/0.22.3")]
extern crate alloc;
use alloc::string::String;
use core::fmt::{self, Write};
pub use maud_macros::{html, html_debug};
pub trait Render {
fn render(&self) -> Markup {
let mut buffer = String::new();
self.render_to(&mut buffer);
PreEscaped(buffer)
}
fn render_to(&self, buffer: &mut String) {
buffer.push_str(&self.render().into_string());
}
}
impl<T: fmt::Display + ?Sized> Render for T {
fn render_to(&self, w: &mut String) {
let _ = write!(Escaper::new(w), "{}", self);
}
}
#[doc(hidden)]
pub mod render {
use crate::Render;
use alloc::string::String;
use core::fmt::Write;
use maud_htmlescape::Escaper;
pub trait RenderInternal {
fn __maud_render_to(&self, w: &mut String);
}
pub struct RenderWrapper<'a, T: ?Sized>(pub &'a T);
impl<'a, T: AsRef<str> + ?Sized> RenderWrapper<'a, T> {
pub fn __maud_render_to(&self, w: &mut String) {
let _ = Escaper::new(w).write_str(self.0.as_ref());
}
}
impl<'a, T: Render + ?Sized> RenderInternal for RenderWrapper<'a, T> {
fn __maud_render_to(&self, w: &mut String) {
self.0.render_to(w);
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct PreEscaped<T: AsRef<str>>(pub T);
impl<T: AsRef<str>> Render for PreEscaped<T> {
fn render_to(&self, w: &mut String) {
w.push_str(self.0.as_ref());
}
}
pub type Markup = PreEscaped<String>;
impl<T: AsRef<str> + Into<String>> PreEscaped<T> {
pub fn into_string(self) -> String {
self.0.into()
}
}
impl<T: AsRef<str> + Into<String>> From<PreEscaped<T>> for String {
fn from(value: PreEscaped<T>) -> String {
value.into_string()
}
}
pub use maud_htmlescape::Escaper;
pub const DOCTYPE: PreEscaped<&'static str> = PreEscaped("<!DOCTYPE html>");
#[cfg(feature = "iron")]
mod iron_support {
extern crate std;
use crate::PreEscaped;
use alloc::{boxed::Box, string::String};
use iron::{
headers::ContentType,
modifier::{Modifier, Set},
modifiers::Header,
response::{Response, WriteBody},
};
use std::io;
impl Modifier<Response> for PreEscaped<String> {
fn modify(self, response: &mut Response) {
response
.set_mut(Header(ContentType::html()))
.set_mut(Box::new(self) as Box<dyn WriteBody>);
}
}
impl WriteBody for PreEscaped<String> {
fn write_body(&mut self, body: &mut dyn io::Write) -> io::Result<()> {
self.0.write_body(body)
}
}
}
#[cfg(feature = "rocket")]
mod rocket_support {
extern crate std;
use crate::PreEscaped;
use alloc::string::String;
use rocket::{
http::{ContentType, Status},
request::Request,
response::{Responder, Response},
};
use std::io::Cursor;
impl Responder<'static> for PreEscaped<String> {
fn respond_to(self, _: &Request) -> Result<Response<'static>, Status> {
Response::build()
.header(ContentType::HTML)
.sized_body(Cursor::new(self.0))
.ok()
}
}
}
#[cfg(feature = "actix-web")]
mod actix_support {
use crate::PreEscaped;
use actix_web_dep::{Error, HttpRequest, HttpResponse, Responder};
use alloc::string::String;
use futures_util::future::{ok, Ready};
impl Responder for PreEscaped<String> {
type Error = Error;
type Future = Ready<Result<HttpResponse, Self::Error>>;
fn respond_to(self, _req: &HttpRequest) -> Self::Future {
ok(HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(self.0))
}
}
}
#[cfg(feature = "tide")]
mod tide_support {
use crate::PreEscaped;
use alloc::string::String;
use tide::{http::mime, Response, StatusCode};
impl From<PreEscaped<String>> for Response {
fn from(markup: PreEscaped<String>) -> Response {
Response::builder(StatusCode::Ok)
.body(markup.into_string())
.content_type(mime::HTML)
.build()
}
}
}
#[cfg(feature = "axum")]
mod axum_support {
use crate::PreEscaped;
use alloc::string::String;
use axum::{
body::Body,
http::{header, HeaderValue, Response, StatusCode},
response::IntoResponse,
};
impl IntoResponse for PreEscaped<String> {
fn into_response(self) -> Response<Body> {
let mut res = Response::new(Body::from(self.0));
*res.status_mut() = StatusCode::OK;
res.headers_mut().insert(
header::CONTENT_TYPE,
HeaderValue::from_static("text/html; charset=utf-8"),
);
res
}
}
}