@@ -12,6 +12,12 @@ use crate::{
12
12
MapOptions , Source , SourceMap ,
13
13
} ;
14
14
15
+ #[ derive( Clone , PartialEq , Eq ) ]
16
+ enum RawValue {
17
+ Buffer ( Vec < u8 > ) ,
18
+ String ( String ) ,
19
+ }
20
+
15
21
/// Represents source code without source map, it will not create source map for the source code.
16
22
///
17
23
/// - [webpack-sources docs](https://github.com/webpack/webpack-sources/#rawsource).
@@ -25,64 +31,88 @@ use crate::{
25
31
/// assert_eq!(s.map(&MapOptions::default()), None);
26
32
/// assert_eq!(s.size(), 16);
27
33
/// ```
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
+ }
34
46
}
35
47
48
+ impl Eq for RawSource { }
49
+
36
50
impl RawSource {
37
51
/// Whether the [RawSource] represent a buffer.
38
52
pub fn is_buffer ( & self ) -> bool {
39
- matches ! ( self , Self :: Buffer ( _ , _) )
53
+ matches ! ( self . value , RawValue :: Buffer ( _) )
40
54
}
41
55
}
42
56
43
57
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
+ }
46
63
}
47
64
}
48
65
49
66
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
+ }
52
72
}
53
73
}
54
74
55
75
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
+ }
58
81
}
59
82
}
60
83
61
84
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
+ }
64
90
}
65
91
}
66
92
67
93
impl Source for RawSource {
68
94
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
+ ) ,
72
102
}
73
103
}
74
104
75
105
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 ) ,
79
109
}
80
110
}
81
111
82
112
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 ( ) ,
86
116
}
87
117
}
88
118
@@ -91,9 +121,9 @@ impl Source for RawSource {
91
121
}
92
122
93
123
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 ,
97
127
} )
98
128
}
99
129
}
@@ -107,9 +137,9 @@ impl Hash for RawSource {
107
137
108
138
impl PartialEq for RawSource {
109
139
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,
113
143
_ => false ,
114
144
}
115
145
}
@@ -121,14 +151,14 @@ impl std::fmt::Debug for RawSource {
121
151
f : & mut std:: fmt:: Formatter < ' _ > ,
122
152
) -> Result < ( ) , std:: fmt:: Error > {
123
153
let mut d = f. debug_struct ( "RawSource" ) ;
124
- match self {
125
- Self :: Buffer ( buffer, _ ) => {
154
+ match & self . value {
155
+ RawValue :: Buffer ( buffer) => {
126
156
d. field (
127
157
"buffer" ,
128
158
& buffer. iter ( ) . take ( 50 ) . copied ( ) . collect :: < Vec < u8 > > ( ) ,
129
159
) ;
130
160
}
131
- Self :: Source ( string) => {
161
+ RawValue :: String ( string) => {
132
162
d. field ( "source" , & string. chars ( ) . take ( 50 ) . collect :: < String > ( ) ) ;
133
163
}
134
164
}
@@ -147,15 +177,16 @@ impl<'a> StreamChunks<'a> for RawSource {
147
177
if options. final_source {
148
178
get_generated_source_info ( & self . source ( ) )
149
179
} 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
153
184
. get_or_init ( || String :: from_utf8_lossy ( buffer) . to_string ( ) ) ;
154
185
stream_chunks_of_raw_source (
155
186
source, options, on_chunk, on_source, on_name,
156
187
)
157
188
}
158
- RawSource :: Source ( source) => stream_chunks_of_raw_source (
189
+ RawValue :: String ( source) => stream_chunks_of_raw_source (
159
190
source, options, on_chunk, on_source, on_name,
160
191
) ,
161
192
}
@@ -172,7 +203,7 @@ mod tests {
172
203
// Fix https://github.com/web-infra-dev/rspack/issues/6793
173
204
#[ test]
174
205
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 ( ) ) ;
176
207
let source1 = ReplaceSource :: new ( source1) ;
177
208
let source2 = OriginalSource :: new ( "world" . to_string ( ) , "world.txt" ) ;
178
209
let concat = ConcatSource :: new ( [ source1. boxed ( ) , source2. boxed ( ) ] ) ;
0 commit comments