[][src]Trait codespan_reporting::files::Files

pub trait Files<'a> {
    type FileId: 'a + Copy + PartialEq;
    type Name: 'a + Display;
    type Source: 'a + AsRef<str>;
    fn name(&'a self, id: Self::FileId) -> Option<Self::Name>;
fn source(&'a self, id: Self::FileId) -> Option<Self::Source>;
fn line_index(
        &'a self,
        id: Self::FileId,
        byte_index: usize
    ) -> Option<usize>;
fn line_range(
        &'a self,
        id: Self::FileId,
        line_index: usize
    ) -> Option<Range<usize>>; fn line_number(
        &'a self,
        id: Self::FileId,
        line_index: usize
    ) -> Option<usize> { ... }
fn column_number(
        &'a self,
        id: Self::FileId,
        line_index: usize,
        byte_index: usize
    ) -> Option<usize> { ... }
fn location(
        &'a self,
        id: Self::FileId,
        byte_index: usize
    ) -> Option<Location> { ... } }

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.

Associated Types

type FileId: 'a + Copy + PartialEq

A unique identifier for files in the file provider. This will be used for rendering diagnostic::Labels in the corresponding source files.

type Name: 'a + Display

The user-facing name of a file, to be displayed in diagnostics.

type Source: 'a + AsRef<str>

The source code of a file.

Loading content...

Required methods

fn name(&'a self, id: Self::FileId) -> Option<Self::Name>

The user-facing name of a file.

fn source(&'a self, id: Self::FileId) -> Option<Self::Source>

The source code of a file.

fn line_index(&'a self, id: Self::FileId, byte_index: usize) -> Option<usize>

The index of the line at the given byte index.

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.

fn line_range(
    &'a self,
    id: Self::FileId,
    line_index: usize
) -> Option<Range<usize>>

The byte range of line in the source of the file.

Loading content...

Provided methods

fn line_number(&'a self, id: Self::FileId, line_index: usize) -> Option<usize>

The user-facing line number at the given line index.

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.

fn column_number(
    &'a self,
    id: Self::FileId,
    line_index: usize,
    byte_index: usize
) -> Option<usize>

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.

fn location(&'a self, id: Self::FileId, byte_index: usize) -> Option<Location>

Convenience method for returning line and column number at the given a byte index in the file.

Loading content...

Implementors

impl<'a, Name, Source> Files<'a> for SimpleFile<Name, Source> where
    Name: 'a + Display + Clone,
    Source: 'a + AsRef<str>, 
[src]

type FileId = ()

type Name = Name

type Source = &'a str

impl<'a, Name, Source> Files<'a> for SimpleFiles<Name, Source> where
    Name: 'a + Display + Clone,
    Source: 'a + AsRef<str>, 
[src]

type FileId = usize

type Name = Name

type Source = &'a str

Loading content...