Skip to content

Commit 1c5e7c2

Browse files
committed
refactor: RawSource
1 parent 5b3a7b3 commit 1c5e7c2

File tree

1 file changed

+69
-38
lines changed

1 file changed

+69
-38
lines changed

src/raw_source.rs

Lines changed: 69 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ use crate::{
1212
MapOptions, Source, SourceMap,
1313
};
1414

15+
#[derive(Clone, PartialEq, Eq)]
16+
enum RawValue {
17+
Buffer(Vec<u8>),
18+
String(String),
19+
}
20+
1521
/// Represents source code without source map, it will not create source map for the source code.
1622
///
1723
/// - [webpack-sources docs](https://github.com/webpack/webpack-sources/#rawsource).
@@ -25,64 +31,88 @@ use crate::{
2531
/// assert_eq!(s.map(&MapOptions::default()), None);
2632
/// assert_eq!(s.size(), 16);
2733
/// ```
28-
#[derive(Clone, Eq)]
29-
pub enum RawSource {
30-
/// Represent buffer.
31-
Buffer(Vec<u8>, OnceLock<String>),
32-
/// Represent string.
33-
Source(String),
34+
pub struct RawSource {
35+
value: RawValue,
36+
value_as_string: OnceLock<String>,
37+
}
38+
39+
impl Clone for RawSource {
40+
fn clone(&self) -> Self {
41+
Self {
42+
value: self.value.clone(),
43+
value_as_string: Default::default(),
44+
}
45+
}
3446
}
3547

48+
impl Eq for RawSource {}
49+
3650
impl RawSource {
3751
/// Whether the [RawSource] represent a buffer.
3852
pub fn is_buffer(&self) -> bool {
39-
matches!(self, Self::Buffer(_, _))
53+
matches!(self.value, RawValue::Buffer(_))
4054
}
4155
}
4256

4357
impl From<String> for RawSource {
44-
fn from(s: String) -> Self {
45-
Self::Source(s)
58+
fn from(value: String) -> Self {
59+
Self {
60+
value: RawValue::String(value),
61+
value_as_string: Default::default(),
62+
}
4663
}
4764
}
4865

4966
impl From<Vec<u8>> for RawSource {
50-
fn from(s: Vec<u8>) -> Self {
51-
Self::Buffer(s, OnceLock::new())
67+
fn from(value: Vec<u8>) -> Self {
68+
Self {
69+
value: RawValue::Buffer(value),
70+
value_as_string: Default::default(),
71+
}
5272
}
5373
}
5474

5575
impl From<&str> for RawSource {
56-
fn from(s: &str) -> Self {
57-
Self::Source(s.to_owned())
76+
fn from(value: &str) -> Self {
77+
Self {
78+
value: RawValue::String(value.to_string()),
79+
value_as_string: Default::default(),
80+
}
5881
}
5982
}
6083

6184
impl From<&[u8]> for RawSource {
62-
fn from(s: &[u8]) -> Self {
63-
Self::Buffer(s.to_owned(), OnceLock::new())
85+
fn from(value: &[u8]) -> Self {
86+
Self {
87+
value: RawValue::Buffer(value.to_owned()),
88+
value_as_string: Default::default(),
89+
}
6490
}
6591
}
6692

6793
impl Source for RawSource {
6894
fn source(&self) -> Cow<str> {
69-
match self {
70-
RawSource::Buffer(i, _) => String::from_utf8_lossy(i),
71-
RawSource::Source(i) => Cow::Borrowed(i),
95+
match &self.value {
96+
RawValue::String(v) => Cow::Borrowed(v),
97+
RawValue::Buffer(v) => Cow::Borrowed(
98+
self
99+
.value_as_string
100+
.get_or_init(|| String::from_utf8_lossy(v).to_string()),
101+
),
72102
}
73103
}
74104

75105
fn buffer(&self) -> Cow<[u8]> {
76-
match self {
77-
RawSource::Buffer(i, _) => Cow::Borrowed(i),
78-
RawSource::Source(i) => Cow::Borrowed(i.as_bytes()),
106+
match &self.value {
107+
RawValue::String(v) => Cow::Borrowed(v.as_bytes()),
108+
RawValue::Buffer(v) => Cow::Borrowed(v),
79109
}
80110
}
81111

82112
fn size(&self) -> usize {
83-
match self {
84-
RawSource::Buffer(i, _) => i.len(),
85-
RawSource::Source(i) => i.len(),
113+
match &self.value {
114+
RawValue::String(v) => v.len(),
115+
RawValue::Buffer(v) => v.len(),
86116
}
87117
}
88118

@@ -91,9 +121,9 @@ impl Source for RawSource {
91121
}
92122

93123
fn to_writer(&self, writer: &mut dyn std::io::Write) -> std::io::Result<()> {
94-
writer.write_all(match self {
95-
RawSource::Buffer(i, _) => i,
96-
RawSource::Source(i) => i.as_bytes(),
124+
writer.write_all(match &self.value {
125+
RawValue::String(v) => v.as_bytes(),
126+
RawValue::Buffer(v) => v,
97127
})
98128
}
99129
}
@@ -107,9 +137,9 @@ impl Hash for RawSource {
107137

108138
impl PartialEq for RawSource {
109139
fn eq(&self, other: &Self) -> bool {
110-
match (self, other) {
111-
(Self::Buffer(l0, _), Self::Buffer(r0, _)) => l0 == r0,
112-
(Self::Source(l0), Self::Source(r0)) => l0 == r0,
140+
match (&self.value, &other.value) {
141+
(RawValue::Buffer(l0), RawValue::Buffer(r0)) => l0 == r0,
142+
(RawValue::String(l0), RawValue::String(r0)) => l0 == r0,
113143
_ => false,
114144
}
115145
}
@@ -121,14 +151,14 @@ impl std::fmt::Debug for RawSource {
121151
f: &mut std::fmt::Formatter<'_>,
122152
) -> Result<(), std::fmt::Error> {
123153
let mut d = f.debug_struct("RawSource");
124-
match self {
125-
Self::Buffer(buffer, _) => {
154+
match &self.value {
155+
RawValue::Buffer(buffer) => {
126156
d.field(
127157
"buffer",
128158
&buffer.iter().take(50).copied().collect::<Vec<u8>>(),
129159
);
130160
}
131-
Self::Source(string) => {
161+
RawValue::String(string) => {
132162
d.field("source", &string.chars().take(50).collect::<String>());
133163
}
134164
}
@@ -147,15 +177,16 @@ impl<'a> StreamChunks<'a> for RawSource {
147177
if options.final_source {
148178
get_generated_source_info(&self.source())
149179
} else {
150-
match &self {
151-
RawSource::Buffer(buffer, value_as_string) => {
152-
let source = value_as_string
180+
match &self.value {
181+
RawValue::Buffer(buffer) => {
182+
let source = self
183+
.value_as_string
153184
.get_or_init(|| String::from_utf8_lossy(buffer).to_string());
154185
stream_chunks_of_raw_source(
155186
source, options, on_chunk, on_source, on_name,
156187
)
157188
}
158-
RawSource::Source(source) => stream_chunks_of_raw_source(
189+
RawValue::String(source) => stream_chunks_of_raw_source(
159190
source, options, on_chunk, on_source, on_name,
160191
),
161192
}
@@ -172,7 +203,7 @@ mod tests {
172203
// Fix https://github.com/web-infra-dev/rspack/issues/6793
173204
#[test]
174205
fn fix_rspack_issue_6793() {
175-
let source1 = RawSource::Source("hello\n\n".to_string());
206+
let source1 = RawSource::from("hello\n\n".to_string());
176207
let source1 = ReplaceSource::new(source1);
177208
let source2 = OriginalSource::new("world".to_string(), "world.txt");
178209
let concat = ConcatSource::new([source1.boxed(), source2.boxed()]);

0 commit comments

Comments
 (0)