FromStrParser

Struct FromStrParser 

Source
pub struct FromStrParser<T> { /* private fields */ }
Expand description

Generic parser implementation for any type that implements FromStr trait

This parser consumes exactly one token and parses it using the target type’s FromStr implementation for producing the parser’s result. Note, that on failure, the error value is discarded. FromStrParser does not recognize any attributes and does not yield any completion suggestions.

Note, that there is no blanket implementation of Parsable that uses this parser implementation. If you want to use this implementation for any custom type, you must either implement Parsable for it or specify this parser explicitly when performing parsing or completion.

The implementation is similar to IntegerParser with the only difference being more specific error handling by the IntegerParser.

§Example

The following example demonstrates how to use FromStrParser for custom types that implement FromStr:

use cmdparse::parsers::FromStrParser;
use cmdparse::{parse, Parsable};
use std::str::FromStr;

#[derive(Debug, PartialEq, Eq, Copy, Clone)]
enum MyBool {
    Yes,
    No,
}

impl FromStr for MyBool {
    type Err = (); // the error type is discarded by the parser

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "yes" => Ok(MyBool::Yes),
            "no" => Ok(MyBool::No),
            _ => Err(()),
        }
    }
}

impl<Ctx> Parsable<Ctx> for MyBool {
    type Parser = FromStrParser<Self>;
}

assert_eq!(parse::<_, MyBool>("yes", ()), Ok(MyBool::Yes));
assert_eq!(parse::<_, MyBool>("no", ()), Ok(MyBool::No));

Trait Implementations§

Source§

impl<T: Clone> Clone for FromStrParser<T>

Source§

fn clone(&self) -> FromStrParser<T>

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<T> Debug for FromStrParser<T>

Source§

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

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

impl<T> Default for FromStrParser<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: FromStr, Ctx> Parser<Ctx> for FromStrParser<T>

Source§

type Value = T

The type that this parser will parse the input stream into.
Source§

fn parse<'a>( &self, input: TokenStream<'a>, _ctx: Ctx, ) -> ParseResult<'a, Self::Value>

Parsers the beginning of the token stream into a Value. Read more
Source§

fn complete<'a>( &self, input: TokenStream<'a>, _ctx: Ctx, ) -> CompletionResult<'a>

Constructs the completion suggestions for the last token of the input stream Read more
Source§

impl<T: Copy> Copy for FromStrParser<T>

Auto Trait Implementations§

§

impl<T> Freeze for FromStrParser<T>

§

impl<T> RefUnwindSafe for FromStrParser<T>
where T: RefUnwindSafe,

§

impl<T> Send for FromStrParser<T>
where T: Send,

§

impl<T> Sync for FromStrParser<T>
where T: Sync,

§

impl<T> Unpin for FromStrParser<T>
where T: Unpin,

§

impl<T> UnwindSafe for FromStrParser<T>
where T: UnwindSafe,

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> ParsableTransformation<Box<T>> for T

Source§

type Input = T

The type that will be transformed by the implementation if this trait (Self::Input -> O).
Source§

fn transform( input: <T as ParsableTransformation<Box<T>>>::Input, ) -> Result<Box<T>, ParseError<'static>>

Performs the transformation.
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.