Skip to content

This tutorial walks you through the golden path of Sourcier: registering two source files, finalizing the map, looking files up by path and id, packing a span into a single u64, and extracting the exact bytes that span covers. You need Rust 1.99 or later and a terminal. The whole tutorial takes about ten minutes.

  • Rust 1.99+ installed through rustup. Check yours with rustc --version.
  • A terminal with cargo on your PATH.
  1. Create a new binary crate and enter it:

    Terminal
    cargo new span-demo
    cd span-demo
  2. Add Sourcier as a dependency:

    Terminal
    cargo add sourcier

    Cargo adds the current 2.0 release to Cargo.toml.

  3. Replace the contents of src/main.rs with the code from the next step.

Sourcier uses a two-phase lifecycle: you register files first, then call finalize() to sort them, deduplicate registrations and assign stable one-based ids.

Add this code to src/main.rs:

src/main.rs
use sourcier::SourceFilesMap;
fn main() {
// u8 ids support up to 255 files; u16 supports 65,535.
let mut files = SourceFilesMap::<u8>::new();
files
.add_file("src/main.rs", b"fn main() {\\n println!(\"Hello\");\\n}")
.expect("capacity available");
files
.add_file("src/lib.rs", b"pub mod utils;")
.expect("capacity available");
// Sorts by path, deduplicates, assigns one-based ids.
files.finalize().expect("no capacity issue");
println!("registered {} files", files.len());
}

Run the program:

Terminal
cargo run

You see the expected output:

Terminal window
registered 2 files

After finalization, the map answers queries in constant time. Extend main:

// Path to id, and id back to path.
let id = files.get_id("src/main.rs").expect("file present");
assert_eq!(files.get_path(id), Some("src/main.rs"));
// Ids are assigned in sorted path order, starting at 1.
assert_eq!(id, 1);
// Id to full borrowed entry.
let entry = files.get(id).expect("file present");
assert_eq!(entry.path, "src/main.rs");
assert!(entry.content.starts_with(b"fn main"));

src/lib.rs sorts before src/main.rs, so it receives id 1 and src/main.rs receives id 2. Sorted ids are stable: the same set of files always produces the same ids, which makes them safe to persist.

AbsolutePosition packs five fields — file id, start line, start column, end line and end column — into one u64. Lines and columns are one-based and the end is inclusive:

src/main.rs
use sourcier::{AbsolutePosition, SourceFilePosition};
let pos = AbsolutePosition::new(id, 2, 5, 2, 22);
assert_eq!(pos.file_id(), id);
assert_eq!(pos.start_line(), 2);
assert_eq!(pos.start_column(), 5);
assert_eq!(pos.end_line(), 2);
assert_eq!(pos.end_column(), 22);

AbsolutePosition::new is infallible and zero-cost: values are encoded as-is. Use try_new when you want the span validated as a well-formed, one-based region:

assert!(AbsolutePosition::<u8>::try_new(1, 2, 1, 1, 1).is_err());
assert!(AbsolutePosition::<u8>::try_new(1, 1, 1, 1, 1).is_ok());

With the default view feature, the map bridges positions back to the bytes they cover:

assert_eq!(
files.view(id, &pos),
Some(&b"println!(\"Hello\");"[..])
);

view returns the bytes of the span including any line terminators it covers. For UTF-8 source you can use view_str, which returns &str and yields None when the span cuts a multi-byte character.

Your program now registers files, looks them up, packs positions and extracts source bytes. Print the extracted view at the end of main to see it in action:

if let Some(bytes) = files.view(id, &pos) {
println!("{}", String::from_utf8_lossy(bytes));
}
Terminal window
println!("Hello");

You completed the golden path. From here: