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: LineColumnStart position (inclusive).
end: LineColumnEnd position (exclusive; one past the last character, matching proc_macro2::Span::end()).
Implementations§
Source§impl SourceSpan
impl SourceSpan
Sourcepub fn new(
start_line: usize,
start_column: usize,
end_line: usize,
end_column: usize,
) -> Self
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);Sourcepub fn from_proc_macro2(span: Span) -> Self
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);Sourcepub fn from_proc_macro2_range(start: Span, end: Span) -> Self
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);Sourcepub fn to_byte_range(&self, source: &str) -> Range<usize> ⓘ
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:
sourceis 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.enddoes not precedeself.start(self.end.line > self.start.line, or they’re equal andself.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
impl Clone for SourceSpan
Source§fn clone(&self) -> SourceSpan
fn clone(&self) -> SourceSpan
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more