srcmap-codec
High-performance VLQ source map codec for Rust.
Encodes and decodes source map mappings strings using the Base64 VLQ format specified in ECMA-426 (Source Map v3). Drop-in Rust equivalent of @jridgewell/sourcemap-codec.
Install
[]
= "0.1"
Usage
Decode and encode mappings
use ;
let mappings = decode.unwrap;
assert_eq!; // 2 lines
assert_eq!; // first segment
let encoded = encode;
assert_eq!;
Low-level VLQ primitives
use ;
let mut buf = Vecnew;
vlq_encode;
let = vlq_decode.unwrap;
assert_eq!;
Parallel encoding
Enable the parallel feature for multi-threaded encoding via rayon. ~1.5x faster for large maps (5K+ lines).
[]
= { = "0.1", = ["parallel"] }
use encode_parallel;
let encoded = encode_parallel;
API
| Function | Description |
|---|---|
decode(mappings) -> Result<SourceMapMappings> |
Decode a VLQ mappings string into lines of segments |
encode(mappings) -> String |
Encode decoded mappings back to a VLQ string |
encode_parallel(mappings) -> String |
Parallel encoding via rayon (requires parallel feature) |
vlq_decode(bytes, offset) -> Result<(i64, usize)> |
Decode a single VLQ value at the given byte offset |
vlq_encode(buf, value) |
Encode a single VLQ value and append to buffer |
Types
type Segment = ; // 1, 4, or 5 fields
type Line = ; // segments on one generated line
type SourceMapMappings = ; // all lines
Segments have 1, 4, or 5 fields:
- 1 field:
[generated_column] - 4 fields:
[generated_column, source_index, original_line, original_column] - 5 fields:
[generated_column, source_index, original_line, original_column, name_index]
Performance
Inlined VLQ decoder with a single-char fast path covering values -15..15 (~85% of real-world source map values).
Part of srcmap
This crate is the foundation of the srcmap source map toolkit. See the main repository for the full suite including parser, generator, and remapping.
License
MIT