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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use std::fmt::{Result, Write};
use swc_common::{BytePos, LineCol, Span};
use super::CssWriter;
pub enum IndentType {
Tab,
Space,
}
impl Default for IndentType {
fn default() -> Self {
IndentType::Space
}
}
pub enum LineFeed {
LF,
CRLF,
}
impl Default for LineFeed {
fn default() -> Self {
LineFeed::LF
}
}
pub struct BasicCssWriterConfig {
pub indent_type: IndentType,
pub indent_width: i32,
pub linefeed: LineFeed,
}
impl Default for BasicCssWriterConfig {
fn default() -> Self {
BasicCssWriterConfig {
indent_type: IndentType::default(),
indent_width: 2,
linefeed: LineFeed::default(),
}
}
}
pub struct BasicCssWriter<'a, W>
where
W: Write,
{
line_start: bool,
line: usize,
col: usize,
indent_type: &'a str,
indent_level: usize,
linefeed: &'a str,
srcmap: Option<&'a mut Vec<(BytePos, LineCol)>>,
config: BasicCssWriterConfig,
w: W,
}
impl<'a, W> BasicCssWriter<'a, W>
where
W: Write,
{
pub fn new(
writer: W,
srcmap: Option<&'a mut Vec<(BytePos, LineCol)>>,
config: BasicCssWriterConfig,
) -> Self {
let indent_type = match config.indent_type {
IndentType::Tab => "\t",
IndentType::Space => " ",
};
let linefeed = match config.linefeed {
LineFeed::LF => "\n",
LineFeed::CRLF => "\r\n",
};
BasicCssWriter {
line_start: true,
line: 0,
col: 0,
indent_type,
indent_level: 0,
linefeed,
config,
srcmap,
w: writer,
}
}
fn write(&mut self, span: Option<Span>, data: &str) -> Result {
if !data.is_empty() {
if self.line_start {
self.write_indent_string()?;
self.line_start = false;
}
if let Some(span) = span {
if !span.is_dummy() {
self.srcmap(span.lo())
}
}
self.raw_write(data)?;
if let Some(span) = span {
if !span.is_dummy() {
self.srcmap(span.hi())
}
}
}
Ok(())
}
fn write_indent_string(&mut self) -> Result {
for _ in 0..(self.config.indent_width * self.indent_level as i32) {
self.raw_write(self.indent_type)?;
}
Ok(())
}
fn raw_write(&mut self, data: &str) -> Result {
self.w.write_str(data)?;
self.col += data.chars().count();
Ok(())
}
fn srcmap(&mut self, byte_pos: BytePos) {
if let Some(ref mut srcmap) = self.srcmap {
srcmap.push((
byte_pos,
LineCol {
line: self.line as _,
col: self.col as _,
},
))
}
}
}
impl<W> CssWriter for BasicCssWriter<'_, W>
where
W: Write,
{
fn write_space(&mut self) -> Result {
self.write_raw(None, " ")
}
fn write_newline(&mut self) -> Result {
if !self.line_start {
self.raw_write(self.linefeed)?;
self.line += 1;
self.col = 0;
self.line_start = true;
}
Ok(())
}
fn write_raw(&mut self, span: Option<Span>, text: &str) -> Result {
debug_assert!(
!text.contains('\n'),
"write_raw should not contains new lines, got '{}'",
text,
);
self.write(span, text)?;
Ok(())
}
fn write_str(&mut self, span: Span, s: &str) -> Result {
if !s.is_empty() {
let mut lines = s.split('\n').peekable();
let mut lo_byte_pos = span.lo();
while let Some(line) = lines.next() {
if !span.is_dummy() {
self.srcmap(lo_byte_pos)
}
self.raw_write(line)?;
if lines.peek().is_some() {
self.raw_write("\n")?;
self.line += 1;
self.col = 0;
if !span.is_dummy() {
lo_byte_pos = lo_byte_pos + BytePos((line.len() + 1) as u32);
}
} else if !span.is_dummy() {
self.srcmap(span.hi());
}
}
}
Ok(())
}
fn increase_indent(&mut self) {
self.indent_level += 1;
}
fn decrease_indent(&mut self) {
debug_assert!(
(self.indent_level as i32) >= 0,
"indent should zero or greater than zero",
);
self.indent_level -= 1;
}
}