@@ -215,6 +215,215 @@ impl<'a> StreamChunks<'a> for RawSource {
215215 }
216216}
217217
218+ /// A string variant of [RawSource].
219+ ///
220+ /// - [webpack-sources docs](https://github.com/webpack/webpack-sources/#rawsource).
221+ ///
222+ /// ```
223+ /// use rspack_sources::{MapOptions, RawStringSource, Source};
224+ ///
225+ /// let code = "some source code";
226+ /// let s = RawStringSource::from(code.to_string());
227+ /// assert_eq!(s.source(), code);
228+ /// assert_eq!(s.map(&MapOptions::default()), None);
229+ /// assert_eq!(s.size(), 16);
230+ /// ```
231+ #[ derive( Clone , PartialEq , Eq ) ]
232+ pub struct RawStringSource ( Cow < ' static , str > ) ;
233+
234+ #[ cfg( any( target_arch = "x86_64" , target_arch = "aarch64" ) ) ]
235+ static_assertions:: assert_eq_size!( RawStringSource , [ u8 ; 24 ] ) ;
236+
237+ impl RawStringSource {
238+ /// Create a new [RawStringSource] from a static &str.
239+ ///
240+ /// ```
241+ /// use rspack_sources::{RawStringSource, Source};
242+ ///
243+ /// let code = "some source code";
244+ /// let s = RawStringSource::from_static(code);
245+ /// assert_eq!(s.source(), code);
246+ /// ```
247+ pub fn from_static ( s : & ' static str ) -> Self {
248+ Self ( Cow :: Borrowed ( s) )
249+ }
250+ }
251+
252+ impl From < String > for RawStringSource {
253+ fn from ( value : String ) -> Self {
254+ Self ( Cow :: Owned ( value) )
255+ }
256+ }
257+
258+ impl From < & str > for RawStringSource {
259+ fn from ( value : & str ) -> Self {
260+ Self ( Cow :: Owned ( value. to_owned ( ) ) )
261+ }
262+ }
263+
264+ impl Source for RawStringSource {
265+ fn source ( & self ) -> Cow < str > {
266+ Cow :: Borrowed ( & self . 0 )
267+ }
268+
269+ fn buffer ( & self ) -> Cow < [ u8 ] > {
270+ Cow :: Borrowed ( self . 0 . as_bytes ( ) )
271+ }
272+
273+ fn size ( & self ) -> usize {
274+ self . 0 . len ( )
275+ }
276+
277+ fn map ( & self , _: & MapOptions ) -> Option < SourceMap > {
278+ None
279+ }
280+
281+ fn to_writer ( & self , writer : & mut dyn std:: io:: Write ) -> std:: io:: Result < ( ) > {
282+ writer. write_all ( self . 0 . as_bytes ( ) )
283+ }
284+ }
285+
286+ impl std:: fmt:: Debug for RawStringSource {
287+ fn fmt (
288+ & self ,
289+ f : & mut std:: fmt:: Formatter < ' _ > ,
290+ ) -> Result < ( ) , std:: fmt:: Error > {
291+ let mut d = f. debug_tuple ( "RawStringSource" ) ;
292+ d. field ( & self . 0 . chars ( ) . take ( 50 ) . collect :: < String > ( ) ) ;
293+ d. finish ( )
294+ }
295+ }
296+
297+ impl Hash for RawStringSource {
298+ fn hash < H : Hasher > ( & self , state : & mut H ) {
299+ "RawStringSource" . hash ( state) ;
300+ self . buffer ( ) . hash ( state) ;
301+ }
302+ }
303+
304+ impl < ' a > StreamChunks < ' a > for RawStringSource {
305+ fn stream_chunks (
306+ & ' a self ,
307+ options : & MapOptions ,
308+ on_chunk : OnChunk < ' _ , ' a > ,
309+ on_source : OnSource < ' _ , ' a > ,
310+ on_name : OnName < ' _ , ' a > ,
311+ ) -> crate :: helpers:: GeneratedInfo {
312+ if options. final_source {
313+ get_generated_source_info ( & self . source ( ) )
314+ } else {
315+ stream_chunks_of_raw_source (
316+ & self . 0 , options, on_chunk, on_source, on_name,
317+ )
318+ }
319+ }
320+ }
321+
322+ /// A buffer variant of [RawSource].
323+ ///
324+ /// - [webpack-sources docs](https://github.com/webpack/webpack-sources/#rawsource).
325+ ///
326+ /// ```
327+ /// use rspack_sources::{MapOptions, RawBufferSource, Source};
328+ ///
329+ /// let code = "some source code".as_bytes();
330+ /// let s = RawBufferSource::from(code);
331+ /// assert_eq!(s.buffer(), code);
332+ /// assert_eq!(s.map(&MapOptions::default()), None);
333+ /// assert_eq!(s.size(), 16);
334+ /// ```
335+ #[ derive( Clone , PartialEq , Eq ) ]
336+ pub struct RawBufferSource {
337+ value : Vec < u8 > ,
338+ value_as_string : OnceLock < String > ,
339+ }
340+
341+ impl From < Vec < u8 > > for RawBufferSource {
342+ fn from ( value : Vec < u8 > ) -> Self {
343+ Self {
344+ value,
345+ value_as_string : Default :: default ( ) ,
346+ }
347+ }
348+ }
349+
350+ impl From < & [ u8 ] > for RawBufferSource {
351+ fn from ( value : & [ u8 ] ) -> Self {
352+ Self {
353+ value : value. to_vec ( ) ,
354+ value_as_string : Default :: default ( ) ,
355+ }
356+ }
357+ }
358+
359+ impl Source for RawBufferSource {
360+ fn source ( & self ) -> Cow < str > {
361+ Cow :: Borrowed (
362+ self
363+ . value_as_string
364+ . get_or_init ( || String :: from_utf8_lossy ( & self . value ) . to_string ( ) ) ,
365+ )
366+ }
367+
368+ fn buffer ( & self ) -> Cow < [ u8 ] > {
369+ Cow :: Borrowed ( & self . value )
370+ }
371+
372+ fn size ( & self ) -> usize {
373+ self . value . len ( )
374+ }
375+
376+ fn map ( & self , _: & MapOptions ) -> Option < SourceMap > {
377+ None
378+ }
379+
380+ fn to_writer ( & self , writer : & mut dyn std:: io:: Write ) -> std:: io:: Result < ( ) > {
381+ writer. write_all ( & self . value )
382+ }
383+ }
384+
385+ impl std:: fmt:: Debug for RawBufferSource {
386+ fn fmt (
387+ & self ,
388+ f : & mut std:: fmt:: Formatter < ' _ > ,
389+ ) -> Result < ( ) , std:: fmt:: Error > {
390+ let mut d = f. debug_tuple ( "RawBufferSource" ) ;
391+ d. field ( & self . value . iter ( ) . take ( 50 ) . copied ( ) . collect :: < Vec < u8 > > ( ) ) ;
392+ d. finish ( )
393+ }
394+ }
395+
396+ impl Hash for RawBufferSource {
397+ fn hash < H : Hasher > ( & self , state : & mut H ) {
398+ "RawBufferSource" . hash ( state) ;
399+ self . buffer ( ) . hash ( state) ;
400+ }
401+ }
402+
403+ impl < ' a > StreamChunks < ' a > for RawBufferSource {
404+ fn stream_chunks (
405+ & ' a self ,
406+ options : & MapOptions ,
407+ on_chunk : OnChunk < ' _ , ' a > ,
408+ on_source : OnSource < ' _ , ' a > ,
409+ on_name : OnName < ' _ , ' a > ,
410+ ) -> crate :: helpers:: GeneratedInfo {
411+ if options. final_source {
412+ get_generated_source_info ( & self . source ( ) )
413+ } else {
414+ stream_chunks_of_raw_source (
415+ self
416+ . value_as_string
417+ . get_or_init ( || String :: from_utf8_lossy ( & self . value ) . to_string ( ) ) ,
418+ options,
419+ on_chunk,
420+ on_source,
421+ on_name,
422+ )
423+ }
424+ }
425+ }
426+
218427#[ cfg( test) ]
219428mod tests {
220429 use crate :: { ConcatSource , OriginalSource , ReplaceSource , SourceExt } ;
0 commit comments