Skip to main content

SourceSpan

Struct SourceSpan 

Source
pub struct SourceSpan {
    pub start: LineColumn,
    pub end: LineColumn,
}
Expand description

Source region as start/end line and column.

Uses proc_macro2::LineColumn for positions (1-based line, 0-based column). This type is Send + Sync. Build it from a proc_macro2::Span via SourceSpan::from_proc_macro2 when you have one (e.g. in the parser).

Fields§

§start: LineColumn

Start position (inclusive).

§end: LineColumn

End position (exclusive; one past the last character, matching proc_macro2::Span::end()).

Implementations§

Source§

impl SourceSpan

Source

pub fn new( start_line: usize, start_column: usize, end_line: usize, end_column: usize, ) -> Self

Builds a span from raw line/column values.

Lines are 1-based, columns are 0-based (matching proc_macro2::LineColumn).

§Examples
use cel_parser::SourceSpan;

let span = SourceSpan::new(1, 0, 1, 5);
assert_eq!(span.start.line, 1);
assert_eq!(span.end.column, 5);
Source

pub fn from_proc_macro2(span: Span) -> Self

Extracts start/end line and column from a proc_macro2::Span.

Use this when creating errors in the parser or other code that has a proc_macro2::Span; the result is Send + Sync and can be stored in CELError for use from async or other threads.

§Examples
use proc_macro2::Span;
use cel_parser::SourceSpan;

let span = SourceSpan::from_proc_macro2(Span::call_site());
assert_eq!(span.start.line, span.end.line);
Source

pub fn from_proc_macro2_range(start: Span, end: Span) -> Self

Builds a span from two proc_macro2::Span values (start of start, end of end).

Use this when an expression spans multiple tokens and you want a range span.

§Examples
use proc_macro2::Span;
use cel_parser::SourceSpan;

let start = Span::call_site();
let end = Span::call_site();
let span = SourceSpan::from_proc_macro2_range(start, end);
assert_eq!(span.start.line, span.end.line);
Source

pub fn to_byte_range(&self, source: &str) -> Range<usize> ⓘ

Converts this span (1-based lines, 0-based character columns) to a byte-offset range within source.

  • Precondition: source is the exact original text this span’s line/column positions were recorded against.

  • Precondition: self.start.line >= 1 (spans use 1-based line numbers).

  • Precondition: self.end does not precede self.start (self.end.line > self.start.line, or they’re equal and self.end.column >= self.start.column).

  • Complexity: O(n) in the length of source.

§Examples
use cel_parser::SourceSpan;

let span = SourceSpan::new(1, 0, 1, 5);
let range = span.to_byte_range("hello world");
assert_eq!(&"hello world"[range], "hello");

Trait Implementations§

Source§

impl Clone for SourceSpan

Source§

fn clone(&self) -> SourceSpan

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for SourceSpan

Source§

impl Debug for SourceSpan

Source§

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

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

impl Default for SourceSpan

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Eq for SourceSpan

Source§

impl PartialEq for SourceSpan

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for SourceSpan

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> Same for T

Source§

type Output = T

Should always be Self
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.