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
use render::RenderOnce;
use std::fmt;
use std::io;
use error::{self, Error};
pub trait Template: RenderOnce + Sized {
fn into_string(self) -> Result<String, Error> {
let mut string = String::with_capacity(self.size_hint());
self.write_to_string(&mut string).map(|_| {
string.shrink_to_fit();
string
})
}
fn write_to_string(self, string: &mut String) -> Result<(), Error> {
let mut buffer = TemplateBuffer {
writer: InnerTemplateWriter::Str(string),
error: Default::default(),
};
self.render_once(&mut buffer);
buffer.into_result()
}
fn write_to_fmt(self, writer: &mut fmt::Write) -> Result<(), Error> {
let mut buffer = TemplateBuffer {
writer: InnerTemplateWriter::Fmt(writer),
error: Default::default(),
};
self.render_once(&mut buffer);
buffer.into_result()
}
fn write_to_io(self, writer: &mut io::Write) -> Result<(), Error> {
let mut buffer = TemplateBuffer {
writer: InnerTemplateWriter::Io(writer),
error: Default::default(),
};
self.render_once(&mut buffer);
buffer.into_result()
}
}
impl<T: RenderOnce + Sized> Template for T {}
pub struct TemplateBuffer<'a> {
writer: InnerTemplateWriter<'a>,
error: Error,
}
enum InnerTemplateWriter<'a> {
Io(&'a mut io::Write),
Fmt(&'a mut fmt::Write),
Str(&'a mut String),
}
impl<'a> TemplateBuffer<'a> {
#[cold]
pub fn record_error<E: Into<Box<::std::error::Error + Send + Sync>>>(&mut self, e: E) {
self.error.render.push(e.into());
}
#[inline(always)]
pub fn write_raw(&mut self, text: &str) {
use std::fmt::Write;
let _ = self.as_raw_writer().write_str(text);
}
#[inline]
pub fn write_fmt(&mut self, args: fmt::Arguments) {
use std::fmt::Write;
let _ = self.as_writer().write_fmt(args);
}
#[inline]
pub fn write_str(&mut self, text: &str) {
use std::fmt::Write;
let _ = self.as_writer().write_str(text);
}
#[inline]
pub fn as_writer<'b>(&'b mut self) -> TemplateWriter<'a, 'b> {
TemplateWriter(self)
}
#[inline]
pub fn as_raw_writer<'b>(&'b mut self) -> RawTemplateWriter<'a, 'b> {
RawTemplateWriter(self)
}
fn into_result(self) -> Result<(), Error> {
if error::is_empty(&self.error) {
Ok(())
} else {
Err(self.error)
}
}
}
pub struct RawTemplateWriter<'a, 'b>(&'b mut TemplateBuffer<'a>) where 'a: 'b;
impl<'a, 'b> fmt::Write for RawTemplateWriter<'a, 'b> {
#[inline(always)]
fn write_str(&mut self, text: &str) -> fmt::Result {
use self::InnerTemplateWriter::*;
if !error::is_empty(&self.0.error) {
return Ok(());
}
match self.0.writer {
Io(ref mut writer) => {
self.0.error.write = writer.write_all(text.as_bytes()).err();
}
Fmt(ref mut writer) => {
if writer.write_str(text).is_err() {
self.0.error.write = Some(io::Error::new(io::ErrorKind::Other, "Format Error"));
}
}
Str(ref mut writer) => {
let _ = writer.write_str(text);
}
}
Ok(())
}
}
pub struct TemplateWriter<'a, 'b>(&'b mut TemplateBuffer<'a>) where 'a: 'b;
impl<'a, 'b> fmt::Write for TemplateWriter<'a, 'b> {
fn write_str(&mut self, text: &str) -> fmt::Result {
use self::InnerTemplateWriter::*;
if !error::is_empty(&self.0.error) {
return Ok(());
}
fn should_escape(b: u8) -> bool {
(b | 0x4) == b'&' || (b | 0x2) == b'>'
}
match self.0.writer {
Io(ref mut writer) => {
for b in text.bytes() {
if let Err(e) = match (should_escape(b), b) {
(true, b'&') => writer.write_all(b"&"),
(true, b'"') => writer.write_all(b"""),
(true, b'<') => writer.write_all(b"<"),
(true, b'>') => writer.write_all(b">"),
_ => writer.write_all(&[b] as &[u8]),
} {
self.0.error.write = Some(e);
break;
}
}
}
Fmt(ref mut writer) => {
for c in text.chars() {
if (match (should_escape(c as u8), c as u8) {
(true, b'&') => writer.write_str("&"),
(true, b'"') => writer.write_str("""),
(true, b'<') => writer.write_str("<"),
(true, b'>') => writer.write_str(">"),
_ => writer.write_char(c),
}).is_err() {
self.0.error.write = Some(io::Error::new(io::ErrorKind::Other, "Format Error"));
break;
}
}
}
Str(ref mut writer) => {
for b in text.bytes() {
match (should_escape(b), b) {
(true, b'&') => writer.push_str("&"),
(true, b'"') => writer.push_str("""),
(true, b'<') => writer.push_str("<"),
(true, b'>') => writer.push_str(">"),
_ => unsafe { writer.as_mut_vec() }.push(b),
}
}
}
}
Ok(())
}
}