Installation
[]
= "1.0"
Or from the terminal:
Usage
Add sources to a map and resolve a global position back to the file and local offset it came from.
use ;
let mut map = new;
let main = map.add?; // global 0..12
let util = map.add?; // global 12..26
// Which file does global position 13 belong to, and where inside it?
let = map.locate.expect;
assert_eq!;
assert_eq!; // 13 - 12
// The id is a stable handle back to the source for the life of the map.
assert_eq!;
# Ok::
Read the located text back out of the resolved source:
use ;
let mut map = new;
map.add?;
let two = map.add?;
let = map.locate.expect;
assert_eq!;
let file = map.source.unwrap;
assert_eq!;
# Ok::
Resolve a global position to its file and 1-based line/column in one step — what a
diagnostic renderer needs to print file:line:col:
use ;
let mut map = new;
map.add?; // global 0..9
let b = map.add?; // global 9..30
let = map.line_col.expect;
assert_eq!;
assert_eq!; // second line of b.rs
# Ok::
Load untrusted input — a file from disk or raw bytes from a buffer — through the same checks, so bad input is a defined error rather than a panic:
use ;
let mut map = new;
map.set_max_source_len; // cap any single source at 1 MiB
// Raw bytes are validated as UTF-8 before they are stored.
let id = map.add_bytes?;
assert_eq!;
// Non-UTF-8 input is rejected, naming the source.
let err = map.add_bytes.unwrap_err;
assert!;
# Ok::
With the default std feature, map.add_file("src/main.rs") reads a path from
disk through those same checks, rejecting an oversize file from its metadata before
a byte is read.
Walk every loaded source in order — id order is also global-offset order:
use SourceMap;
let mut map = new;
map.add?;
map.add?;
let names: = map.iter.map.collect;
assert_eq!;
# Ok::
With the serde feature, a whole SourceMap round-trips through any serde format —
its spans and ids are regenerated on load, so the layout is validated rather than
trusted:
let mut map = new;
map.add?;
let json = to_string?;
let restored: SourceMap = from_str?;
assert_eq!;
See docs/API.md for the full reference.
How it works
Sources are placed end to end in the order they are added: the first occupies
global offsets 0..len₀, the next len₀..len₀ + len₁, and so on. The ranges never
overlap, and because each base is the running total of all earlier sources, the
internal list stays sorted by offset — so locate is a binary search,
O(log files), that borrows the resolved source rather than copying it.
The shared space is 32 bits wide (the same envelope a single BytePos
addresses), so the combined length of every source is capped at 4 GiB;
overrunning it is a defined error, never a silent wrap into a neighbour's range.
Status
v1.0.0 is the stable release: the public API is frozen and follows Semantic Versioning, with no breaking changes before 2.0. The surface is the full source map — the SourceMap, stable SourceIds, and the non-overlapping global position space with its O(log files) resolver, disk and buffer loading, line_col resolution, and optional serde — each invariant property-tested against a naive linear scan, verified on Linux, macOS, and Windows. See the SemVer promise.
Contributing
See dev/DIRECTIVES.md for engineering standards and the definition of done. Before a PR: cargo fmt --all, cargo clippy --all-targets --all-features -- -D warnings, and cargo test --all-features must be clean.