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
use crate::{SourceMapBuilder, Span};
pub trait ToString {
fn push(&mut self, chr: char);
fn push_new_line(&mut self);
fn push_str(&mut self, string: &str);
fn push_str_contains_new_line(&mut self, string: &str);
fn add_mapping(&mut self, source_span: &Span);
}
impl ToString for String {
fn push(&mut self, chr: char) {
self.push(chr);
}
fn push_new_line(&mut self) {
self.push('\n');
}
fn push_str(&mut self, string: &str) {
self.push_str(string)
}
fn push_str_contains_new_line(&mut self, string: &str) {
self.push_str(string)
}
fn add_mapping(&mut self, _source_span: &Span) {}
}
#[derive(Default)]
pub struct StringWithSourceMap(String, SourceMapBuilder);
impl StringWithSourceMap {
pub fn new() -> Self {
Self::default()
}
pub fn build(self) -> (String, String) {
(self.0, self.1.build())
}
#[cfg(feature = "inline-source-map")]
pub fn build_with_inline_source_map(self) -> String {
let Self(mut source, source_map) = self;
let built_source_map = source_map.build();
source.push_str("\n//# sourceMappingURL=data:application/json;base64,");
source.push_str(&base64::encode(built_source_map));
source
}
}
impl ToString for StringWithSourceMap {
fn push(&mut self, chr: char) {
self.1.add_to_column(chr.len_utf16());
self.0.push(chr);
}
fn push_new_line(&mut self) {
self.1.add_new_line();
self.0.push('\n');
}
fn push_str(&mut self, slice: &str) {
self.1.add_to_column(slice.chars().count());
self.0.push_str(slice);
}
fn push_str_contains_new_line(&mut self, slice: &str) {
for chr in slice.chars() {
if chr == '\n' {
self.1.add_new_line()
}
}
self.0.push_str(slice);
}
fn add_mapping(&mut self, source_span: &Span) {
self.1.add_mapping(source_span);
}
}
#[derive(Default)]
pub struct Counter(usize);
impl Counter {
pub fn new() -> Self {
Self::default()
}
pub fn get_count(self) -> usize {
self.0
}
}
impl ToString for Counter {
fn push(&mut self, chr: char) {
self.0 += chr.len_utf8();
}
fn push_new_line(&mut self) {
self.push('\n');
}
fn push_str(&mut self, string: &str) {
self.0 += string.len();
}
fn push_str_contains_new_line(&mut self, string: &str) {
self.0 += string.len();
}
fn add_mapping(&mut self, _source_span: &Span) {}
}
#[cfg(test)]
mod to_string_tests {
use super::{Counter, ToString};
fn serializer<T: ToString>(t: &mut T) {
t.push_str("Hello");
t.push(' ');
t.push_str("World");
}
#[test]
fn string_concatenation() {
let mut s = String::new();
serializer(&mut s);
assert_eq!(&s, "Hello World");
}
#[test]
fn counting() {
let mut s = Counter::new();
serializer(&mut s);
assert_eq!(s.get_count(), 11);
}
}