[][src]Struct lisbeth_error::span::SpannedStr

pub struct SpannedStr<'a> { /* fields omitted */ }

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

impl<'a> SpannedStr<'a>[src]

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

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

pub const fn span(self) -> Span[src]

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

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

Returns the span content.

Example

use lisbeth_error::span:: SpannedStr;

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

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

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

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

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

impl<'a> Clone for SpannedStr<'a>[src]

impl<'a> Copy for SpannedStr<'a>[src]

impl<'a> Debug for SpannedStr<'a>[src]

impl<'a> PartialEq<SpannedStr<'a>> for SpannedStr<'a>[src]

impl<'a> StructuralPartialEq for SpannedStr<'a>[src]

Auto Trait Implementations

impl<'a> RefUnwindSafe for SpannedStr<'a>[src]

impl<'a> Send for SpannedStr<'a>[src]

impl<'a> Sync for SpannedStr<'a>[src]

impl<'a> Unpin for SpannedStr<'a>[src]

impl<'a> UnwindSafe for SpannedStr<'a>[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.