Skip to main content

MapParser

Struct MapParser 

Source
pub struct MapParser<T, M> { /* private fields */ }
Expand description

Maps the result of a parser to a new value

§Example

use microparsec::{MapParser, IntegerParser, SequenceParser, StringParser, ParserRc, ContextParserT, StringParserT, parsers};

let hello_parser = StringParser::new("Hello");
let space_parser = StringParser::new(" ");
let world_parser = StringParser::new("World");
let hello_world_parser = SequenceParser::new(parsers!(hello_parser, space_parser, world_parser));
let res = MapParser::new(ParserRc::new(hello_world_parser),
                         |res| Ok(res.val.join("")))
            .parse("Hello World");
assert_eq!(res.unwrap().val, "Hello World");

let res = MapParser::new(ParserRc::new(IntegerParser::new()),
                         |res| Ok(res.val.parse::<u32>().unwrap()))
            .parse("234");
assert_eq!(res.unwrap().val, 234);

Implementations§

Source§

impl<T, M> MapParser<T, M>

Source

pub fn new( parser: ParserRc<dyn ContextParserT<T>>, mapper: fn(Success<T>) -> Result<M, String>, ) -> Self

Trait Implementations§

Source§

impl<T: Clone, M: Clone> Clone for MapParser<T, M>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T, M> ContextParserT<M> for MapParser<T, M>

Source§

fn get_generic_error_message(&self) -> String

Returns a generic error message of the parser that is configured at initialization and independent of the runtime result of the attempted parse
Source§

fn get_parser_type(&self) -> ParserType

Returns the ParserType of the parser (used for stack traces and error messages)
Source§

fn parse_from_context(&self, ctx: Context) -> Result<Success<M>, Failure>

Consumes a Context and attempts to parse it
Source§

impl<T, M> StringParserT<M> for MapParser<T, M>

Source§

fn parse<S: AsRef<str>>(&self, txt: S) -> Result<Success<T>, Failure>

Consumes a string type and attempts to parse it

Auto Trait Implementations§

§

impl<T, M> !RefUnwindSafe for MapParser<T, M>

§

impl<T, M> !Send for MapParser<T, M>

§

impl<T, M> !Sync for MapParser<T, M>

§

impl<T, M> !UnwindSafe for MapParser<T, M>

§

impl<T, M> Freeze for MapParser<T, M>
where Rc<dyn ContextParserT<T>>: Freeze, fn(Success<T>) -> Result<M, String>: Freeze,

§

impl<T, M> Unpin for MapParser<T, M>
where Rc<dyn ContextParserT<T>>: Unpin, fn(Success<T>) -> Result<M, String>: Unpin,

§

impl<T, M> UnsafeUnpin for MapParser<T, M>

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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.