Use this guide to handle the two error enums Sourcier returns:
MapError for building or querying a map, and PositionError for checked
position constructors. Both enums are #[non_exhaustive], so match arms
outside the crate always include a catch-all arm.
Match MapError variants
Section titled “Match MapError variants”MapError has five variants. This example recovers from each one
appropriately:
fn register(builder: &mut SourceFilesBuilder<u8>, path: &str, data: &[u8]) { if let Err(err) = builder.add_file_mut(path, data) { match err { MapError::TooManyFiles { max } => { eprintln!("id type exhausted: at most {max} files"); // Recover: switch to u16 ids, or stop accepting files. } MapError::FileTooLarge { size } => { eprintln!("file of {size} bytes exceeds the 4 GiB line-offset limit"); // Recover: skip the file; views resolve to None for it. } MapError::PathNotUtf8 { path } => { eprintln!("skipping non-UTF-8 path: {path}"); // Recover: skip; the path carries U+FFFD replacements. } // The map cannot be modified after finalization; with the // type-state builder these two variants are unrepresentable, // but the raw map can still report them. MapError::Finalized | MapError::AlreadyFinalized => { unreachable!("builder guarantees the build phase") } // non_exhaustive: always keep a catch-all arm. other => { eprintln!("unexpected map error: {other}"); } } }}The variants and when they occur:
| Variant | Fields | Occurs when |
|---|---|---|
TooManyFiles |
max: usize |
Registration or finalization exceeds the id type capacity (255 for u8, 65,535 for u16) |
AlreadyFinalized |
— | finalize is called on an already finalized map |
Finalized |
— | The finalized map is mutated (add_file, add_files, add_path) |
FileTooLarge |
size: u64 |
A file exceeds the 4 GiB u32 line-offset limit |
PathNotUtf8 |
path: String |
add_path receives a path that is not valid UTF-8 |
Handle checked position failures
Section titled “Handle checked position failures”PositionError has a single variant, InvalidRange, which carries the
rejected span:
match AbsolutePosition::<u8>::try_new(1, 2, 1, 1, 1) { Ok(pos) => println!("packed as {}", pos.as_raw()), Err(PositionError::InvalidRange { start_line, start_col, end_line, end_col }) => { eprintln!( "invalid span {start_line}:{start_col}-{end_line}:{end_col}" ); } Err(_) => unreachable!("only one variant exists today"),}A valid span satisfies all of the following:
start_line >= 1andend_line >= 1start_col >= 1andend_col >= 1start_line < end_line, orstart_line == end_line && start_col <= end_col
Read the display strings
Section titled “Read the display strings”Both enums implement std::error::Error through thiserror, and their
display strings are part of the API surface and covered by snapshot tests:
cannot register more than 255 files for this file id typethe source files map has already been finalizedcannot modify the source files map after finalizationfile content of 4294967296 bytes exceeds the 4 GiB line offset limitpath is not valid UTF-8: caf<U+FFFD>.txtinvalid position range: start 2:1 must not come after end 1:1 and all lines and columns must be at least 1Plan for capacity up front
Section titled “Plan for capacity up front”MapError::TooManyFiles is the variant most tools meet first. Choose the id
type before registration starts: u8 addresses 255 files, u16 addresses
65,535. Check remaining_capacity on a builder to fail early with your own
message instead of surfacing the library error.
Next steps
Section titled “Next steps”- Follow to avoid capacity errors in the first place.
- Check the for the complete variant list with field tables.
- Read to see how
non_exhaustiveinteracts with the semver policy.
