-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathformatting.rs
More file actions
195 lines (165 loc) · 4.47 KB
/
formatting.rs
File metadata and controls
195 lines (165 loc) · 4.47 KB
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
use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::Path;
pub(crate) type FormattingResult<T> = Result<T, Box<dyn std::error::Error>>;
pub(crate) trait Printer {
fn write(&mut self, s: &str) -> FormattingResult<()>;
fn newline(&mut self) -> FormattingResult<()>;
fn writeln(&mut self, s: &str) -> FormattingResult<()> {
self.newline()?;
self.write(s)
}
}
pub(crate) struct FilePrinter {
writer: BufWriter<File>,
first_newline: bool,
}
impl FilePrinter {
pub(crate) fn new<T: AsRef<Path>>(filepath: T) -> FormattingResult<Self> {
tracing::info!("Create file: {}", filepath.as_ref().display());
let file = File::create(filepath)?;
let writer = BufWriter::new(file);
Ok(Self {
writer,
first_newline: false,
})
}
}
impl Drop for FilePrinter {
fn drop(&mut self) {
// UNIX newline at the end of file
self.newline().unwrap();
}
}
impl Printer for FilePrinter {
fn write(&mut self, s: &str) -> FormattingResult<()> {
self.writer
.write(s.as_bytes())
.map(|_| {})
.map_err(|e| e.into())
}
fn newline(&mut self) -> FormattingResult<()> {
if !self.first_newline {
self.first_newline = true;
Ok(())
} else {
writeln!(self.writer).map_err(|e| e.into())
}
}
}
pub(crate) struct PrefixPrinter<'a, 'b> {
inner: &'a mut dyn Printer,
prefix: &'b str,
}
impl<'a, 'b> PrefixPrinter<'a, 'b> {
pub(crate) fn new(printer: &'a mut dyn Printer, prefix: &'b str) -> Self {
Self {
inner: printer,
prefix,
}
}
}
impl Printer for PrefixPrinter<'_, '_> {
fn write(&mut self, s: &str) -> FormattingResult<()> {
self.inner.write(s)
}
fn newline(&mut self) -> FormattingResult<()> {
self.inner.newline()?;
self.inner.write(self.prefix)
}
}
pub(crate) struct IndentedPrinter<'a> {
inner: PrefixPrinter<'a, 'static>,
}
impl<'a> IndentedPrinter<'a> {
pub(crate) fn new(printer: &'a mut dyn Printer) -> Self {
Self {
inner: PrefixPrinter::new(printer, " "),
}
}
}
impl Printer for IndentedPrinter<'_> {
fn write(&mut self, s: &str) -> FormattingResult<()> {
self.inner.write(s)
}
fn newline(&mut self) -> FormattingResult<()> {
self.inner.newline()
}
}
pub(crate) struct CommentedPrinter<'a> {
inner: PrefixPrinter<'a, 'static>,
}
impl<'a> CommentedPrinter<'a> {
pub(crate) fn new(f: &'a mut dyn Printer) -> Self {
Self {
inner: PrefixPrinter::new(f, "// "),
}
}
}
impl Printer for CommentedPrinter<'_> {
fn write(&mut self, s: &str) -> FormattingResult<()> {
self.inner.write(s)
}
fn newline(&mut self) -> FormattingResult<()> {
self.inner.newline()
}
}
pub(crate) struct DoxygenPrinter<'a> {
inner: PrefixPrinter<'a, 'static>,
}
impl<'a> DoxygenPrinter<'a> {
pub(crate) fn new(printer: &'a mut dyn Printer) -> Self {
Self {
inner: PrefixPrinter::new(printer, "/// "),
}
}
}
impl Printer for DoxygenPrinter<'_> {
fn write(&mut self, s: &str) -> FormattingResult<()> {
self.inner.write(s)
}
fn newline(&mut self) -> FormattingResult<()> {
self.inner.newline()
}
}
pub(crate) fn indented<F, T>(f: &mut dyn Printer, cb: F) -> FormattingResult<T>
where
F: FnOnce(&mut dyn Printer) -> FormattingResult<T>,
{
let mut printer = IndentedPrinter::new(f);
cb(&mut printer)
}
pub(crate) fn commented<F, T>(f: &mut dyn Printer, cb: F) -> FormattingResult<T>
where
F: FnOnce(&mut dyn Printer) -> FormattingResult<T>,
{
let mut printer = CommentedPrinter::new(f);
cb(&mut printer)
}
pub(crate) fn doxygen<F, T>(f: &mut dyn Printer, cb: F) -> FormattingResult<T>
where
F: FnOnce(&mut dyn Printer) -> FormattingResult<T>,
{
let mut printer = DoxygenPrinter::new(f);
cb(&mut printer)
}
pub(crate) fn blocked_open_close<F, T>(
f: &mut dyn Printer,
open: &str,
close: &str,
cb: F,
) -> FormattingResult<T>
where
F: FnOnce(&mut dyn Printer) -> FormattingResult<T>,
{
f.writeln(open)?;
let result = indented(f, |f| cb(f))?;
f.writeln(close)?;
Ok(result)
}
pub(crate) fn blocked<F, T>(f: &mut dyn Printer, cb: F) -> FormattingResult<T>
where
F: FnOnce(&mut dyn Printer) -> FormattingResult<T>,
{
blocked_open_close(f, "{", "}", cb)
}