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
use std::io::{Result, Write};
#[derive(Clone, Copy)]
pub enum IndentConfig {
Tab,
Space(usize),
}
pub struct IndentedWriter<T> {
out: T,
indentation: Vec<u8>,
config: IndentConfig,
at_begining_of_line: bool,
}
impl<T> IndentedWriter<T> {
pub fn new(out: T, config: IndentConfig) -> Self {
Self {
out,
indentation: Vec::new(),
config,
at_begining_of_line: true,
}
}
pub fn indent(&mut self) {
match self.config {
IndentConfig::Tab => {
self.indentation.push(b'\t');
}
IndentConfig::Space(n) => {
self.indentation.resize(self.indentation.len() + n, b' ');
}
}
}
pub fn unindent(&mut self) {
match self.config {
IndentConfig::Tab => {
self.indentation.pop();
}
IndentConfig::Space(n) => {
self.indentation
.truncate(self.indentation.len().saturating_sub(n));
}
}
}
}
impl<T: Write> Write for IndentedWriter<T> {
fn write(&mut self, mut buf: &[u8]) -> Result<usize> {
let mut bytes_written = 0;
while !buf.is_empty() {
let (before_newline, has_newline, after_newline) =
if let Some(idx) = buf.iter().position(|&b| b == b'\n') {
(&buf[..idx], true, &buf[idx + 1..])
} else {
(buf, false, &buf[buf.len()..])
};
if self.at_begining_of_line && !before_newline.is_empty() {
self.out.write_all(&self.indentation)?;
self.at_begining_of_line = false;
}
self.out.write_all(before_newline)?;
bytes_written += before_newline.len();
if has_newline {
self.out.write_all(b"\n")?;
bytes_written += 1;
self.at_begining_of_line = true;
}
buf = after_newline;
}
Ok(bytes_written)
}
fn flush(&mut self) -> Result<()> {
self.out.flush()
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn basic() -> Result<()> {
let mut buffer: Vec<u8> = Vec::new();
let mut out = IndentedWriter::new(&mut buffer, IndentConfig::Space(2));
writeln!(out, "foo")?;
out.indent();
writeln!(out, "bar")?;
writeln!(out)?;
writeln!(out, "bar")?;
out.indent();
writeln!(out, "foobar")?;
writeln!(out)?;
writeln!(out, "foobar")?;
out.unindent();
out.unindent();
writeln!(out, "foo")?;
let expect: &[u8] = b"\
foo
bar
bar
foobar
foobar
foo
";
assert_eq!(buffer, expect);
Ok(())
}
}