Skip to main content

SourceDocument

Struct SourceDocument 

Source
pub struct SourceDocument { /* private fields */ }
Expand description

Immutable source content retained by a compilation.

Implementations§

Source§

impl SourceDocument

Source

pub fn id(&self) -> SourceId

Return this document’s compilation-local identity.

Source

pub fn origin(&self) -> &SourceOrigin

Return this document’s stable physical or logical origin.

Source

pub fn label(&self) -> &str

Return the display label used for this document.

Source

pub fn bytes(&self) -> &[u8]

Return the immutable source bytes.

Source

pub fn len(&self) -> ByteOffset

Return the source length as a validated compiler byte offset.

Source

pub fn is_empty(&self) -> bool

Return whether this document contains no bytes.

Source

pub fn line_count(&self) -> usize

Return the number of logical lines.

Every document has at least one line. A trailing LF, CRLF, or lone CR creates a final empty line.

Source

pub fn offset(&self, offset: usize) -> Result<ByteOffset, SourceRangeError>

Validate and convert a byte index into a compiler byte offset.

The exclusive end-of-document offset is valid.

Source

pub fn range( &self, range: Range<usize>, ) -> Result<SourceRange, SourceRangeError>

Create a checked half-open byte range in this document.

Source

pub fn empty_range( &self, offset: usize, ) -> Result<SourceRange, SourceRangeError>

Create a checked empty range at a byte offset in this document.

Source

pub fn slice(&self, range: SourceRange) -> Result<&[u8], SourceRangeError>

Return the bytes covered by a checked source range.

Source

pub fn line_column( &self, offset: ByteOffset, ) -> Result<(usize, usize), SourceRangeError>

Convert a checked byte offset to a one-based line and byte column.

The exclusive end-of-document offset is valid. Columns count bytes; callers needing editor character encodings must convert explicitly.

Source

pub fn byte_position( &self, offset: ByteOffset, ) -> Result<BytePosition, PositionError>

Convert a byte offset to a zero-based byte position.

Every offset from zero through EOF is representable, including offsets on either byte of CRLF and offsets inside invalid UTF-8.

Source

pub fn byte_offset( &self, position: BytePosition, ) -> Result<ByteOffset, PositionError>

Convert a zero-based byte position to its byte offset.

On non-final lines, positions identify every terminator byte. The offset immediately after an LF, CRLF, or lone CR is column zero of the next line. On the final line, its end position identifies EOF.

Source

pub fn position( &self, offset: ByteOffset, encoding: PositionEncoding, ) -> Result<Position, PositionError>

Convert a byte offset to an editor position in an explicit encoding.

The source must be valid UTF-8, and the offset must lie on a Unicode scalar boundary. Line terminators are excluded from editor columns. The start of LF, CRLF, or lone CR maps to the line’s end position; the offset between CR and LF has no editor-position representation.

Source

pub fn position_offset( &self, position: Position, encoding: PositionEncoding, ) -> Result<ByteOffset, PositionError>

Convert an editor position in an explicit encoding to a byte offset.

Lines and characters are zero-based. End-of-line positions map to the first byte of LF, CRLF, or lone CR; a trailing terminator creates a final empty line whose zero position maps to EOF.

Trait Implementations§

Source§

impl Debug for SourceDocument

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more