pub trait Files<'a> {
type FileId: 'a + Copy + PartialEq;
type Name: 'a + Display;
type Source: 'a + AsRef<str>;
// Required methods
fn name(&'a self, id: Self::FileId) -> Result<Self::Name, Error>;
fn source(&'a self, id: Self::FileId) -> Result<Self::Source, Error>;
fn line_index(
&'a self,
id: Self::FileId,
byte_index: usize,
) -> Result<usize, Error>;
fn line_range(
&'a self,
id: Self::FileId,
line_index: usize,
) -> Result<Range<usize>, Error>;
// Provided methods
fn line_number(
&'a self,
id: Self::FileId,
line_index: usize,
) -> Result<usize, Error> { ... }
fn column_number(
&'a self,
id: Self::FileId,
line_index: usize,
byte_index: usize,
) -> Result<usize, Error> { ... }
fn location(
&'a self,
id: Self::FileId,
byte_index: usize,
) -> Result<Location, Error> { ... }
}
Expand description
A minimal interface for accessing source files when rendering diagnostics.
A lifetime parameter 'a
is provided to allow any of the returned values to returned by reference.
This is to workaround the lack of higher kinded lifetime parameters.
This can be ignored if this is not needed, however.
Required Associated Types§
Required Methods§
Sourcefn name(&'a self, id: Self::FileId) -> Result<Self::Name, Error>
fn name(&'a self, id: Self::FileId) -> Result<Self::Name, Error>
The user-facing name of a file.
Sourcefn source(&'a self, id: Self::FileId) -> Result<Self::Source, Error>
fn source(&'a self, id: Self::FileId) -> Result<Self::Source, Error>
The source code of a file.
Sourcefn line_index(
&'a self,
id: Self::FileId,
byte_index: usize,
) -> Result<usize, Error>
fn line_index( &'a self, id: Self::FileId, byte_index: usize, ) -> Result<usize, Error>
The index of the line at the given byte index. If the byte index is past the end of the file, returns the maximum line index in the file. This means that this function only fails if the file is not present.
§Note for trait implementors
This can be implemented efficiently by performing a binary search over
a list of line starts that was computed by calling the line_starts
function that is exported from the files
module. It might be useful
to pre-compute and cache these line starts.
Provided Methods§
Sourcefn line_number(
&'a self,
id: Self::FileId,
line_index: usize,
) -> Result<usize, Error>
fn line_number( &'a self, id: Self::FileId, line_index: usize, ) -> Result<usize, Error>
The user-facing line number at the given line index. It is not necessarily checked that the specified line index is actually in the file.
§Note for trait implementors
This is usually 1-indexed from the beginning of the file, but
can be useful for implementing something like the
C preprocessor’s #line
macro.
Sourcefn column_number(
&'a self,
id: Self::FileId,
line_index: usize,
byte_index: usize,
) -> Result<usize, Error>
fn column_number( &'a self, id: Self::FileId, line_index: usize, byte_index: usize, ) -> Result<usize, Error>
The user-facing column number at the given line index and byte index.
§Note for trait implementors
This is usually 1-indexed from the the start of the line.
A default implementation is provided, based on the column_index
function that is exported from the files
module.