srcmap-codec 0.1.3

High-performance VLQ source map codec
Documentation

srcmap-codec

crates.io docs.rs CI Coverage

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

[dependencies]
srcmap-codec = "0.1"

Usage

Decode and encode mappings

use srcmap_codec::{decode, encode};

let mappings = decode("AAAA;AACA,EAAE").unwrap();
assert_eq!(mappings.len(), 2); // 2 lines
assert_eq!(mappings[0][0], vec![0, 0, 0, 0]); // first segment

let encoded = encode(&mappings);
assert_eq!(encoded, "AAAA;AACA,EAAE");

Low-level VLQ primitives

use srcmap_codec::{vlq_decode, vlq_encode};

let mut buf = Vec::new();
vlq_encode(&mut buf, 42);

let (value, bytes_read) = vlq_decode(&buf, 0).unwrap();
assert_eq!(value, 42);

Parallel encoding

Enable the parallel feature for multi-threaded encoding via rayon. ~1.5x faster for large maps (5K+ lines).

[dependencies]
srcmap-codec = { version = "0.1", features = ["parallel"] }
use srcmap_codec::encode_parallel;

let encoded = encode_parallel(&mappings);

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 = Vec<i64>;          // 1, 4, or 5 fields
type Line = Vec<Segment>;         // segments on one generated line
type SourceMapMappings = Vec<Line>; // 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