pub struct FileInformation { /* private fields */ }Expand description
Efficient file content analysis for location lookups
This struct stores metadata about a file that enables fast conversion from byte offsets to (row, column) positions without storing the full file content.
Implementations§
Source§impl FileInformation
impl FileInformation
Sourcepub fn new(content: &str) -> Self
pub fn new(content: &str) -> Self
Create file information by analyzing content
Scans the content once to build an index of line break positions. This enables O(log n) offset-to-location lookups via binary search.
§Example
use quarto_source_map::FileInformation;
let info = FileInformation::new("line 1\nline 2\nline 3");Sourcepub fn from_parts(line_breaks: Vec<usize>, total_length: usize) -> Self
pub fn from_parts(line_breaks: Vec<usize>, total_length: usize) -> Self
Create file information from pre-computed parts
This is useful when deserializing from formats that store line break information directly (like JSON).
§Example
use quarto_source_map::FileInformation;
let info = FileInformation::from_parts(vec![6, 13], 20);Sourcepub fn offset_to_location(
&self,
offset: usize,
content: &str,
) -> Option<Location>
pub fn offset_to_location( &self, offset: usize, content: &str, ) -> Option<Location>
Convert a byte offset to a Location with row and column
Uses binary search to find which line contains the offset. Runs in O(log n) time where n is the number of lines.
The column is computed as character count (not byte count) from the start of the line to the offset, which requires the content parameter.
Returns None if the offset is out of bounds.
§Example
use quarto_source_map::FileInformation;
let content = "hello\nworld";
let info = FileInformation::new(content);
let loc = info.offset_to_location(6, content).unwrap();
assert_eq!(loc.row, 1);
assert_eq!(loc.column, 0);Sourcepub fn total_length(&self) -> usize
pub fn total_length(&self) -> usize
Get the total length of the file in bytes
Sourcepub fn line_breaks(&self) -> &[usize]
pub fn line_breaks(&self) -> &[usize]
Get the line breaks array (byte offsets of newline characters)
Sourcepub fn line_count(&self) -> usize
pub fn line_count(&self) -> usize
Get the number of lines in the file
Trait Implementations§
Source§impl Clone for FileInformation
impl Clone for FileInformation
Source§fn clone(&self) -> FileInformation
fn clone(&self) -> FileInformation
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for FileInformation
impl Debug for FileInformation
Source§impl<'de> Deserialize<'de> for FileInformation
impl<'de> Deserialize<'de> for FileInformation
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 PartialEq for FileInformation
impl PartialEq for FileInformation
Source§fn eq(&self, other: &FileInformation) -> bool
fn eq(&self, other: &FileInformation) -> bool
self and other values to be equal, and is used by ==.