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>
impl<'a> SpannedStr<'a>
Sourcepub fn input_file(content: &'a str) -> SpannedStr<'a>
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);Sourcepub const fn span(self) -> Span
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());Sourcepub const fn content(self) -> &'a str
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");Sourcepub fn split_at(self, idx: usize) -> (SpannedStr<'a>, SpannedStr<'a>)
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");Sourcepub fn take_while<F>(self, f: F) -> (SpannedStr<'a>, SpannedStr<'a>)
pub fn take_while<F>(self, f: F) -> (SpannedStr<'a>, SpannedStr<'a>)
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>
impl<'a> Clone for SpannedStr<'a>
Source§fn clone(&self) -> SpannedStr<'a>
fn clone(&self) -> SpannedStr<'a>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more