Skip to main content

SourcePosition

Struct SourcePosition 

Source
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 is Some when the token source can provide it (e.g. StrToGraphQLTokenSource), and None when it cannot (e.g. RustMacroGraphQLTokenSource in libgraphql-macros which uses proc_macro2::Span that 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_utf8 advances by 1 for each UTF-8 character
  • col_utf16 advances by the character’s UTF-16 length (1 or 2 code units)

Implementations§

Source§

impl SourcePosition

Source

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 line
  • col_utf16: 0-based UTF-16 code unit offset within current line, or None if not available (e.g., from proc_macro2::Span)
  • byte_offset: 0-based byte offset from document start
Source

pub fn line(&self) -> usize

Returns the 0-based line number.

Source

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.

Source

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.

Source

pub fn byte_offset(&self) -> usize

Returns the 0-based byte offset from document start.

Source

pub fn to_ast_pos(&self) -> AstPos

Convert to an AstPos for compatibility with graphql_parser types.

Note: AstPos uses 1-based line and column numbers, so this method adds 1 to both. The column is always derived from col_utf8.

Trait Implementations§

Source§

impl Clone for SourcePosition

Source§

fn clone(&self) -> SourcePosition

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SourcePosition

Source§

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

Formats the value using the given formatter. Read more
Source§

impl PartialEq for SourcePosition

Source§

fn eq(&self, other: &SourcePosition) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for SourcePosition

Source§

impl StructuralPartialEq for SourcePosition

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.