srcmap-sourcemap
High-performance source map parser and consumer for Rust.
Parses source map JSON and provides O(log n) position lookups. Implements the ECMA-426 Source Map v3 specification. Drop-in Rust equivalent of @jridgewell/trace-mapping.
Install
[]
= "0.1"
Usage
use SourceMap;
let json = r#"{"version":3,"sources":["input.js"],"names":[],"mappings":"AAAA;AACA"}"#;
let sm = from_json.unwrap;
// Forward lookup: generated position -> original position (0-based)
let loc = sm.original_position_for.unwrap;
assert_eq!;
assert_eq!;
assert_eq!;
// Reverse lookup: original position -> generated position
let pos = sm.generated_position_for.unwrap;
assert_eq!;
assert_eq!;
API
SourceMap
| Method | Description |
|---|---|
from_json(json) -> Result<SourceMap> |
Parse a source map from a JSON string |
original_position_for(line, col) -> Option<OriginalLocation> |
Look up original position for a generated position |
generated_position_for(source, line, col) -> Option<GeneratedPosition> |
Reverse lookup |
all_mappings() -> &[Mapping] |
Iterate all decoded mappings |
source(index) -> &str |
Resolve a source index to its filename |
name(index) -> &str |
Resolve a name index to its string |
line_count() -> usize |
Number of generated lines |
mapping_count() -> usize |
Total number of decoded mappings |
Fields
| Field | Type |
|---|---|
sources |
Vec<String> |
sources_content |
Vec<Option<String>> |
names |
Vec<String> |
file |
Option<String> |
source_root |
Option<String> |
ignore_list |
Vec<u32> |
Features
- O(log n) binary search for both forward and reverse lookups
- Flat 24-byte mapping structs for cache-friendly iteration
- Lazy reverse index built on first
generated_position_forcall - Indexed source maps (
sections) with automatic flattening - Zero-copy JSON parsing via borrowed mappings string
- Robust error handling for malformed input
Performance
Benchmarked on a 100K segment source map (Criterion):
| Operation | Time |
|---|---|
| Parse | 701 us |
| Single lookup | 3 ns |
| 1000x lookups | 5.8 us (5.8 ns/lookup) |
Part of srcmap
This crate is the core of the srcmap source map toolkit. See also:
srcmap-codec- VLQ encode/decodesrcmap-generator- Source map buildersrcmap-remapping- Concatenation and composition
License
MIT