[][src]Struct codespan::Files

pub struct Files { /* fields omitted */ }

A database of source files.

Methods

impl Files[src]

pub fn new() -> Files[src]

Create a new, empty database of files.

pub fn add(
    &mut self,
    name: impl Into<String>,
    source: impl Into<String>
) -> FileId
[src]

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

pub fn update(&mut self, file_id: FileId, source: impl Into<String>)[src]

Update a source file in place.

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

pub fn name(&self, file_id: FileId) -> &str[src]

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);

pub fn line_span(
    &self,
    file_id: FileId,
    line_index: impl Into<LineIndex>
) -> Result<Span, LineIndexOutOfBoundsError>
[src]

Get the span at the given line index.

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

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

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

assert_eq!(
    line_sources,
    [
        Ok(Span::new(0, 4)),    // 0: "foo\n"
        Ok(Span::new(4, 9)),    // 1: "bar\r\n"
        Ok(Span::new(9, 10)),   // 2: ""
        Ok(Span::new(10, 13)),  // 3: "baz"
        Err(LineIndexOutOfBoundsError {
            given: LineIndex::from(5),
            max: LineIndex::from(4),
        }),
    ]
);

pub fn location(
    &self,
    file_id: FileId,
    byte_index: impl Into<ByteIndex>
) -> Result<Location, LocationError>
[src]

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

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

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

assert_eq!(files.location(file_id, 0), Ok(Location::new(0, 0)));
assert_eq!(files.location(file_id, 7), Ok(Location::new(1, 3)));
assert_eq!(files.location(file_id, 8), Ok(Location::new(1, 4)));
assert_eq!(files.location(file_id, 9), Ok(Location::new(2, 0)));
assert_eq!(
    files.location(file_id, 100),
    Err(LocationError::OutOfBounds {
        given: ByteIndex::from(100),
        span: Span::new(0, 13),
    }),
);

pub fn source(&self, file_id: FileId) -> &str[src]

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);

pub fn source_span(&self, file_id: FileId) -> Span[src]

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));

pub fn source_slice(
    &self,
    file_id: FileId,
    span: impl Into<Span>
) -> Result<&str, SpanOutOfBoundsError>
[src]

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)), Ok("hello"));
assert!(files.source_slice(file_id, Span::new(0, 100)).is_err());

Trait Implementations

impl Clone for Files[src]

impl Debug for Files[src]

Auto Trait Implementations

impl Send for Files

impl Unpin for Files

impl Sync for Files

impl UnwindSafe for Files

impl RefUnwindSafe for Files

Blanket Implementations

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]