pub struct TextSpan {
pub start: Position,
pub end: Position,
pub literal: String,
}Expand description
Represents a span of text between two positions, including the literal text.
Fields§
§start: PositionThe starting position of the text span.
end: PositionThe ending position of the text span.
literal: StringThe literal text contained in the span.
Implementations§
Source§impl TextSpan
impl TextSpan
Sourcepub fn new(start: Position, end: Position, literal: String) -> TextSpan
pub fn new(start: Position, end: Position, literal: String) -> TextSpan
Creates a new TextSpan from a starting position, an ending position, and a literal string.
§Arguments
start- The starting position of the text span.end- The ending position of the text span.literal- The literal text represented by the span.
§Example
use roan_error::{Position, TextSpan};
let start = Position::new(1, 1, 0);
let end = Position::new(1, 5, 4);
let span = TextSpan::new(start, end, "test".to_string());
assert_eq!(span.length(), 4);Sourcepub fn combine(spans: Vec<TextSpan>) -> Option<TextSpan>
pub fn combine(spans: Vec<TextSpan>) -> Option<TextSpan>
Combines multiple TextSpan objects into one. The spans are sorted by their starting positions.
§Panics
Panics if the input vector is empty.
§Arguments
spans- A vector ofTextSpanobjects to combine.
§Returns
A new TextSpan that spans from the start of the first span to the end of the last span,
with the concatenated literal text.
§Example
use roan_error::{Position, TextSpan};
let span1 = TextSpan::new(Position::new(1, 1, 0), Position::new(1, 5, 4), "test".to_string());
let span2 = TextSpan::new(Position::new(1, 6, 5), Position::new(1, 10, 9), "span".to_string());
let combined = TextSpan::combine(vec![span1, span2]);
assert_eq!(combined.unwrap().literal, "testspan");Sourcepub fn length(&self) -> usize
pub fn length(&self) -> usize
Returns the length of the span, calculated as the difference between the end and start indices.
§Returns
The length of the text span in bytes.
Trait Implementations§
Source§impl Debug for TextSpan
impl Debug for TextSpan
Source§fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>
Formats the TextSpan as "literal" (line:column).
§Example
use roan_error::{Position, TextSpan};
let span = TextSpan::new(Position::new(1, 1, 0), Position::new(1, 5, 4), "test".to_string());
assert_eq!(format!("{:?}", span), "\"test\" (1:1)");impl Eq for TextSpan
impl StructuralPartialEq for TextSpan
Auto Trait Implementations§
impl Freeze for TextSpan
impl RefUnwindSafe for TextSpan
impl Send for TextSpan
impl Sync for TextSpan
impl Unpin for TextSpan
impl UnwindSafe for TextSpan
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.