pub struct SourceMap { /* private fields */ }Expand description
A storage for source files.
§Example
use span::{Span, source::{FileName, SourceMap, SourceFile}};
fn process_file(f: &SourceFile) -> Span {
Span {
offset: 2 + f.offset(),
len: 3,
}
}
let mut source = SourceMap::default();
let f = source.add_file(FileName::Annon, "ABCDEFG".into());
let span1 = process_file(f);
let f = source.add_file(FileName::Annon, "DEFGHIJK".into());
let span2 = process_file(f);
let slice1 = source.slice(&span1);
let slice2 = source.slice(&span2);
assert_eq!(slice1, Some("CDE"));
assert_eq!(slice2, Some("FGH"));Implementations§
Source§impl SourceMap
impl SourceMap
Sourcepub fn add_file(&mut self, fname: FileName, contents: Rc<str>) -> &SourceFile
pub fn add_file(&mut self, fname: FileName, contents: Rc<str>) -> &SourceFile
Adds a new SourceFile to the SourceMap
Returns a reference to the newly created file
Sourcepub fn add_file_fs(&mut self, path: PathBuf) -> Result<&SourceFile>
pub fn add_file_fs(&mut self, path: PathBuf) -> Result<&SourceFile>
Reads the given path and creates a SourceFile with it’s contents
Sourcepub fn add_file_annon(&mut self, contents: Rc<str>) -> &SourceFile
pub fn add_file_annon(&mut self, contents: Rc<str>) -> &SourceFile
Adds a new annonymous SourceFile (without a filename) to this SourceMap
Sourcepub fn get_file_of_span(&self, span: &Span) -> Option<&SourceFile>
pub fn get_file_of_span(&self, span: &Span) -> Option<&SourceFile>
Gets the corresponding SourceFile for the given span
§Example
use span::{Span, source::{SourceMap, FileName}};
let mut sm = SourceMap::new();
let (input1, offset1) =
sm.add_file(
FileName::from("file1.txt"),
"Hello world!".into()
)
.into_parts();
let (input2, offset2) =
sm.add_file(
FileName::from("file2.txt"),
"Another file :)".into()
)
.into_parts();
let span1 = Span {
offset: 3 + offset1,
len: 3,
};
let span2 = Span {
offset: 1 + offset2,
len: 4,
};
let file1 = sm.get_file_of_span(&span1).unwrap();
assert_eq!(file1.filename().unwrap(), "file1.txt");
let file2 = sm.get_file_of_span(&span2).unwrap();
assert_eq!(file2.filename().unwrap(), "file2.txt");Sourcepub fn get_file_for_offset(&self, offset: usize) -> Option<&SourceFile>
pub fn get_file_for_offset(&self, offset: usize) -> Option<&SourceFile>
Returns the file for a given offset
pub fn get_file_for_id(&self, id: &FileId) -> Option<&SourceFile>
Sourcepub fn get_base_offset_of_span(&self, span: &Span) -> Option<usize>
pub fn get_base_offset_of_span(&self, span: &Span) -> Option<usize>
Returns base offset for a Span. This is: the offset of the
file it belongs to.
If no file is found, returns 0
Sourcepub fn slice(&self, span: &Span) -> Option<&str>
pub fn slice(&self, span: &Span) -> Option<&str>
Slices the given Span
This function will compute the SourceFile for the span
and slice it
Sourcepub fn file_position(&self, span: &Span) -> Option<FilePosition>
pub fn file_position(&self, span: &Span) -> Option<FilePosition>
Returns the FilePosition for this Span
This function will compute the SourceFile for the span
and slice it
pub fn files(&self) -> &[SourceFile]
Sourcepub fn is_valid(&self, span: &Span) -> bool
pub fn is_valid(&self, span: &Span) -> bool
Returns true if this is a valid span, this is, if it finds
a matching SourceFile for it
Trait Implementations§
Auto Trait Implementations§
impl Freeze for SourceMap
impl RefUnwindSafe for SourceMap
impl !Send for SourceMap
impl !Sync for SourceMap
impl Unpin for SourceMap
impl UnwindSafe for SourceMap
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more