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 url(&self) -> Option<Url> { ... }
fn is_empty(&self) -> bool { ... }
fn get_char_at(&self, offset: usize) -> Option<char> { ... }
fn get_text_from(&self, offset: usize) -> Cow<'_, str> { ... }
fn get_url(&self) -> Option<&Url> { ... }
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
- Different error handling requirements
All offsets exposed by this trait are simple text ranges from the start of this source.
Required Methods§
Sourcefn length(&self) -> usize
fn length(&self) -> usize
Get the length of this source.
This represents the total size of this source in bytes.
Sourcefn chunk_at(&self, offset: usize) -> TextChunk<'_>
fn chunk_at(&self, offset: usize) -> TextChunk<'_>
Returns a text chunk containing the specified offset.
Sourcefn get_text_in(&self, range: Range<usize>) -> Cow<'_, str>
fn get_text_in(&self, range: Range<usize>) -> Cow<'_, str>
Get the text content at the specified range.
The range is specified as simple offsets from the start of this source. The returned text should have any character encoding transformations already applied (e.g., Unicode escapes decoded, HTML entities resolved).
§Arguments
range- The byte range to extract text from (relative to this source)
§Returns
The text content in the specified range.
Provided Methods§
Sourcefn get_char_at(&self, offset: usize) -> Option<char>
fn get_char_at(&self, offset: usize) -> Option<char>
Get a single character at the specified 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
Sourcefn get_text_from(&self, offset: usize) -> Cow<'_, str>
fn get_text_from(&self, offset: usize) -> Cow<'_, str>
Sourcefn get_url(&self) -> Option<&Url>
fn get_url(&self) -> Option<&Url>
Get the URL of this source, if available.
This method returns a reference to the URL associated with this source, typically used for file-based sources or remote resources.
§Returns
An optional reference to the source URL, or None if no URL is available