pub struct Files<Source> { /* private fields */ }
Expand description

A database of source files.

The Source generic parameter determines how source text is stored. Using String will have Files take ownership of all source text. Smart pointer types such as Cow<'_, str>, Rc<str> or Arc<str> can be used to share the source text with the rest of the program.

Implementations

Create a new, empty database of files.

Add a file to the database, returning the handle that can be used to refer to it again.

Update a source file in place.

This will mean that any outstanding byte indexes will now point to invalid locations.

Get the name of the source file.

use codespan::Files;

let name = "test";

let mut files = Files::new();
let file_id = files.add(name, "hello world!");

assert_eq!(files.name(file_id), name);

Get the span at the given line index.

use codespan::{Files, LineIndex, Span};

let mut files = Files::new();
let file_id = files.add("test", "foo\nbar\r\n\nbaz");

let line_sources = (0..4)
    .map(|line| files.line_span(file_id, line).unwrap())
    .collect::<Vec<_>>();

assert_eq!(line_sources,
    [
        Span::new(0, 4),    // 0: "foo\n"
        Span::new(4, 9),    // 1: "bar\r\n"
        Span::new(9, 10),   // 2: ""
        Span::new(10, 13),  // 3: "baz"
    ]
);
assert!(files.line_span(file_id, 4).is_err());

Get the line index at the given byte in the source file.

use codespan::{Files, LineIndex};

let mut files = Files::new();
let file_id = files.add("test", "foo\nbar\r\n\nbaz");

assert_eq!(files.line_index(file_id, 0), LineIndex::from(0));
assert_eq!(files.line_index(file_id, 7), LineIndex::from(1));
assert_eq!(files.line_index(file_id, 8), LineIndex::from(1));
assert_eq!(files.line_index(file_id, 9), LineIndex::from(2));
assert_eq!(files.line_index(file_id, 100), LineIndex::from(3));

Get the location at the given byte index in the source file.

use codespan::{ByteIndex, Files, Location, Span};

let mut files = Files::new();
let file_id = files.add("test", "foo\nbar\r\n\nbaz");

assert_eq!(files.location(file_id, 0).unwrap(), Location::new(0, 0));
assert_eq!(files.location(file_id, 7).unwrap(), Location::new(1, 3));
assert_eq!(files.location(file_id, 8).unwrap(), Location::new(1, 4));
assert_eq!(files.location(file_id, 9).unwrap(), Location::new(2, 0));
assert!(files.location(file_id, 100).is_err());

Get the source of the file.

use codespan::Files;

let source = "hello world!";

let mut files = Files::new();
let file_id = files.add("test", source);

assert_eq!(*files.source(file_id), source);

Return the span of the full source.

use codespan::{Files, Span};

let source = "hello world!";

let mut files = Files::new();
let file_id = files.add("test", source);

assert_eq!(files.source_span(file_id), Span::from_str(source));

Return a slice of the source file, given a span.

use codespan::{Files, Span};

let mut files = Files::new();
let file_id = files.add("test",  "hello world!");

assert_eq!(files.source_slice(file_id, Span::new(0, 5)).unwrap(), "hello");
assert!(files.source_slice(file_id, Span::new(0, 100)).is_err());

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

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

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

The source code of a file.

The user-facing name of a file.

The source code of a file.

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. Read more

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

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. Read more

The user-facing column number at the given line index and byte index. Read more

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

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.