[−][src]Struct codespan::Files
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
impl<Source> Files<Source> where
Source: AsRef<str>,
[src]
Source: AsRef<str>,
pub fn new() -> Self
[src]
Create a new, empty database of files.
pub fn add(&mut self, name: impl Into<OsString>, source: Source) -> 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: Source)
[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) -> &OsStr
[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, Error>
[src]
&self,
file_id: FileId,
line_index: impl Into<LineIndex>
) -> Result<Span, Error>
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());
pub fn line_index(
&self,
file_id: FileId,
byte_index: impl Into<ByteIndex>
) -> LineIndex
[src]
&self,
file_id: FileId,
byte_index: impl Into<ByteIndex>
) -> LineIndex
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));
pub fn location(
&self,
file_id: FileId,
byte_index: impl Into<ByteIndex>
) -> Result<Location, Error>
[src]
&self,
file_id: FileId,
byte_index: impl Into<ByteIndex>
) -> Result<Location, Error>
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());
pub fn source(&self, file_id: FileId) -> &Source
[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, Error>
[src]
&self,
file_id: FileId,
span: impl Into<Span>
) -> Result<&str, Error>
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
impl<Source: Clone> Clone for Files<Source>
[src]
impl<Source: Debug> Debug for Files<Source>
[src]
impl<Source> Default for Files<Source> where
Source: AsRef<str>,
[src]
Source: AsRef<str>,
impl<'a, Source> Files<'a> for Files<Source> where
Source: AsRef<str>,
[src]
Source: AsRef<str>,
type FileId = FileId
A unique identifier for files in the file provider. This will be used
for rendering diagnostic::Label
s in the corresponding source files. Read more
type Name = String
The user-facing name of a file, to be displayed in diagnostics.
type Source = &'a str
The source code of a file.
fn name(&self, id: FileId) -> Result<String, Error>
[src]
fn source(&'a self, id: FileId) -> Result<&str, Error>
[src]
fn line_index(&self, id: FileId, byte_index: usize) -> Result<usize, Error>
[src]
fn line_range(
&'a self,
id: FileId,
line_index: usize
) -> Result<Range<usize>, Error>
[src]
&'a self,
id: FileId,
line_index: usize
) -> Result<Range<usize>, Error>
fn line_number(
&'a self,
id: Self::FileId,
line_index: usize
) -> Result<usize, Error>
[src]
&'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>
[src]
&'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>
[src]
&'a self,
id: Self::FileId,
byte_index: usize
) -> Result<Location, Error>
Auto Trait Implementations
impl<Source> RefUnwindSafe for Files<Source> where
Source: RefUnwindSafe,
Source: RefUnwindSafe,
impl<Source> Send for Files<Source> where
Source: Send,
Source: Send,
impl<Source> Sync for Files<Source> where
Source: Sync,
Source: Sync,
impl<Source> Unpin for Files<Source> where
Source: Unpin,
Source: Unpin,
impl<Source> UnwindSafe for Files<Source> where
Source: UnwindSafe,
Source: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,