pub struct SourcePosition { /* private fields */ }Expand description
Source position information for parsing, with dual column tracking.
This is a pure data struct with no mutation methods. Lexers are responsible for computing position values as they scan input.
This is standalone with no dependency on libgraphql-core. All fields are private with accessor methods.
§Indexing Convention
All position values are 0-based:
line: 0 = first line of the document (0-based)col_utf8: UTF-8 character count within the current line (0-based)col_utf16: Optional UTF-16 code unit offset within the current line (0-based)byte_offset: byte offset within the whole document (0-based)
§Dual Column Tracking
Two column representations are supported:
col_utf8(always available): Number of UTF-8 characters from the start of the current line. Increments by 1 for each character regardless of its byte representation. This is intuitive for users and matches what most text editors display as “column”.col_utf16(optional): UTF-16 code unit offset within the line. This aligns with LSP (Language Server Protocol) and many editors. It isSomewhen the token source can provide it (e.g.StrToGraphQLTokenSource), andNonewhen it cannot (e.g.RustMacroGraphQLTokenSourceinlibgraphql-macroswhich usesproc_macro2::Spanthat only provides UTF-8 char-based positions).
For ASCII text, both columns are equal. For text containing characters outside the Basic Multilingual Plane (e.g., emoji), they differ:
col_utf8advances by 1 for each UTF-8 charactercol_utf16advances by the character’s UTF-16 length (1 or 2 code units)
§Size
This struct is 20 bytes (4 × u32 + Option<u32>) and derives Copy
for cheap value semantics. The u32 fields support files up to 4 GB
and lines/columns up to ~4 billion — more than sufficient for any
realistic GraphQL document. Accessors return usize for ergonomic
interop with Rust’s standard indexing types.
Implementations§
Source§impl SourcePosition
impl SourcePosition
Sourcepub fn new(
line: usize,
col_utf8: usize,
col_utf16: Option<usize>,
byte_offset: usize,
) -> Self
pub fn new( line: usize, col_utf8: usize, col_utf16: Option<usize>, byte_offset: usize, ) -> Self
Create a new SourcePosition.
§Arguments
line: 0-based line number (0 = first line)col_utf8: 0-based UTF-8 character count within current linecol_utf16: 0-based UTF-16 code unit offset within current line, orNoneif not available (e.g., fromproc_macro2::Span)byte_offset: 0-based byte offset from document start
Sourcepub fn line_col(&self) -> (u32, u32)
pub fn line_col(&self) -> (u32, u32)
Returns the 0-based (line, col_utf8) tuple as u32
values.
This is a low-level accessor that returns the raw stored
u32 fields without converting to usize. Useful for
compact representations (e.g. serialization, span maps).
Sourcepub fn col_utf8(&self) -> usize
pub fn col_utf8(&self) -> usize
Returns the 0-based (UTF-8) character count within the current line.
This increments by 1 for each character regardless of byte representation. For example, both ‘a’ (1 byte) and ‘🎉’ (4 bytes) each add 1 to this count.
Sourcepub fn col_utf16(&self) -> Option<usize>
pub fn col_utf16(&self) -> Option<usize>
Returns the 0-based UTF-16 code unit offset within the current line, if available.
This is Some when the token source can provide UTF-16 column
information (e.g., StrToGraphQLTokenSource), and None when it
cannot (e.g., RustMacroGraphQLTokenSource in libgraphql-macros).
For example, ‘a’ (1 UTF-16 code unit) adds 1 to this count, while ‘🎉’ (a surrogate pair requiring 2 UTF-16 code units) adds 2 to this count.
For LSP compatibility, prefer this method when available.
Sourcepub fn byte_offset(&self) -> usize
pub fn byte_offset(&self) -> usize
Returns the 0-based byte offset from document start.
Trait Implementations§
Source§impl Clone for SourcePosition
impl Clone for SourcePosition
Source§fn clone(&self) -> SourcePosition
fn clone(&self) -> SourcePosition
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more