Skip to main content

StreamingGenerator

Struct StreamingGenerator 

Source
pub struct StreamingGenerator { /* private fields */ }
Expand description

Source map generator that encodes VLQ on-the-fly.

Unlike SourceMapGenerator, which collects all mappings and sorts them at finalization, StreamingGenerator encodes each mapping to VLQ immediately. Mappings must be added in sorted order (generated_line, generated_column).

This avoids intermediate Vec<Mapping> allocation, making it ideal for streaming composition pipelines.

§Examples

use srcmap_generator::StreamingGenerator;

let mut sg = StreamingGenerator::new(Some("bundle.js".to_string()));
let src = sg.add_source("src/app.ts");
sg.set_source_content(src, "const x = 1;".to_string());

// Mappings must be added in order
sg.add_mapping(0, 0, src, 0, 6);
sg.add_mapping(1, 0, src, 1, 0);

let json = sg.to_json();
assert!(json.contains(r#""version":3"#));

Implementations§

Source§

impl StreamingGenerator

Source

pub fn new(file: Option<String>) -> Self

Create a new streaming source map generator.

Source

pub fn set_source_root(&mut self, root: impl Into<String>)

Set the source root prefix.

Source

pub fn set_debug_id(&mut self, id: impl Into<String>)

Set the debug ID (UUID) for this source map (ECMA-426).

Source

pub fn add_source(&mut self, source: &str) -> u32

Register a source file and return its index.

Source

pub fn set_source_content( &mut self, source_idx: u32, content: impl Into<String>, )

Set the content for a source file.

Source

pub fn add_name(&mut self, name: &str) -> u32

Register a name and return its index.

Source

pub fn add_to_ignore_list(&mut self, source_idx: u32)

Add a source index to the ignore list.

Source

pub fn add_generated_mapping( &mut self, generated_line: u32, generated_column: u32, )

Add a mapping with no source information (generated-only).

Mappings must be added in sorted order (generated_line, generated_column).

Source

pub fn add_mapping( &mut self, generated_line: u32, generated_column: u32, source: u32, original_line: u32, original_column: u32, )

Add a mapping from generated position to original position.

Mappings must be added in sorted order (generated_line, generated_column).

Source

pub fn add_range_mapping( &mut self, generated_line: u32, generated_column: u32, source: u32, original_line: u32, original_column: u32, )

Add a range mapping from generated position to original position.

Same as add_mapping but marks this mapping as a range mapping (ECMA-426). Mappings must be added in sorted order (generated_line, generated_column).

Source

pub fn add_named_mapping( &mut self, generated_line: u32, generated_column: u32, source: u32, original_line: u32, original_column: u32, name: u32, )

Add a mapping with a name.

Mappings must be added in sorted order (generated_line, generated_column).

Source

pub fn add_named_range_mapping( &mut self, generated_line: u32, generated_column: u32, source: u32, original_line: u32, original_column: u32, name: u32, )

Add a named range mapping from generated position to original position.

Same as add_named_mapping but marks this mapping as a range mapping (ECMA-426). Mappings must be added in sorted order (generated_line, generated_column).

Source

pub fn mapping_count(&self) -> usize

Get the number of mappings added so far.

Source

pub fn to_json(&self) -> String

Generate the source map as a JSON string.

Source

pub fn to_decoded_map(&self) -> Result<SourceMap, ParseError>

Directly construct a SourceMap from the streaming generator’s state.

Parses the already-encoded VLQ mappings to build a decoded SourceMap. More efficient than to_json() + SourceMap::from_json() since it skips JSON generation and parsing.

§Panics

Panics if the internal VLQ-encoded mappings string is corrupted or contains invalid VLQ sequences. This is not expected under normal use, since the streaming encoder always produces valid output.

Trait Implementations§

Source§

impl Debug for StreamingGenerator

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.