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
use std::{fmt, io};
use crate::utils::AutoEscape;
use crate::value::Value;
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "unstable_machinery_serde", derive(serde::Serialize))]
pub enum CaptureMode {
Capture,
#[allow(unused)]
Discard,
}
pub struct Output<'a> {
w: &'a mut (dyn fmt::Write + 'a),
capture_stack: Vec<Option<String>>,
}
impl<'a> Output<'a> {
pub(crate) fn with_string(buf: &'a mut String) -> Self {
Self {
w: buf,
capture_stack: Vec::new(),
}
}
pub(crate) fn with_write(w: &'a mut (dyn fmt::Write + 'a)) -> Self {
Self {
w,
capture_stack: Vec::new(),
}
}
pub(crate) fn null() -> Self {
Self {
w: NullWriter::get_mut(),
capture_stack: Vec::new(),
}
}
pub(crate) fn begin_capture(&mut self, mode: CaptureMode) {
self.capture_stack.push(match mode {
CaptureMode::Capture => Some(String::new()),
CaptureMode::Discard => None,
});
}
pub(crate) fn end_capture(&mut self, auto_escape: AutoEscape) -> Value {
if let Some(captured) = self.capture_stack.pop().unwrap() {
if !matches!(auto_escape, AutoEscape::None) {
Value::from_safe_string(captured)
} else {
Value::from(captured)
}
} else {
Value::UNDEFINED
}
}
#[inline(always)]
fn target(&mut self) -> &mut dyn fmt::Write {
match self.capture_stack.last_mut() {
Some(Some(stream)) => stream as _,
Some(None) => NullWriter::get_mut(),
None => self.w,
}
}
#[inline]
pub fn write_str(&mut self, s: &str) -> fmt::Result {
self.target().write_str(s)
}
#[inline]
pub fn write_fmt(&mut self, a: fmt::Arguments<'_>) -> fmt::Result {
self.target().write_fmt(a)
}
}
impl fmt::Write for Output<'_> {
#[inline]
fn write_str(&mut self, s: &str) -> fmt::Result {
fmt::Write::write_str(self.target(), s)
}
#[inline]
fn write_char(&mut self, c: char) -> fmt::Result {
fmt::Write::write_char(self.target(), c)
}
#[inline]
fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> fmt::Result {
fmt::Write::write_fmt(self.target(), args)
}
}
pub struct NullWriter;
impl NullWriter {
pub fn get_mut() -> &'static mut NullWriter {
static mut NULL_WRITER: NullWriter = NullWriter;
unsafe { &mut NULL_WRITER }
}
}
impl fmt::Write for NullWriter {
#[inline]
fn write_str(&mut self, _s: &str) -> fmt::Result {
Ok(())
}
#[inline]
fn write_char(&mut self, _c: char) -> fmt::Result {
Ok(())
}
}
pub struct WriteWrapper<W> {
pub w: W,
pub err: Option<io::Error>,
}
impl<W: io::Write> fmt::Write for WriteWrapper<W> {
#[inline]
fn write_str(&mut self, s: &str) -> fmt::Result {
self.w.write_all(s.as_bytes()).map_err(|e| {
self.err = Some(e);
fmt::Error
})
}
#[inline]
fn write_char(&mut self, c: char) -> fmt::Result {
self.w
.write_all(c.encode_utf8(&mut [0; 4]).as_bytes())
.map_err(|e| {
self.err = Some(e);
fmt::Error
})
}
}