pub struct SourceFile { /* private fields */ }
Expand description
A SourceFile represents a single file stored in a super::SourceManager
Implementations§
Source§impl SourceFile
impl SourceFile
Sourcepub fn new(
id: SourceId,
lang: SourceLanguage,
uri: Uri,
content: impl Into<Box<str>>,
) -> Self
pub fn new( id: SourceId, lang: SourceLanguage, uri: Uri, content: impl Into<Box<str>>, ) -> Self
Create a new SourceFile from its raw components
Sourcepub fn from_raw_parts(id: SourceId, content: SourceContent) -> Self
pub fn from_raw_parts(id: SourceId, content: SourceContent) -> Self
This function is intended for use by super::SourceManager implementations that need to construct a SourceFile from its raw components (i.e. the identifier for the source file and its content).
Since the only entity that should be constructing a SourceId is a super::SourceManager, it is only valid to call this function in one of two scenarios:
- You are a super::SourceManager constructing a SourceFile after allocating a SourceId
- You pass
SourceId::default()
, i.e.SourceId::UNKNOWN
for the source identifier. The resulting SourceFile will be valid and safe to use in a context where there isn’t a super::SourceManager present. If there is a source manager in use, then constructing detached SourceFiles is not recommended, because it will make it confusing to determine whether a given SourceFile reference is safe to use.
You should rarely, if ever, fall in camp 2 - but it can be handy in some narrow cases
Sourcepub fn content(&self) -> &SourceContent
pub fn content(&self) -> &SourceContent
Returns a reference to the underlying SourceContent
Sourcepub fn content_mut(&mut self) -> &mut SourceContent
pub fn content_mut(&mut self) -> &mut SourceContent
Returns a mutable reference to the underlying SourceContent
Sourcepub fn line_count(&self) -> usize
pub fn line_count(&self) -> usize
Returns the number of lines in this file
Sourcepub fn source_span(&self) -> SourceSpan
pub fn source_span(&self) -> SourceSpan
Returns a SourceSpan covering the entirety of this file
Sourcepub fn source_slice(&self, span: impl Into<Range<usize>>) -> Option<&str>
pub fn source_slice(&self, span: impl Into<Range<usize>>) -> Option<&str>
Returns a subset of the underlying content as a string slice.
The bounds of the given span are character indices, not byte indices.
Returns None
if the given span is out of bounds
Sourcepub fn slice(self: &Arc<Self>, span: impl Into<Range<u32>>) -> SourceFileRef
pub fn slice(self: &Arc<Self>, span: impl Into<Range<u32>>) -> SourceFileRef
Returns a SourceFileRef corresponding to the bytes contained in the specified span.
Sourcepub fn line_column_to_span(
&self,
line: LineNumber,
column: ColumnNumber,
) -> Option<SourceSpan>
pub fn line_column_to_span( &self, line: LineNumber, column: ColumnNumber, ) -> Option<SourceSpan>
Get a SourceSpan which points to the first byte of the character at column
on line
Returns None
if the given line/column is out of bounds for this file.
Sourcepub fn location(&self, span: SourceSpan) -> FileLineCol
pub fn location(&self, span: SourceSpan) -> FileLineCol
Get a FileLineCol equivalent to the start of the given SourceSpan
Trait Implementations§
Source§impl AsRef<[u8]> for SourceFile
impl AsRef<[u8]> for SourceFile
Source§impl AsRef<str> for SourceFile
impl AsRef<str> for SourceFile
Source§impl Clone for SourceFile
impl Clone for SourceFile
Source§fn clone(&self) -> SourceFile
fn clone(&self) -> SourceFile
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for SourceFile
impl Debug for SourceFile
Source§impl<'de> Deserialize<'de> for SourceFile
impl<'de> Deserialize<'de> for SourceFile
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl Hash for SourceFile
impl Hash for SourceFile
Source§impl Ord for SourceFile
impl Ord for SourceFile
Source§fn cmp(&self, other: &SourceFile) -> Ordering
fn cmp(&self, other: &SourceFile) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq for SourceFile
impl PartialEq for SourceFile
Source§impl PartialOrd for SourceFile
impl PartialOrd for SourceFile
Source§impl Serialize for SourceFile
impl Serialize for SourceFile
Source§impl SourceCode for SourceFile
impl SourceCode for SourceFile
Source§fn read_span<'a>(
&'a self,
span: &SourceSpan,
context_lines_before: usize,
context_lines_after: usize,
) -> Result<Box<dyn SpanContents<'a> + 'a>, MietteError>
fn read_span<'a>( &'a self, span: &SourceSpan, context_lines_before: usize, context_lines_after: usize, ) -> Result<Box<dyn SpanContents<'a> + 'a>, MietteError>
SourceCode
, keeping a
certain number of lines before and after the span as context.