SpannedStr

Struct SpannedStr 

Source
pub struct SpannedStr<'a> { /* private fields */ }
Expand description

Represents a portion of input file.

This is represented the same way as Span, but with an additionnal content field.

It is initially created with the input_file function, and can then be splitted at desired index. Its content and span can be accessed with the content and span methods.

§Example

The following code shows how to extract a sequence of numbers separated by non-digit characters.

use lisbeth_error::span::{Span, SpannedStr};

#[derive(Debug)]
struct Number(u32, Span);

// Parses a number from input, if any failure occurs, returns None
fn extract_number<'a>(input: SpannedStr<'a>) -> (Number, SpannedStr<'a>) {
    let (matched, tail) = input.take_while(char::is_numeric);

    let value = matched.content().parse().unwrap();
    let number = Number(value, matched.span());
    (number, tail)
}

let input = SpannedStr::input_file("42 or nothing");
let (number, tail) = extract_number(input);

assert_eq!(number.0, 42);
assert_eq!(tail.content(), " or nothing");

Implementations§

Source§

impl<'a> SpannedStr<'a>

Source

pub fn input_file(content: &'a str) -> SpannedStr<'a>

Creates a new SpannedStr from an input file.

This returned spanned string can then be splitted at various places during the parsing step.

§Example
use lisbeth_error::span::SpannedStr;

let file_content = "fn main() { println!(\"Hello, world!\"); }";

let whole_file = SpannedStr::input_file(file_content);
Source

pub const fn span(self) -> Span

Returns the contained Span.

The span contains the position at which the content is located in the input data.

§Example
use lisbeth_error::span::SpannedStr;

let a = SpannedStr::input_file("foo bar");
let b = SpannedStr::input_file("baz qux");

// a and b have the same length and the same starting point, so they
// have the same span.
assert_eq!(a.span(), b.span());
Source

pub const fn content(self) -> &'a str

Returns the span content.

§Example
use lisbeth_error::span:: SpannedStr;

let a = SpannedStr::input_file("hello");
assert_eq!(a.content(), "hello");
Source

pub fn split_at(self, idx: usize) -> (SpannedStr<'a>, SpannedStr<'a>)

Splits the spanned string at a given byte index.

This method works the same way as str::split_at, but updates the span so that it is still correct.

§Panics

This method panics when one of the condition listed in str::split_at is met.

§Example
use lisbeth_error::span::SpannedStr;

let input = SpannedStr::input_file("helloworld");
let (left, right) = input.split_at(5);

assert_eq!(left.content(), "hello");
assert_eq!(right.content(), "world");
Source

pub fn take_while<F>(self, f: F) -> (SpannedStr<'a>, SpannedStr<'a>)
where F: FnMut(char) -> bool,

Returns the longest prefix of input that match a given a condition.

§Example
use lisbeth_error::span::SpannedStr;

let i = SpannedStr::input_file("42 101");
let (number, tail) = i.take_while(char::is_numeric);

assert_eq!(number.content(), "42");
assert_eq!(tail.content(), " 101");

Trait Implementations§

Source§

impl<'a> Clone for SpannedStr<'a>

Source§

fn clone(&self) -> SpannedStr<'a>

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<'a> Debug for SpannedStr<'a>

Source§

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

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

impl<'a> PartialEq for SpannedStr<'a>

Source§

fn eq(&self, other: &SpannedStr<'a>) -> 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<'a> Copy for SpannedStr<'a>

Source§

impl<'a> StructuralPartialEq for SpannedStr<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for SpannedStr<'a>

§

impl<'a> RefUnwindSafe for SpannedStr<'a>

§

impl<'a> Send for SpannedStr<'a>

§

impl<'a> Sync for SpannedStr<'a>

§

impl<'a> Unpin for SpannedStr<'a>

§

impl<'a> UnwindSafe for SpannedStr<'a>

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.