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.
Prerequisites
Section titled “Prerequisites”- Rust 1.99+ installed through rustup. Check yours with
rustc --version. - A terminal with
cargoon yourPATH.
Create a demo project
Section titled “Create a demo project”-
Create a new binary crate and enter it:
Terminal cargo new span-democd span-demo -
Add Sourcier as a dependency:
Terminal cargo add sourcierCargo adds the current
2.0release toCargo.toml. -
Replace the contents of
src/main.rswith the code from the next step.
Register files and finalize
Section titled “Register files and finalize”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:
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:
cargo runYou see the expected output:
registered 2 filesLook up files by path and id
Section titled “Look up files by path and id”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.
Pack a span into a u64
Section titled “Pack a span into a u64”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:
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());Extract the exact bytes of the span
Section titled “Extract the exact bytes of the span”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));}println!("Hello");Next steps
Section titled “Next steps”You completed the golden path. From here:
- Follow for loops, batches,
PathBufregistration and duplicate handling. - Follow for conversions, ordering and serialization.
- Browse the for the complete method list.
