forked from parcel-bundler/lightningcss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprinter.rs
More file actions
60 lines (48 loc) · 1.07 KB
/
printer.rs
File metadata and controls
60 lines (48 loc) · 1.07 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
use std::fmt::*;
pub struct Printer<'a, W> {
dest: &'a mut W,
indent: u8,
pub minify: bool
}
impl<'a, W: Write + Sized> Printer<'a, W> {
pub fn new(dest: &mut W, minify: bool) -> Printer<W> {
Printer { dest, indent: 0, minify }
}
pub fn write_str(&mut self, s: &str) -> Result {
self.dest.write_str(s)
}
pub fn whitespace(&mut self) -> Result {
if self.minify {
return Ok(())
}
self.write_char(' ')
}
pub fn delim(&mut self, delim: char, ws_before: bool) -> Result {
if ws_before {
self.whitespace()?;
}
self.write_char(delim)?;
self.whitespace()
}
pub fn newline(&mut self) -> Result {
if self.minify {
return Ok(())
}
self.write_char('\n')?;
if self.indent > 0 {
self.write_str(&" ".repeat(self.indent as usize))?;
}
Ok(())
}
pub fn indent(&mut self) {
self.indent += 2;
}
pub fn dedent(&mut self) {
self.indent -= 2;
}
}
impl<'a, W: Write + Sized> Write for Printer<'a, W> {
fn write_str(&mut self, s: &str) -> Result {
self.dest.write_str(s)
}
}