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
pub fn escape_guard<T: std::fmt::Write>(a: T) -> EscapeGuard<T> {
EscapeGuard::new(a)
}
pub struct EscapeGuard<T> {
writer: T,
}
impl<T: std::fmt::Write> EscapeGuard<T> {
pub fn new(writer: T) -> EscapeGuard<T> {
EscapeGuard { writer }
}
}
impl<T: std::fmt::Write> std::fmt::Write for EscapeGuard<T> {
fn write_str(&mut self, s: &str) -> Result<(), std::fmt::Error> {
for c in s.chars() {
let r = match c {
'\"' => Some("""),
'\'' => Some("'"),
'<' => Some("<"),
'>' => Some(">"),
'&' => Some("&"),
_ => None,
};
if let Some(r) = r {
self.writer.write_str(r)?;
} else {
self.writer.write_char(c)?;
}
}
Ok(())
}
}
pub struct Adaptor<T> {
pub inner: T,
pub error: Result<(), std::io::Error>,
}
pub fn upgrade_write<T: std::io::Write>(inner: T) -> Adaptor<T> {
Adaptor {
inner,
error: Ok(()),
}
}
impl<T: std::io::Write> std::fmt::Write for Adaptor<T> {
fn write_str(&mut self, s: &str) -> std::fmt::Result {
match self.inner.write_all(s.as_bytes()) {
Ok(()) => Ok(()),
Err(e) => {
self.error = Err(e);
Err(std::fmt::Error)
}
}
}
}
use std::fmt;
#[macro_export]
macro_rules! format_move {
($($arg:tt)*) => {
$crate::tools::disp_const(move |w| write!(w,$($arg)*))
}
}
pub fn disp_const<F: Fn(&mut fmt::Formatter) -> fmt::Result>(a: F) -> DisplayableClosure<F> {
DisplayableClosure::new(a)
}
pub struct DisplayableClosure<F>(pub F);
impl<F: Fn(&mut fmt::Formatter) -> fmt::Result> DisplayableClosure<F> {
#[inline(always)]
pub fn new(a: F) -> Self {
DisplayableClosure(a)
}
}
impl<F: Fn(&mut fmt::Formatter) -> fmt::Result> fmt::Display for DisplayableClosure<F> {
#[inline(always)]
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
(self.0)(formatter)
}
}
pub struct WriteWrap<'a>(pub &'a mut dyn fmt::Write);
impl<'a> WriteWrap<'a> {
pub fn borrow_mut(&mut self) -> WriteWrap {
WriteWrap(self.0)
}
}
impl fmt::Write for WriteWrap<'_> {
fn write_str(&mut self, s: &str) -> Result<(), fmt::Error> {
self.0.write_str(s)
}
fn write_char(&mut self, c: char) -> Result<(), fmt::Error> {
self.0.write_char(c)
}
fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> Result<(), fmt::Error> {
self.0.write_fmt(args)
}
}