TextSpan

Struct TextSpan 

Source
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: Position

The starting position of the text span.

§end: Position

The ending position of the text span.

§literal: String

The literal text contained in the span.

Implementations§

Source§

impl TextSpan

Source

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);
Source

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 of TextSpan objects 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");
Source

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.

Source

pub fn literal<'a>(&self, input: &'a str) -> &'a str

Extracts the literal text from the given input string based on the start and end positions.

§Arguments
  • input - The input string from which to extract the literal text.
§Returns

A slice of the input string that corresponds to the span’s range.

Trait Implementations§

Source§

impl Clone for TextSpan

Source§

fn clone(&self) -> TextSpan

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 TextSpan

Source§

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)");
Source§

impl Default for TextSpan

Source§

fn default() -> TextSpan

Creates a new TextSpan with default values.

§Returns

A new TextSpan with the starting and ending positions set to (0, 0, 0) and an empty string.

Source§

impl PartialEq for TextSpan

Source§

fn eq(&self, other: &TextSpan) -> 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 TextSpan

Source§

impl StructuralPartialEq for TextSpan

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more