Skip to main content

Source

Trait Source 

Source
pub trait Source: Send + Sync {
    // Required methods
    fn length(&self) -> usize;
    fn chunk_at(&self, offset: usize) -> TextChunk<'_>;
    fn get_text_in(&self, range: Range<usize>) -> Cow<'_, str>;

    // Provided methods
    fn source_id(&self) -> Option<SourceId> { ... }
    fn is_empty(&self) -> bool { ... }
    fn get_char_at(&self, offset: usize) -> Option<char> { ... }
    fn get_text_from(&self, offset: usize) -> Cow<'_, str> { ... }
    fn find_char_from(&self, offset: usize, ch: char) -> Option<usize> { ... }
    fn find_str_from(&self, offset: usize, pattern: &str) -> Option<usize> { ... }
    fn syntax_error(&self, message: String, offset: usize) -> OakError { ... }
}
Expand description

Trait for abstract text sources.

This trait provides a unified interface for different text sources that may have:

  • Different character representations (Unicode escapes, HTML entities)
  • Different internal storage formats (contiguous, chunked, ropes)
  • Different error handling requirements

All offsets exposed by this trait are simple byte ranges from the start of this source.

Required Methods§

Source

fn length(&self) -> usize

Returns the total length of this source in bytes.

Source

fn chunk_at(&self, offset: usize) -> TextChunk<'_>

Returns a text chunk containing the specified byte offset.

This allows for efficient traversal of large or non-contiguous sources.

Source

fn get_text_in(&self, range: Range<usize>) -> Cow<'_, str>

Returns the text content within the specified byte range.

§Arguments
  • range - The byte range to extract text from.
§Returns

The text content in the specified range, potentially as a borrowed slice.

Provided Methods§

Source

fn source_id(&self) -> Option<SourceId>

Returns a unique identifier for this source, if available.

Useful for associating diagnostics with specific files.

Source

fn is_empty(&self) -> bool

Returns true if the source has no content.

Source

fn get_char_at(&self, offset: usize) -> Option<char>

Returns the character at the specified byte offset.

This method should handle any character encoding transformations and return the actual character that would be seen by the parser.

§Arguments
  • offset - The byte offset from the start of this source.
§Returns

The character at the specified offset, or None if the offset is invalid.

Source

fn get_text_from(&self, offset: usize) -> Cow<'_, str>

Returns the text from the specified byte offset to the end of the source.

§Arguments
  • offset - The byte offset to start from.
Source

fn find_char_from(&self, offset: usize, ch: char) -> Option<usize>

Finds the next occurrence of a character starting from an offset.

§Arguments
  • offset - The byte offset to start searching from.
  • ch - The character to search for.
§Returns

The absolute byte offset of the next occurrence, or None if not found.

Source

fn find_str_from(&self, offset: usize, pattern: &str) -> Option<usize>

Finds the next occurrence of a substring starting from an offset.

§Arguments
  • offset - The byte offset to start searching from.
  • pattern - The substring to search for.
§Returns

The absolute byte offset of the next occurrence, or None if not found.

Source

fn syntax_error(&self, message: String, offset: usize) -> OakError

Creates a syntax error with location information associated with this source.

§Arguments
  • message - The error message.
  • offset - The byte offset where the error occurred.

Implementations on Foreign Types§

Source§

impl Source for str

Source§

fn length(&self) -> usize

Source§

fn chunk_at(&self, offset: usize) -> TextChunk<'_>

Source§

fn get_text_in(&self, range: Range<usize>) -> Cow<'_, str>

Source§

impl<S: Source + ?Sized> Source for &S

Source§

fn length(&self) -> usize

Source§

fn chunk_at(&self, offset: usize) -> TextChunk<'_>

Source§

fn get_text_in(&self, range: Range<usize>) -> Cow<'_, str>

Source§

fn source_id(&self) -> Option<SourceId>

Source§

fn get_char_at(&self, offset: usize) -> Option<char>

Source§

fn get_text_from(&self, offset: usize) -> Cow<'_, str>

Source§

fn find_char_from(&self, offset: usize, ch: char) -> Option<usize>

Source§

fn find_str_from(&self, offset: usize, pattern: &str) -> Option<usize>

Source§

fn syntax_error(&self, message: String, offset: usize) -> OakError

Implementors§