Struct Parser

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

A struct for gradually parsing data from a string from left to right.

Each time a method is called, the processed portion of the string is “consumed”, and future method calls will only consider the remainder of the string.

§Examples

use aoc::Parse;

let s = "move 10 from 271 to 3";
let mut parser = s.as_parser();

assert_eq!(parser.between(" ", " "), "10");
assert_eq!(parser.between(" ", " "), "271");
assert_eq!(parser.after(" "), "3");

Another way to do the same thing is:

use aoc::Parse;

let s = "move 10 from 271 to 3";
let mut parser = s.as_parser().skip(5);

assert_eq!(parser.before(" "), "10");
parser.skip(5);
assert_eq!(parser.before(" "), "271");
parser.skip(3);
assert_eq!(parser.rest(), "3");

Or alternatively:

use aoc::Parse;

let s = "move 10 from 271 to 3";
let mut parser = s.as_parser();

assert_eq!(parser.between("move ", " "), "10");
assert_eq!(parser.between("from ", " "), "271");
assert_eq!(parser.after("to "), "3");

Implementations§

Source§

impl<'a> Parser<'a>

Source

pub fn new(s: &'a str) -> Self

Creates a new Parser from the given &str.

Source

pub fn skip(&mut self, n: usize) -> Self

Skips past the next n bytes (ASCII characters) of the string.

Future method calls on self will work on the remainder of the string.

Both mutates self and returns a copy of self after the mutation.

Panics if the string has less than n bytes left.

§Examples
use aoc::Parse;

let s = "12345foo1234567bar";
let mut parser = s.as_parser().skip(5);

assert_eq!(parser.take(3), "foo");
parser.skip(7);
assert_eq!(parser.rest(), "bar");
Source

pub fn take(&mut self, n: usize) -> &str

Returns the next n bytes (ASCII characters) of the string.

Future method calls on self will then work on the remainder of the string.

Panics if the string has less than n bytes left.

§Examples
use aoc::Parse;

let s = "12345foo1234567bar.";
let mut parser = s.as_parser().skip(5);

assert_eq!(parser.take(3), "foo");
parser.skip(7);
assert_eq!(parser.take(3), "bar");
Source

pub fn rest(self) -> &'a str

Returns the remainder of the string, consuming self.

§Examples
use aoc::Parse;

let s = "hello, world!";
let mut parser = s.as_parser();

parser.skip(7);
assert_eq!(parser.rest(), "world!");
Source

pub fn before(&mut self, suffix: &str) -> &'a str

Returns the slice of the string before the first occurrence of suffix.

Future method calls on self will then work on the remainder of the string after suffix.

Panics if suffix is not contained in the remainder of the string.

§Examples
use aoc::Parse;

let s = "move 10 from 271 to 3";
let mut parser = s.as_parser().skip(5);

assert_eq!(parser.before(" "), "10");
parser.skip(5);
assert_eq!(parser.before(" "), "271");
parser.skip(3);
assert_eq!(parser.rest(), "3");
Source

pub fn after(self, prefix: &str) -> &'a str

Returns the slice of the string after the first occurrence of prefix, consuming self.

Panics if prefix is not contained in the remainder of the string.

§Examples
use aoc::Parse;

let s = "move 10 from 271 to 3";
let mut parser = s.as_parser();

assert_eq!(parser.between(" ", " "), "10");
assert_eq!(parser.between(" ", " "), "271");
assert_eq!(parser.after(" "), "3");
Source

pub fn between(&mut self, prefix: &str, suffix: &str) -> &'a str

Returns the slice of the string after the first occurrence of prefix, and before the next occurrence of suffix.

Future method calls on self will then work on the remainder of the string after suffix.

§Examples
use aoc::Parse;

let s = "move 10 from 271 to 3";
let mut parser = s.as_parser();

assert_eq!(parser.between("move ", " "), "10");
assert_eq!(parser.between("from ", " "), "271");
assert_eq!(parser.after("to "), "3");

Trait Implementations§

Source§

impl<'a> Clone for Parser<'a>

Source§

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

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for Parser<'a>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Parser<'a>

§

impl<'a> RefUnwindSafe for Parser<'a>

§

impl<'a> Send for Parser<'a>

§

impl<'a> Sync for Parser<'a>

§

impl<'a> Unpin for Parser<'a>

§

impl<'a> UnwindSafe for Parser<'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.